Is it possible to have a variable that is defined in a python file be passed through to a html file in the same directory and used in a 'script' tag?
Synopsis: Im producing a flask website that pulls data from a database and uses it to create a line chart in chartjs. Flask uses jijna to manage templates. The data in the databse has been normalised so that it can be read by the chart script (label is in a list, data as a tuple). So far I have produced routes so that each page can be accessed and passed the value into the return_template parameter as shown here:
@app.route('/room2')
def room2():
return render_template('room2.html', title = 'Room2', time = newLabel, count = newData )
with the html for the room being called along with the title which is accessed here:
{% if title %}
<title>Website title - {{ title }}</title>
{% else%}
<title>Website</title>
{% endif %}
The chart is being generated in the html file, and I want to take the data (time and count) and use it in the script the same way the title is used. However the script for chart.js doesnt take {{ }} (which is essentially a print function anyway). So far ive managed to get the data i want INTO the required html file, which is proven by the data being printed out onto the page itself but im not sure how to translate it into a variable to be used by chartjs.
Any suggestions? (NOTE: using routes like this may be the wrong starting point but it's one that makes logical sense and hasn't produced any errors beside logical)
Files:
- data.py: Takes information from database and converts it into two variables;
label = ['09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00']
data = ['4,7,2,3,9,9,5]
- routes.py: holds the routes to all the pages,
- layout.html: holds overarching html code for website,
- room.html: holds graph, extends from layout

script.jsfile. You either need to put a<script>block in the template with some JS likevar myVar = {{variable}};or another good method would be this answer which uses a data attribute of an HTML tag, and retrieves this in Javascript code. However for loading more complicated objects (x,y cords, etc) it may be advisable to use the fetch API to make a separate request on page load. I'll see if I can write an answer demonstrates the latter method. - v25@app.route('/room2')in a py file, but i will try to make the variable as suggested (ill use let instead of var although the two are proobably interchangeable right?) Thank you for writing the demonstration i think that will help me understand what you mean - pynooblet myVar = '{{variable}}';(was getting an error if the single quotes werent there) within the script, which printed out the graph but in an interesting way: the lines of the line chart werent connected to each other, and the labels in the y axis werent correct (will add an image to the post to show update) - pynoob