I wish to print a table in an html with Flask(jinja) from a nested dict:
Python part:
@app.route("/")
def index():
#the nested dict exemple:
dict_temp = {0: {'symbol': 'NFLX', 'name': 'Netflix', 'share': '6', 'price': '123', 'total':
''}, 1: {'symbol': 'NFLX', 'name': 'Netflix', 'share': '6', 'price': '123', 'total': ''}, 2: {'symbol': 'NFLX', 'name': 'Netflix', 'share': '6', 'price': '123', 'total': ''}}
parent_dict = dict_temp
return render_template("test.html", parent_dict = parent_dict)
Jinja template with the table:
<table style="width:100%">
<tr>
<th>Symbol</th>
<th>Name</th>
<th>Shares</th>
<th>Price</th>
<th>Total</th>
</tr>
{% for dict_item in parent_dict %}
<tr>
{% for key, value in dict_item.items() %}
<td>{{value}}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
The code work with this python:
parent_dict = [{'A':'item1 A','B':'item1 B'},{'a':'item2 A','b':'Item2 B', 'c':'Item2 C'}]
How could I loop with jinja
and a nested dict
of this type:
{1:{'A':'item1 A','B':'item1 B'}, 2:{'a':'item2 A','b':'Item2 B', 'c':'Item2 C'}, ... }
Or maybe I have to build my input differently?