How to display a dictionary on an html page with flask?

Issue

How to display a dictionary on an html page with flask? Sorry for the stupid question, I can’t find a good example of both an html page and a python file.

Solution

backend

@app.route('/')
def print_dict():
    d = {'hello': 'world'}
    return render_template('base.html', d=d)

base.html:

<ul>
{% for key, value in d.items() %}
    <li>{{ key }}: {{ value}} </li>
{% endfor %}
</ul>

Answered By – orion_tvv

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published