elbecker
elbecker

Reputation: 75

Using flask to display part of a python dictionary in an HTML file

I'm following a tutorial about how to use Flask and render_template to display a value from my a dictionary pinDict in an HTML file. When I navigate to http://127.0.0.1:5000/21/on, only "Hi" appears. Why is this?

#app.py
from flask import Flask, render_template

app = Flask(__name__)
pinDict = {"21": {"name": "GPIO 21", "state": "off"}}

@app.route('/')
def index():
    return "hey"

@app.route("/<changePin>/<action>")
def turnOn(changePin, action):
    return render_template("home.html", **pinDict)

if __name__ == '__main__':
    app.run(debug=True, host="0.0.0.0")

My home.html file is this:

<!DOCTYPE html>
<html>
    <body id = "myButton">
    <h1>RPi Web Server</h1>
    {% for pin in pinDict %}
    <h2> {{pinDict[pin]["name"]}} </h2>
    {% endfor %}
    <script>
        function printLog() {
            var myHTML = "<h1> Hi </h1>"
            document.getElementById("myButton").innerHTML = myHTML;
        }
    </script>
    <script>printLog()</script>
    </body>
</html>

Pretty simple. Kinda confused why it's not displaying, since this is pretty much the exact same code from the tutorial I followed. Would appreciate some help, thanks for your time.

Upvotes: 1

Views: 667

Answers (1)

IoaTzimas
IoaTzimas

Reputation: 10624

You would better use

return render_template("home.html", pinDict=pinDict)

instead of

return render_template("home.html", **pinDict)

Upvotes: 2

Related Questions