liveBook · Manning

Render_template

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

render_template is a Flask function that renders a named template with supplied context data and returns the resulting response content.

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

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Shows the template filename, default directory, keyword context, and a larger dictionary context; does not detail Jinja2 internals.
Examples
Uses a current-time page, then adds a page-visit counter and a list of banner colors.
What it assumes
Uses Python and a Flask route while introducing template placement and Jinja2 value substitution directly.
What you can do after
Build it from scratch: serve a dynamic Flask page using a template and supplied context values.
How it connects
Connects Flask routes and Jinja2 templates to context data, template inheritance, and HTML.

Also explained in

Compare how 3 books explain render_template →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.