0
0
Flaskframework~10 mins

Blueprint routes and templates in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Blueprint routes and templates
Create Blueprint
Define routes in Blueprint
Register Blueprint in app
Run Flask app
Request URL matching Blueprint route
Render template from Blueprint folder
This flow shows how a Flask Blueprint is created, routes are defined inside it, then registered with the main app, and finally how a request triggers rendering a template from the Blueprint.
Execution Sample
Flask
from flask import Flask, Blueprint, render_template
app = Flask(__name__)
bp = Blueprint('bp', __name__, template_folder='templates')
@bp.route('/')
def home():
    return render_template('home.html')

app.register_blueprint(bp, url_prefix='/bp')

if __name__ == '__main__':
    app.run()
Defines a Blueprint with a route '/' that renders 'home.html', then registers it under '/bp' prefix in the main app.
Execution Table
StepActionEvaluationResult
1Create Blueprint 'bp'Blueprint object createdbp ready with template_folder='templates'
2Define route '/' in bpRoute '/' added to bpbp has route '/'
3Register bp with app at '/bp'bp routes prefixed with '/bp'app routes include '/bp/'
4Run Flask appApp listens for requestsServer running
5Request URL '/bp/'Matches bp route '/' with prefixCalls home() function
6home() calls render_template('home.html')Template searched in bp's templates folderHTML content of home.html returned
7Response sent to browserBrowser displays rendered pageUser sees home page
8Request URL '/bp/unknown'No matching route404 Not Found error
💡 Execution stops after response sent or 404 error if route not found
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 5After Step 6Final
bpNoneBlueprint objectBlueprint with route '/'Registered with appRoute matched on requestTemplate renderedResponse sent
appFlask appFlask appFlask appFlask app with bp routesRunning serverRunning serverRunning server
Key Moments - 3 Insights
Why does the URL '/bp/' match the Blueprint route '/'?
Because the Blueprint is registered with url_prefix='/bp', all its routes get that prefix. So '/' inside Blueprint becomes '/bp/' in the app (see execution_table step 3 and 5).
Where does Flask look for the template 'home.html' when using Blueprint?
Flask looks inside the Blueprint's template_folder directory specified when creating the Blueprint (step 6). This keeps templates organized per Blueprint.
What happens if a URL does not match any Blueprint route?
Flask returns a 404 Not Found error because no route matches the request (step 8).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result after Step 3?
ABlueprint routes are registered with the app with prefix '/bp/'
BBlueprint is created but not registered
CApp is running and serving requests
DTemplate is rendered
💡 Hint
Check the 'Result' column in Step 3 of the execution_table
At which step does the template 'home.html' get rendered?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look for 'render_template' call in the 'Action' column
If the Blueprint was registered without url_prefix, what would be the URL to access 'home.html'?
A/home.html
B/bp/
C/
D/bp/home.html
💡 Hint
Refer to the explanation in key_moments about url_prefix effect on routes
Concept Snapshot
Blueprints organize routes and templates in Flask.
Create Blueprint with template_folder.
Define routes inside Blueprint.
Register Blueprint with app and optional url_prefix.
Request URL uses prefix + route path.
Templates load from Blueprint folder.
Full Transcript
In Flask, Blueprints help organize routes and templates separately from the main app. First, you create a Blueprint object and specify a folder for its templates. Then, you define routes inside this Blueprint using decorators. Next, you register the Blueprint with the main Flask app, optionally adding a URL prefix. When the app runs and receives a request matching the Blueprint's route (including prefix), it calls the route function. This function can render a template located in the Blueprint's template folder. If no route matches, Flask returns a 404 error. This structure keeps code and templates modular and easier to manage.