8

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?

6
  • Could you be more specific on how you are using python with your web app? Are you using Django? Commented Aug 12, 2015 at 12:43
  • 3
    you sure your question is not already answered thousands of times and googling for it is of no use? Commented Aug 12, 2015 at 12:44
  • @RobertPounder I am using Flask. Have information on an API saved as a variable and wish to display this on a HTML page Commented Aug 12, 2015 at 12:46
  • That would certainly be the right approach if you were using a templating language that supported it and passing VariableName to the template... but are you? Could you give a minimal reproducible example? Why not just try it and see? Commented Aug 12, 2015 at 12:46
  • {{variablename}} Should work. stackoverflow.com/questions/14975114/… Commented Aug 12, 2015 at 12:50

1 Answer 1

38

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

Sign up to request clarification or add additional context in comments.

1 Comment

thanks! but what if my variable is declared into a class?

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.