0
0
Flaskframework~10 mins

Application factory pattern deep dive in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Application factory pattern deep dive
Define create_app() function
Inside create_app: Create Flask app instance
Configure app settings
Register extensions (db, login, etc.)
Register blueprints (routes)
Return app instance
Call create_app() to get app
Run app or use in tests
The factory function creates and configures the Flask app step-by-step, then returns it for use.
Execution Sample
Flask
from flask import Flask

def create_app():
    app = Flask(__name__)
    app.config['DEBUG'] = True
    @app.route('/')
    def home():
        return 'Hello!'
    return app
This code defines a factory function that creates a Flask app, sets debug mode, adds a home route, and returns the app.
Execution Table
StepActionEvaluationResult
1Call create_app()Function startsNew Flask app instance created
2Set app.config['DEBUG'] = TrueConfig updatedApp debug mode enabled
3Define route '/' with home()Route registeredHome route ready to respond
4Return appFunction endsApp instance returned
5Use app.run()App runsServer starts listening
6Client requests '/'Route matchesReturns 'Hello!' response
7EndNo more stepsExecution stops
💡 Execution stops after app is returned and run; server handles requests.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
appNoneFlask instance createdDebug=True setHome route addedReturned Flask app
Key Moments - 3 Insights
Why do we create the app inside a function instead of at the top level?
Creating the app inside create_app() delays app creation until called, allowing different configs or tests. See execution_table step 1 where the app is created only when create_app() runs.
How do routes get registered if the app is not created yet?
Routes are registered inside create_app() after the app instance exists (step 3). They are not global but tied to the app returned by the factory.
What happens if we call create_app() multiple times?
Each call creates a new, separate Flask app instance with its own config and routes, as shown by the app variable changing in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what change happens to the app?
ADebug mode is enabled in app config
BA new route is added
CThe app instance is returned
DThe server starts running
💡 Hint
Check the 'Action' and 'Result' columns at step 2 in execution_table.
At which step does the home route get registered?
AStep 1
BStep 4
CStep 3
DStep 5
💡 Hint
Look for 'Define route' in the 'Action' column in execution_table.
If we call create_app() twice, what happens to the 'app' variable in variable_tracker?
AIt stays the same instance
BIt creates a new Flask instance each time
CIt becomes None
DIt raises an error
💡 Hint
Refer to the explanation in key_moments about multiple calls and variable_tracker.
Concept Snapshot
Application Factory Pattern in Flask:
- Define a function create_app() that creates and configures the app.
- Inside, create Flask instance, set config, register routes and extensions.
- Return the app instance.
- Call create_app() to get a ready-to-use app.
- Enables flexible config and testing by creating app on demand.
Full Transcript
The application factory pattern in Flask means we write a function called create_app() that builds the Flask app step-by-step. When called, it creates a new Flask instance, sets configuration like debug mode, adds routes such as the home page, and then returns the app. This way, the app is not created until we want it, which helps when testing or running different versions. The execution table shows each step: calling create_app creates the app, sets config, registers routes, and returns the app. The variable tracker shows how the app variable changes from None to a fully configured Flask app. Key moments clarify why we create the app inside a function and how routes are registered only after the app exists. The visual quiz tests understanding of these steps. This pattern is a clean way to organize Flask apps for flexibility and clarity.