patternpythonflaskModerate
Flask application factory pattern for testability and config isolation
Viewed 0 times
Flask 2.x+
application factorycreate_appinit_apptestingconfig isolationextensions
Error Messages
Problem
Creating the Flask app at module level (app = Flask(__name__)) makes it impossible to create multiple app instances with different configs — needed for testing and multi-tenant setups.
Solution
Wrap app creation in a create_app() factory function. Pass a config object or string. Tests call create_app(TestingConfig) to get an isolated app instance.
# app/__init__.py
from flask import Flask
from .extensions import db, login_manager
def create_app(config_object='app.settings.DevelopmentConfig'):
app = Flask(__name__)
app.config.from_object(config_object)
# Initialize extensions
db.init_app(app)
login_manager.init_app(app)
# Register blueprints
from .auth import auth_bp
from .api import api_bp
app.register_blueprint(auth_bp)
app.register_blueprint(api_bp)
return app
# extensions.py
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
db = SQLAlchemy() # Unbound — init_app() binds it
login_manager = LoginManager()Why
Extensions created without an app (db = SQLAlchemy()) are unbound. init_app() registers them with a specific app instance. The factory pattern ensures each test gets a fresh, isolated app instance without global state leaking between tests.
Gotchas
- Accessing db.session outside an app context raises RuntimeError — use app.app_context() or @app.teardown_appcontext
- current_app proxy only works inside an active request or app context
- Extensions initialized with the app constructor (db = SQLAlchemy(app)) break the factory pattern
- Flask CLI (flask run) looks for create_app() in the module pointed to by FLASK_APP
Context
Flask apps that need testing, multiple configurations, or extension management
Revisions (0)
No revisions yet.