Structuring Large Apps with Blueprints
Blueprints allow you to organize your web application into modules. This helps in scaling applications, organizing code layers, and separating domain concerns.
1 Creating & Registering Blueprints
Python — blog/routes.py
from flask import Blueprint, render_template
blog_bp = Blueprint("blog", __name__, template_folder="templates")
@blog_bp.route("/")
def index():
return render_template("blog/index.html")
Python — main_app.py
from flask import Flask
from blog.routes import blog_bp
app = Flask(__name__)
# Registering Blueprint with URL prefix
app.register_blueprint(blog_bp, url_prefix="/blog")
2 Code Challenge
Challenge: Create an
api_bp blueprint structure for user-endpoints, configuring routing prefixes mapping to /api/v1/users/ cleanly.