Blueprints help split a big Flask app into smaller parts. This makes the app easier to build and manage.
Why blueprints organize large applications in Flask
from flask import Blueprint bp = Blueprint('name', __name__, url_prefix='/prefix') @bp.route('/route') def view_func(): return 'Hello from blueprint!' # In main app file from flask import Flask from your_blueprint_file import bp app = Flask(__name__) app.register_blueprint(bp)
The first argument to Blueprint is the blueprint's name.
url_prefix adds a prefix to all routes in the blueprint.
bp = Blueprint('blog', __name__, url_prefix='/blog') @bp.route('/') def blog_home(): return 'Welcome to the blog!'
bp = Blueprint('admin', __name__, url_prefix='/admin') @bp.route('/dashboard') def dashboard(): return 'Admin dashboard here.'
This example shows a simple Flask app with a blueprint for user pages. The user blueprint has a route '/profile' that shows a user profile page. The blueprint is registered to the main app with the prefix '/user'. So the full URL is '/user/profile'.
from flask import Flask, Blueprint # Create a blueprint for user pages user_bp = Blueprint('user', __name__, url_prefix='/user') @user_bp.route('/profile') def profile(): return 'User profile page' # Create main app app = Flask(__name__) # Register the blueprint app.register_blueprint(user_bp) if __name__ == '__main__': app.run(debug=True)
Blueprints let you group related routes and code together.
They help keep your main app file small and clean.
You can register many blueprints in one app.
Blueprints split big Flask apps into smaller parts.
They make code easier to manage and reuse.
Use blueprints to organize routes with URL prefixes.
Practice
blueprints in large applications?Solution
Step 1: Understand the purpose of blueprints and compare options
Blueprints help organize code by splitting a large app into smaller parts. Only splitting the app into smaller parts matches the blueprint's role.Final Answer:
To split the app into smaller, manageable parts -> Option AQuick Check:
Blueprints organize code [OK]
- Thinking blueprints speed up the app
- Confusing blueprints with security features
- Assuming blueprints generate templates automatically
blog?Solution
Step 1: Recall Blueprint constructor syntax and check options
The Blueprint constructor requires the blueprint name and the module name (usually __name__). Only blog = Blueprint('blog', __name__) uses Blueprint('blog', __name__), which is correct syntax.Final Answer:
blog = Blueprint('blog', __name__) -> Option BQuick Check:
Blueprint(name, __name__) [OK]
- Omitting __name__ parameter
- Using wrong class name like FlaskBlueprint
- Swapping parameters order
from flask import Flask, Blueprint
blog = Blueprint('blog', __name__, url_prefix='/blog')
@blog.route('/')
def index():
return 'Hello from blog!'
app = Flask(__name__)
app.register_blueprint(blog)Solution
Step 1: Understand blueprint URL prefix and check the route path
The blueprint uses url_prefix='/blog', so all its routes start with '/blog'. The route is '/', so combined with prefix it becomes '/blog/'.Final Answer:
/blog/ -> Option AQuick Check:
Blueprint prefix + route = /blog/ [OK]
- Ignoring url_prefix and using '/' only
- Assuming route name adds to URL
- Confusing '/blog/' with '/blog/index'
from flask import Flask, Blueprint
app = Flask(__name__)
blog = Blueprint('blog', __name__)
@app.route('/home')
def home():
return 'Home Page'
app.register_blueprint(blog)Solution
Step 1: Identify where routes are defined and understand blueprint usage
The route '/home' is defined on the app, not on the blueprint. Blueprint routes must be defined using the blueprint object, not the app.Final Answer:
Routes are defined on app, not on the blueprint -> Option CQuick Check:
Routes must use blueprint, not app [OK]
- Defining routes on app instead of blueprint
- Thinking url_prefix is mandatory
- Believing blueprint name must be 'app'
admin and user. You want URLs like /admin/dashboard and /user/profile. How should you organize and register these blueprints?Solution
Step 1: Use separate blueprints with url_prefix and define routes inside each
To get URLs like '/admin/dashboard' and '/user/profile', each blueprint needs its own url_prefix. Routes go inside their blueprint, then both blueprints register on the app.Final Answer:
Create two blueprints with url_prefix='/admin' and '/user', define routes inside each, then register both on app -> Option DQuick Check:
Separate blueprints + url_prefix = correct URLs [OK]
- Using one blueprint for all routes without prefixes
- Not using url_prefix and hardcoding paths
- Skipping blueprints and defining routes on app
Ready to prove it?
Pass the test and this pattern moves unknown
