Build Modular Flask Apps with Blueprints
A Flask skill for modular blueprint architecture with app factories, blueprint factories, error handlers, and versioned API blueprints.
Why it matters
Streamline the development of scalable and maintainable Flask applications by leveraging the power of blueprints for clean code organization and efficient separation of concerns.
Outcomes
What it gets done
Generate modular Flask application structures using blueprints.
Implement functional separation and hierarchical organization of routes and logic.
Apply blueprint registration patterns and factory patterns for dynamic creation.
Write tests for blueprint-specific functionality and integration.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/vb-flask-blueprint-builder | bash Overview
Flask Blueprint Builder
A Flask skill for structuring applications into modular blueprints, covering an application factory with conditional registration, a blueprint factory pattern for versioned API resources, and shared decorators for auth and role checks. It includes blueprint-scoped error handlers, cross-blueprint URL generation, and pytest fixtures for testing. Use it when structuring a Flask application that has outgrown a single-file layout and needs real feature separation (auth, API, admin) - not for simple single-file Flask apps that don't need blueprint-level separation.
What it does
This skill is expert in Flask blueprint architecture, specializing in modular, scalable Flask applications using blueprints for clean separation of concerns and maintainable code organization. Its core principles are functional separation (grouping related routes, views, and logic into cohesive blueprints), hierarchical organization for complex apps, resource-based design around business domains or API resources, and proper management of shared utilities, models, and configuration. It covers registration patterns - URL prefixes and subdomain handling, conditional registration based on config, and blueprint factories for dynamic creation - and provides a standard blueprint structure (a Blueprint object with url_prefix, template_folder, static_folder, importing its own routes/models/forms), an advanced blueprint factory that dynamically registers Flask-RESTful resources, and a full modular directory layout separating an application factory, shared models, per-feature blueprints (auth, api, admin), and shared utilities.
When to use - and when NOT to
Use this skill when structuring a Flask application that has outgrown a single-file or single-module layout and needs real separation between features like auth, API, and admin. It covers advanced patterns beyond basic routing: blueprint-scoped error handlers and before/after-request hooks, dependency injection into a blueprint factory (e.g. a services blueprint that takes a service_manager), cross-blueprint URL generation via url_for, shared decorators for API-key and role-based access control, and blueprint-specific configuration (auth token expiration, API rate limits, admin enablement toggled per environment). It is not meant for simple single-file Flask apps - the whole point of blueprints is separating concerns across features, which is overhead a small app doesn't need.
Inputs and outputs
### app/__init__.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
db = SQLAlchemy()
login_manager = LoginManager()
def create_app(config_name='development'):
app = Flask(__name__)
app.config.from_object(f'app.config.{config_name.title()}Config')
db.init_app(app)
login_manager.init_app(app)
register_blueprints(app)
return app
def register_blueprints(app):
from app.blueprints.auth import auth_bp
from app.blueprints.api import api_v1, api_v2
from app.blueprints.admin import admin_bp
app.register_blueprint(auth_bp)
app.register_blueprint(api_v1)
app.register_blueprint(api_v2)
if app.config.get('ADMIN_ENABLED', False):
app.register_blueprint(admin_bp, url_prefix='/admin')
Given a set of application features, the skill produces an application factory like the one above with conditional blueprint registration, individual blueprint modules with their own routes/forms/templates, versioned API blueprints (api_v1, api_v2) built via a blueprint factory wrapping Flask-RESTful resources, shared decorators (api_required, role_required), blueprint-scoped error handlers and request hooks, and pytest fixtures for testing blueprint routes against a test client.
Who it's for
Flask developers structuring a growing application into maintainable, feature-scoped modules who need concrete patterns for registration, versioning, shared utilities, and testing - not just the basic Blueprint API. It suits teams planning for microservice extraction or horizontal scaling later, since the skill's deployment guidance covers blueprint versioning for API backwards compatibility and designing blueprints with that eventual split in mind.
FAQ
Common questions
Discussion
Questions & comments ยท 0
Sign In Sign in to leave a comment.