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

← All Flask questions