Home > Net >  How to pass json array to html page via flask render_template
How to pass json array to html page via flask render_template

Time:10-26

I'm working on attendance monitoring, where each student attendance status will be captured via API response in flask and need to show the response in dashboard format.

Below the sample response we get -

[
{"StudentName":"Name1", "Status":"Present"},
{"StudentName":"Name2", "Status":"Absent"}
]

I'm able to capture individual student details using array[0] and passing only 1 value to html page via flask render_template().

How to pass the array like structure to html page and show the status of each student ?

CodePudding user response:

Did you tried to simply pass the entire array of dictionaries?

return render_template("page.html", attendance=array_name)

Then, in the jinja2 template you can loop through it in order to display the information.

{% for student in attendance %}

<p>{{ student['StudentName'] }}</p>
<p>{{ student['Status'] }}</p>

{% endfor %}
  • Related