1

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.

2
  • 1
    Try return render_template("home.html", pinDict=pinDict) Commented Oct 1, 2020 at 15:17
  • Great, i will add it as answer Commented Oct 1, 2020 at 15:24

1 Answer 1

2

You would better use

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

instead of

return render_template("home.html", **pinDict)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.