I'm pretty new to Flask and d3 but having some difficulty in loading json from an api into d3 functions using d3.json
I'm broadly trying to follow the steps here for an api I have access to: https://realpython.com/web-development-with-flask-fetching-data-with-requests/
here's my run.py
file:
from flask import Flask, render_template, jsonify
from harvest_scraper import api_scraper
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/data')
def data():
return jsonify(api_scraper())
I can confirm that when I run the app and go to the /data
endpoint that I get valid json and the data that I want to use in my d3 visualisation.
Am I correct in thinking that I then can use this endpoint as a url to load the json into any d3 scripts on template pages?
Here's the index.html
template I have at the moment:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>d3 api example</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
</head>
<body>
<script>
d3.json('/data', function(data) {
console.log(data);
});
</script>
</body>
</html>
What I'm expecting here is for my /
page to make a request to the /data
page and console.log
the json from the api call.
At the moment I can't see anything on the /
page and cannot use this data in any d3 scripts in the index.html
template. There are no errors being raised either that I can see.
I think there's something I'm either not understanding the format of the json that is returned or the way to access data from an endpoint.