Structuring a large Flask app: application factory, Blueprints, service layer

Backend Live Coding November 13, 2026 python

An application factory (create_app) so you can have different config in tests and production, Blueprints per domain (like Django apps), and extensions (Flask-SQLAlchemy, Flask-Migrate) that call init_app inside the factory.

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# ======================================================================
# Q: How do you organize a large Flask application?
#
# An application factory (create_app) so you can have different config in tests
# and production, Blueprints per domain (like Django apps), and extensions
# (Flask-SQLAlchemy, Flask-Migrate) that call init_app inside the factory.
# Business logic goes in a service layer, not in the views, so that a Celery
# task can call it too.
# ======================================================================

def create_app(config_object='app.config.Production'):
    app = Flask(__name__)
    app.config.from_object(config_object)
    db.init_app(app)
    migrate.init_app(app, db)
    from app.assets.routes import bp as assets_bp
    app.register_blueprint(assets_bp, url_prefix='/v1/assets')
    return app