0
0
Flaskframework~10 mins

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

Choose your learning style9 modes available
Concept Flow - Application factory pattern preview
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 app
Run app
This flow shows how the application factory function creates and configures the Flask app, then returns it for running.
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
Defines a function that creates and configures a Flask app, then returns it.
Execution Table
StepActionEvaluationResult
1Call create_app()Function startsEnter create_app function
2Create Flask app instanceapp = Flask(__name__)app object created
3Set config DEBUG=Trueapp.config['DEBUG'] = Trueapp config updated
4Define route '/'@app.route('/')Route registered
5Define home() functiondef home(): return 'Hello, Flask!'Route handler ready
6Return appreturn appapp instance returned
7Assign app = create_app()app = create_app()app variable holds Flask app
8Run appapp.run()Flask server starts
9ExitApp running, waiting for requestsExecution stops here until interrupted
💡 Execution stops after app.run() starts the server and waits for requests.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 6After Step 7
appundefinedFlask app instanceConfig DEBUG=True setReturned from create_appAssigned to app variable
Key Moments - 2 Insights
Why do we define the app inside a function instead of globally?
Defining app inside create_app delays creation until called, allowing flexible configuration and testing. See execution_table steps 1-6.
What happens if we call app.run() outside create_app?
app.run() starts the server and blocks further code. It is called after create_app returns the app, as shown in steps 7-8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of 'app' after step 3?
AUndefined
BFlask app instance with DEBUG=True in config
CFunction create_app itself
DRoute '/' registered
💡 Hint
Check variable_tracker column 'After Step 3' and execution_table step 3
At which step is the route '/' registered?
AStep 2
BStep 6
CStep 4
DStep 8
💡 Hint
Look at execution_table step descriptions for route registration
If we did not return 'app' from create_app, what would happen at step 7?
Aapp variable would be None or undefined
Bapp would be a Flask instance anyway
Capp.run() would start normally
DThe server would start twice
💡 Hint
Refer to execution_table step 6 and 7 about returning and assigning app
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.
- Return the app instance.
- Call create_app() to get the app and run it.
- Enables flexible app setup and testing.
Full Transcript
The Application Factory Pattern in Flask means we write a function called create_app that builds and configures the Flask app. When we call this function, it creates the app instance, sets configuration like DEBUG mode, registers routes such as '/' with their handlers, and then returns the app. After that, we assign the returned app to a variable and run it. This pattern helps us create the app only when needed, making it easier to configure differently for testing or production. The execution steps show calling create_app, creating the app, setting config, registering routes, returning the app, assigning it, and running the server. Variables like 'app' change from undefined to a Flask instance with config set. Key points include why the app is created inside a function and when the server starts running.