I am trying to visualize a bunch of data in JSON format by p5js with a server organized by Flask.
Suppose I have a JSON File data.json
[{"a":"1"},{"b":"2"},{"c","3"}]
And my Python code is:
from flask import *
app = Flask(__name__)
def index():
data_list = json.load(open('data.json'))
data_json = json.dumps(data_list)
return render_template("index.html", data_json=data_json)
if __name__ == '__main__':
app.run(port=7775)
So far I figured out how to send my JSON file to HTML, but how to read the JSON file in HTML through p5js? Here is my HTML code:
<!DOCTYPE html>
<html>
<head>
<script src="js/p5.js"></script>
<script src="sketch.js"></script>
</head>
<body>
Hello
</body>
</html>
At first, actually, it looks that, in Flask, HTML can't read p5.js
and sketch.js
correctly. The error code is Failed to load resource: the server responded with a status of 404 (NOT FOUND)
Second, I can open the JSON file in HTML by {{data_json}}
, but how can I transmit to sketch.js
so it can be used for visualization?
What should I do to fix it? Really appreciate your help!