Blueprints help organize your Flask app by grouping routes and code. They make big apps easier to manage.
Blueprint creation and registration in Flask
from flask import Blueprint bp = Blueprint('name', __name__, url_prefix='/prefix') @bp.route('/path') def view_func(): return 'Hello from blueprint!' # In main app file from flask import Flask 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('users', __name__, url_prefix='/users') @bp.route('/') def users_home(): return 'Users Home'
bp = Blueprint('admin', __name__, url_prefix='/admin') @bp.route('/dashboard') def admin_dashboard(): return 'Admin Dashboard'
app = Flask(__name__) app.register_blueprint(bp)
This example creates a blog blueprint with two routes. It registers the blueprint to the main app. When you visit '/blog/' you see the blog home. When you visit '/blog/post/1' you see post 1.
from flask import Flask, Blueprint # Create a blueprint for blog blog_bp = Blueprint('blog', __name__, url_prefix='/blog') @blog_bp.route('/') def blog_home(): return 'Welcome to the Blog!' @blog_bp.route('/post/<int:id>') def show_post(id): return f'Post number {id}' # Create main app app = Flask(__name__) # Register the blueprint app.register_blueprint(blog_bp) if __name__ == '__main__': app.run(debug=True)
Blueprints let you organize routes and code separately from the main app.
Always register blueprints before running the app.
You can create many blueprints for different parts of your app.
Blueprints group routes and code for better organization.
Use Blueprint to create, then app.register_blueprint() to add it.
Blueprints help keep your Flask app clean and modular.
Practice
Blueprint in a Flask application?Solution
Step 1: Understand Blueprint role
A Blueprint groups routes and code to keep the app organized and modular.Step 2: Compare options
Only To organize routes and related code into reusable modules describes organizing routes and code. Others describe unrelated tasks.Final Answer:
To organize routes and related code into reusable modules -> Option CQuick Check:
Blueprint purpose = organize routes [OK]
- Thinking Blueprint creates database models
- Confusing Blueprint with authentication
- Assuming Blueprint serves static files
Solution
Step 1: Recall Blueprint constructor
The Blueprint constructor requires the name and import name (usually __name__).Step 2: Check options
blog = Blueprint('blog', __name__) uses correct order: name 'blog' and __name__. Others miss or swap arguments.Final Answer:
blog = Blueprint('blog', __name__) -> Option DQuick Check:
Blueprint(name, __name__) = correct [OK]
- Omitting __name__ argument
- Swapping argument order
- Passing app instead of __name__
from flask import Flask, Blueprint
app = Flask(__name__)
hello_bp = Blueprint('hello', __name__)
@hello_bp.route('/hello')
def hello():
return 'Hello from Blueprint!'
app.register_blueprint(hello_bp)
if __name__ == '__main__':
app.run()Solution
Step 1: Check route registration
The route '/hello' is defined in the Blueprint and the Blueprint is registered on the app.Step 2: Understand response
Accessing '/hello' calls the hello() function returning 'Hello from Blueprint!'.Final Answer:
Hello from Blueprint! -> Option BQuick Check:
Registered route returns defined string [OK]
- Forgetting to register the Blueprint
- Using wrong route path
- Expecting error without registration
from flask import Flask, Blueprint
app = Flask(__name__)
admin_bp = Blueprint('admin', __name__)
@admin_bp.route('/dashboard')
def dashboard():
return 'Admin Dashboard'
# Missing line here
if __name__ == '__main__':
app.run()Solution
Step 1: Check Blueprint creation and route
Blueprint and route are correctly defined with proper syntax and return statement.Step 2: Look for registration
The Blueprint is not registered with the Flask app usingapp.register_blueprint(admin_bp).Final Answer:
Blueprint is not registered with the app -> Option AQuick Check:
Missing app.register_blueprint() causes route not found [OK]
- Forgetting app.register_blueprint()
- Misplacing route decorator
- Missing return in route function
user_bp and admin_bp, each with a route '/profile'. How do you register them so that user_bp routes are under '/user' and admin_bp routes under '/admin' in the main app?Solution
Step 1: Understand url_prefix usage
To group routes under a path prefix, useurl_prefixwhen registering Blueprints.Step 2: Apply prefixes correctly
Registeruser_bpwithurl_prefix='/user'andadmin_bpwithurl_prefix='/admin'.Final Answer:
app.register_blueprint(user_bp, url_prefix='/user')\napp.register_blueprint(admin_bp, url_prefix='/admin') -> Option AQuick Check:
url_prefix sets route base path [OK]
- Not using url_prefix causing route conflicts
- Swapping prefixes between Blueprints
- Trying to set route paths inside Blueprint incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
