I'm using Flask for the course project. I have some javascript, and HTML files in static and templates folder, the hierarchy looks like this:
/flask_project
-->/run.py
-->/templates
-->/index.html
-->/js
-->/chart.js
There is a function in chart.js that can draw plots, and I need to pass some data from my python code to this function.
@app.route('/index')
def index():
data=[1,2,3,4,5,6]
return render_template('index.html',data=data)
and in my index.html, I have
<script src="{{url_for('static',filename='chart.js')}}"></script>
and in my chart.js I have a function
draw(data){...}
I want to pass my data to this function, but if I add var data={{data}} in .js file, it will give me an error.
I know it would work if I copy codes from .js to <scripts></scripts> in index.html file, but it's very long and will make the HTML file looks ugly.
So is there any way that I can pass variables directly from jinja2 to javascript? Like calling the function draw({{data}}) in index.html?