Ricky92d
Ricky92d

Reputation: 171

How to display a variable in HTML

I am making a web app using Python and have a variable that I want to display on an HTML page. How can I go about doing so? Would using {% VariableName %} in the HTML page be the right approach to this?

Upvotes: 8

Views: 93521

Answers (1)

mhawke
mhawke

Reputation: 87134

This is very clearly explained in the Flask documentation so I recommend that you read it for a full understanding, but here is a very simple example of rendering template variables.

HTML template file stored in templates/index.html:

<html>
<body>
    <p>Here is my variable: {{ variable }}</p>
</body>
</html>

And the simple Flask app:

from flask import Flask, render_template

app = Flask('testapp')

@app.route('/')
def index():
    return render_template('index.html', variable='12345')

if __name__ == '__main__':
    app.run()

Run this script and visit http://127.0.0.1:5000/ in your browser. You should see the value of variable rendered as 12345

Upvotes: 38

Related Questions