External Exam Download Resources Web Applications Games Recycle Bin

Jinja2 {{variables}} in templates

simpletemplate2.py

from flask import *
import random

app = Flask(__name__)

@app.route("/")
def main():
    if random.randint(1, 2) == 1:
        return render_template("basic.html", answer="salmon")
    else:
        return render_template("basic.html", answer="plum")
    
app.run()

#put basic.html in templates folder

put Jinja2 html template file in templates folder of the root python folder:

templates\basic.html

<!doctype html>
<h1>Salmon or Plum?</h1><br>
<h1>Python returned: {{answer}} </h1><br>