0
0
Flaskframework~10 mins

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

Choose your learning style9 modes available
Concept Flow - Application factory pattern
Define create_app function
Inside create_app: Create Flask app instance
Configure app settings
Register blueprints and extensions
Return app instance
Call create_app() to get configured app
Run or test the app
The application factory pattern defines a function that creates and configures a Flask app instance, 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, Flask!'
    return app
This code defines a 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 updatedDebug mode enabled
3Define route '/' with home()Route registeredHome route ready
4Return appFunction endsConfigured app instance returned
5Run app.run()App starts runningServer listens for requests
6Request to '/' routehome() calledResponse: 'Hello, Flask!'
7EndNo more stepsExecution stops
💡 Execution stops after app runs and handles requests or program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
appundefinedFlask instance createdDebug=True setRoute '/' addedReturned configured app
Key Moments - 3 Insights
Why do we define the app inside a function instead of at the top level?
Defining the app inside create_app delays creation until called, allowing different configs or tests. See execution_table step 1 where create_app is called to create the app.
How does the route get registered if it's inside the function?
The route decorator runs when create_app is called, adding the route to the app instance before returning it. See execution_table step 3.
What happens if we call create_app multiple times?
Each call creates a new, separate Flask app instance with its own config and routes. This supports testing or multiple app versions.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of 'app' after Step 2?
AApp instance created but debug mode not set
BApp instance not created yet
CApp instance created with debug mode enabled
DApp instance created with routes registered
💡 Hint
Check variable_tracker column 'After Step 2' and execution_table Step 2
At which step does the home route get registered to the app?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look at execution_table Step 3 where route '/' is registered
If we call create_app() twice, what happens to the 'app' variable each time?
AA new app instance is created each time
BApp instance is destroyed after first call
CSame app instance is reused
DApp instance is shared globally
💡 Hint
Refer to key_moments about multiple calls creating separate instances
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/blueprints.
- Return the app instance.
- Call create_app() to get a ready-to-use app.
- Supports flexible config and testing.
Full Transcript
The application factory pattern in Flask means defining a function named create_app that creates a Flask app instance when called. Inside this function, the app is created, configured, and routes or blueprints are registered. Then the app instance is returned. This delays app creation until needed, allowing different configurations or easier testing. When create_app is called, it creates a new app, sets debug mode, adds a home route, and returns the configured app. Running the app starts the server, and requests to routes call the registered functions. Calling create_app multiple times creates separate app instances. This pattern helps organize Flask apps cleanly and flexibly.