Reputation: 194
I have 2 lists in my Flask App, I want to display the values of both lists in this manner
Score | Remark
5 | Excellent
2 | Poor
3 | Satisfactory
And this the route in my app.py file
def remarks():
remarks=[]
if request.method == 'POST':
score = list(map(float,(request.form['scores']).split()))
for i in range(len(score)):
if(score[i]>=4):
remarks.append("Excellent")
elif(score[i]==3):
remarks.append("Satisfactory")
else:
remarks.append("Poor")
return render_template('results.html', score=score, remarks=remarks)
This is my results.html file
{% extends 'base.html' %}
{% block content %}
<h3>Here is your report</h3>
{% for s in score %}
<h1>{{s}}</h1>
{% endfor %}
{% endblock %}
How do I display the remarks list besides every score in my results.html file?
Upvotes: 0
Views: 334
Reputation:
You can zip both score and remarks in flask . then you loop over them in front end table
just to declare it: edit the last line of (python) return:
score_remarks = zip(score, remarks)
return render_template('results.html',sr=score_remarks)
and the front-end will look like that:
<table>
<tr>
<th>Score</th>
<th>Remark</th>
</tr>
{% for s in sr %}
<tr>
<td>{{s[0]}}</td>
<td>{{s[1]}}</td>
</tr>
{% endfor %}
</table>
Upvotes: 1
Reputation: 1767
Use a dictionary instead of an array and render it directly as key-value.
In your app.py do this :
def remarks():
score_remark_dict = {}
if request.method == 'POST':
score = list(map(float, (request.form['scores']).split()))
for i in range(len(score)):
if score[i] >= 4:
score_remark_dict[score[i]] = 'Excellent'
elif score[i] == 3:
score_remark_dict[score[i]] = 'Satisfactory'
else:
score_remark_dict[score[i]] = "Poor"
return render_template('results.html', score_remark_dict=score_remark_dict)
In your HTML do this :
{% extends 'base.html' %}
{% block content %}
<h3> Here is your report </h3>
<h1> Score | Remark </h1>
{% for s in score_remark_dict %}
{{ s }} | {{ score_remark_dict[s] }}
{% endfor %}
{% endblock %}
Upvotes: 1