Flask interview question · Most asked
What are Blueprints in Flask?
Summary
Blueprints group related routes, templates, static files and error handlers into reusable components that are registered on an app (optionally with a URL prefix), so large apps can be organized by feature.
Longer Answer
A blueprint records operations and applies them when registered. You can register the same blueprint multiple times with different prefixes, and nest blueprints (Flask 2.0+). url_for('orders.detail', id=1) uses the blueprint name as a namespace. Blueprint-specific hooks (bp.before_request) only run for its routes, while bp.before_app_request runs for all requests.
Code Example
from flask import Blueprint, jsonify
bp = Blueprint("orders", __name__)
@bp.get("/<int:order_id>")
def detail(order_id: int):
order = Order.query.get_or_404(order_id)
return jsonify(order.to_dict())
Key Points
- Feature-based modules
- Registered with
url_prefix - Namespaced endpoints
bp.view - Per-blueprint hooks and error handlers
More most-asked Flask questions
- How do Flask sessions work and what are their security implications?
- How do you use SQLAlchemy with Flask (Flask-SQLAlchemy 3.x)?
- What is the application factory pattern in Flask and why use it?
- Explain Flask's application context and request context.
- What is Flask and what does 'microframework' mean?
- How does routing work in Flask, including variable rules and HTTP methods?
- How do you access request data and build responses in Flask?
- How do you implement user authentication in Flask?