A browser cannot receive new server-generated values after a page has been delivered unless another request is made. For a dynamic Flask page, put the HTML in a Jinja2 template and call `render_template` from a route. Flask renders the template with the supplied data before sending the page. section 6.4.3
The first argument names the template file. Flask looks for `index.html` in a `templates` directory beside `app.py`. Additional keyword arguments provide values to the template: `now=datetime.now()` makes the current timestamp available as `{{now}}`. section 6.4.3
from flask import Flask, render_template #1
from datetime import datetime #2
app = Flask(__name__)
@app.route("/")
def home():
return render_template("index.html", now=datetime.now()) #3
The home route renders `index.html` and supplies a timestamp through the named `now` argument. section 6.4.3
As a page grows, a single `data` argument can hold a dictionary of values. Jinja2 can read its entries to display a timestamp, call a page-visit counter, and loop over a list of banner colors. A template can also extend `base.html`, keeping shared page elements in one place instead of copying them across pages. section 6.4.4
@app.route("/")
def home():
banner_colors = BannerColors().get_colors() #1
return render_template("index.html", data={ #2
"now": datetime.now(), #2
"page_visit": PageVisit(), #2
"banner_colors": { #2
"display": banner_colors, #2
"js": json.dumps(banner_colors) #2
} #2
})
The larger home route passes a nested dictionary containing the time, a visit-counter object, and banner colors for display and JavaScript. section 6.4.4


