Flask
Blueprints
Organize an app into modular blueprints.
By EZ4Code Team
blueprintsstructure
Code
# auth.py
from flask import Blueprint, request, jsonify
auth_bp = Blueprint("auth", __name__, url_prefix="/auth")
@auth_bp.route("/login", methods=["POST"])
def login():
data = request.get_json()
return jsonify({"ok": True, "user": data.get("username")})
# app.py
from flask import Flask
from auth import auth_bp
app = Flask(__name__)
app.register_blueprint(auth_bp)
if __name__ == "__main__":
app.run(debug=True)Explanation
A Blueprint groups related routes into a reusable module registered on the main app. The url_prefix prepends a path to every route in the blueprint. register_blueprint() mounts the blueprint, enabling large apps to split features across files.
More Flask Snippets
Routing
Define routes with methods and dynamic parameters.
Request and Response
Access request data and build custom responses.
Jinja2 Templates
Render templates with context and template inheritance.
Session
Store per-user data in signed session cookies.
Error Handling
Register custom error handlers for HTTP exceptions.
Flask-SQLAlchemy
Define models and query them with Flask-SQLAlchemy.