AtomLearnAtomLearn
DashboardGoalsPathAchievementsReviewSign In
DashboardFlaskBlueprints
FlaskNot Started

Blueprints

Organize routes into modular Blueprints — the Flask equivalent of routers

0%
Knowledge0%
Learn & Drill
Fluency0%
Drill & Speed
Retention0%
Mastery & Review
Confidence0%
All modes
Practice

Free tier: read the explanation here. Upgrade to Pro for Drills, Speed challenges & Mastery badges.

Upgrade
Knowledge
Fluency
Retention

Knowledge Debt detected

You can study this freely — but your score may plateau if these foundations have gaps. The Mastery badge requires them to be solid.

Explanation

s split a Flask app into modular components — each feature gets its own file with its own routes.

Defining a Blueprint:

python # app/users/routes.py from flask import Blueprint, jsonify, request users_bp = Blueprint("users", __name__, url_prefix="/users") @users_bp.route("/") def list_users(): return jsonify([]) @users_bp.route("/<int:user_id>") def get_user(user_id): return jsonify({"id": user_id}) @users_bp.route("/", methods=["POST"]) def create_user(): data = request.get_json() return jsonify({"id": 1, **data}), 201

Registering Blueprints in the app factory:

python # app/__init__.py from flask import Flask from .users.routes import users_bp from .posts.routes import posts_bp def create_app(config=None): app = Flask(__name__) if config: app.config.from_object(config) app.register_blueprint(users_bp) app.register_blueprint(posts_bp) return app

Resulting routes:

GET /users/ → list_users GET /users/42 → get_user(42) POST /users/ → create_user

url_for with Blueprints — prefix with the blueprint name: ``python from flask import url_for url_for("users.get_user", user_id=42) # /users/42

Typical project structure:

app/ __init__.py ← create_app() factory config.py users/ __init__.py routes.py ← Blueprint definition models.py schemas.py posts/ routes.py auth/ routes.py

Examples

Versioned API with Blueprints

Blueprints make API versioning clean — separate blueprints, separate URL prefixes

from flask import Flask, Blueprint, jsonify

v1 = Blueprint("v1", __name__, url_prefix="/api/v1")
v2 = Blueprint("v2", __name__, url_prefix="/api/v2")

@v1.route("/users")
def users_v1():
    return jsonify({"version": 1, "users": []})

@v2.route("/users")
def users_v2():
    return jsonify({"version": 2, "data": [], "meta": {}})

def create_app():
    app = Flask(__name__)
    app.register_blueprint(v1)
    app.register_blueprint(v2)
    return app

# GET /api/v1/users → {"version": 1, ...}
# GET /api/v2/users → {"version": 2, ...}

Blueprint-level before_request

Blueprint before_request hooks apply only to that blueprint — cleaner than app-wide middleware

from flask import Blueprint, request, jsonify, g

auth_bp = Blueprint("auth", __name__, url_prefix="/auth")
api_bp  = Blueprint("api",  __name__, url_prefix="/api")

@api_bp.before_request
def require_token():
    """Runs before every route in this blueprint."""
    token = request.headers.get("Authorization", "").removeprefix("Bearer ")
    if not token:
        return jsonify({"error": "Authentication required"}), 401
    g.user_id = verify_token(token)   # store for route handlers

@api_bp.route("/me")
def me():
    return jsonify({"user_id": g.user_id})

How well did you understand this?

Next in Flask

Error Handlers & Middleware

Continue