0
0
Flaskframework~10 mins

Environment-based configuration in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Environment-based configuration
Start Flask App
Read ENV Variable
Select Config Class
Apply Config to App
Run App with Config
App Behavior Depends on Config
The app starts, reads environment variables to pick the right config, applies it, then runs with those settings.
Execution Sample
Flask
import os
from flask import Flask

class Config:
    DEBUG = False

class DevConfig(Config):
    DEBUG = True

app = Flask(__name__)
config_name = os.getenv('FLASK_ENV', 'default')
if config_name == 'development':
    app.config.from_object(DevConfig)
else:
    app.config.from_object(Config)

@app.route('/')
def home():
    return f"Debug is {app.config['DEBUG']}"
This Flask app reads the FLASK_ENV environment variable to decide if debug mode is on or off.
Execution Table
StepActionENV FLASK_ENVConfig Selectedapp.config['DEBUG']Output
1Start appNot read yetNoneNoneNone
2Read FLASK_ENVdevelopmentNoneNoneNone
3Check config_namedevelopmentDevConfigNoneNone
4Apply DevConfigdevelopmentDevConfigTrueNone
5Call home routedevelopmentDevConfigTrueDebug is True
6EnddevelopmentDevConfigTrueApp runs with debug True
💡 App runs with configuration based on FLASK_ENV environment variable.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
config_nameNonedevelopmentdevelopmentdevelopmentdevelopment
app.config['DEBUG']NoneNoneNoneTrueTrue
Key Moments - 2 Insights
Why does the app use DevConfig when FLASK_ENV is 'development'?
Because at step 3 in the execution table, the code checks if config_name equals 'development' and then selects DevConfig, which sets DEBUG to True.
What happens if FLASK_ENV is not set?
The code uses the default Config class which has DEBUG set to False, so the app runs without debug mode.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is app.config['DEBUG'] after applying configuration?
AFalse
BTrue
CNone
DError
💡 Hint
Check step 4 in the execution table where DevConfig is applied.
At which step does the app decide which configuration to use?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the step where config_name is checked to select the config class.
If FLASK_ENV was set to 'production', what config would be selected?
ADevConfig
BNone
CConfig
DError
💡 Hint
The else branch applies Config when FLASK_ENV is not 'development'.
Concept Snapshot
Environment-based configuration in Flask:
- Use environment variables like FLASK_ENV to select config.
- Define config classes with settings (e.g., DEBUG).
- Load config with app.config.from_object().
- App behavior changes based on selected config.
- Helps run app differently in dev, test, production.
Full Transcript
This example shows how a Flask app reads the FLASK_ENV environment variable to choose which configuration class to use. The app defines a base Config class with DEBUG set to False and a DevConfig subclass with DEBUG True. When the app starts, it reads FLASK_ENV. If it is 'development', it applies DevConfig, enabling debug mode. Otherwise, it uses the default Config. The home route returns the current debug setting. This approach lets the app behave differently depending on the environment, like turning on debug mode during development but off in production.