Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 766
Ricky92d Avatar asked Aug 12 '15 12:08

Ricky92d


People also ask

How do you put a variable in a script in HTML?

To add the content of the javascript variable to the html use innerHTML() or create any html tag, add the content of that variable to that created tag and append that tag to the body or any other existing tags in the html.

How do I display the results of a function in HTML?

To write out into HTML and display its output, you can use the “document. write()” function. To write into an HTML element and display its output, you can use the “document. getElementById()” function with the “.

How do you print a variable in JavaScript?

log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Syntax: console. log(A);


1 Answers

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

like image 180
mhawke Avatar answered Oct 03 '22 08:10

mhawke