0Pricing
Python Academy · Lesson

Templates with Jinja2

Render dynamic HTML.

Why Templates?

Returning HTML from Python strings gets messy fast. Templates keep your HTML in separate files and fill in data at render time. Flask uses the Jinja2 engine.

from flask import Flask, render_template
app = Flask(__name__)
# render_template loads a file from the templates/ folder
print('Jinja2 powers Flask templates')

The templates Folder

Flask looks for templates in a folder named templates/ next to your app. Files usually end in .html.

# project/
#   app.py
#   templates/
#     index.html
print('Templates live in templates/')

All lessons in this course

  1. Routes and Views
  2. Templates with Jinja2
  3. Forms and Request Data
  4. Building a REST Endpoint
← Back to Python Academy