📖 Scenario: You are building a simple Flask web application that needs to behave differently depending on the environment it runs in, such as development or production. This is common in real projects where you want to enable debugging in development but disable it in production for security.
🎯 Goal: Create a Flask app that reads its configuration from an environment variable called FLASK_ENV. The app should set debug mode to true if FLASK_ENV is development, and false otherwise.
📋 What You'll Learn
Create a Flask app instance named app
Read the environment variable FLASK_ENV to determine the environment
Set app.config['DEBUG'] to true if FLASK_ENV is development
Set app.config['DEBUG'] to false if FLASK_ENV is not development
Add a simple route / that returns 'Hello, Flask!'
💡 Why This Matters
🌍 Real World
Many web applications need to behave differently in development and production environments. Using environment variables to control configuration is a common and secure way to manage this.
💼 Career
Understanding environment-based configuration is essential for deploying Flask apps safely and effectively in real-world jobs.
Progress0 / 4 steps
1
Create the Flask app instance
Import Flask from flask and create a Flask app instance called app.
Flask
Hint
Use Flask(__name__) to create the app instance.
2
Read the environment variable FLASK_ENV
Import os and create a variable called env that reads the environment variable FLASK_ENV using os.getenv.
Flask
Hint
Use os.getenv('FLASK_ENV') to get the environment variable.
3
Set app.config['DEBUG'] based on environment
Use an if statement to set app.config['DEBUG'] = True if env == 'development', otherwise set app.config['DEBUG'] = False.
Flask
Hint
Use a simple if and else to set the debug config.
4
Add a route that returns a greeting
Add a route to app for the path '/' using @app.route('/'). Define a function called home that returns the string 'Hello, Flask!'.
Flask
Hint
Use @app.route('/') decorator and define a function that returns the greeting.
Practice
(1/5)
1. What is the main reason to use environment-based configuration in a Flask app?
easy
A. To avoid using environment variables completely
B. To write all configuration directly in the main app file
C. To separate settings for development, testing, and production environments
D. To hardcode sensitive data like passwords in the code
Assuming the environment variable MY_CONFIG is not set.
medium
A. It will silently ignore missing MY_CONFIG and use default config
B. It will raise a RuntimeError because MY_CONFIG is missing
C. It will load config from a file named 'MY_CONFIG'
D. It will load config from environment variables automatically
Solution
Step 1: Understand from_envvar behavior
from_envvar expects an environment variable with a filepath; if missing, it raises RuntimeError.
Step 2: Check what happens if MY_CONFIG is not set
Since MY_CONFIG is missing, Flask raises RuntimeError to alert missing config file.
Final Answer:
It will raise a RuntimeError because MY_CONFIG is missing -> Option B
Quick Check:
Missing env var causes RuntimeError [OK]
Hint: from_envvar requires env var set or it errors [OK]
Common Mistakes:
Assuming it ignores missing env var
Thinking it loads from env vars automatically
Confusing from_envvar with from_object
Expecting silent fallback to defaults
5. You want your Flask app to load different configs based on the environment variable FLASK_ENV. Which code snippet correctly implements this?
hard
A. if os.getenv('FLASK_ENV') == 'production':
app.config.from_object('config.ProductionConfig')
else:
app.config.from_object('config.DevelopmentConfig')
B. app.config.from_object('config.' + FLASK_ENV + 'Config')
C. app.config.from_envvar('FLASK_ENV')
D. app.config.from_object('config.DefaultConfig')
Solution
Step 1: Check how to read environment variable
Use os.getenv('FLASK_ENV') to get the current environment string.
Step 2: Use conditional logic to load correct config
If environment is 'production', load ProductionConfig; otherwise load DevelopmentConfig.
Final Answer:
if os.getenv('FLASK_ENV') == 'production':
app.config.from_object('config.ProductionConfig')
else:
app.config.from_object('config.DevelopmentConfig') -> Option A
Quick Check:
Use os.getenv + if-else to select config [OK]
Hint: Use os.getenv and if-else to pick config class [OK]
Common Mistakes:
Trying to concatenate string without quotes
Using from_envvar with FLASK_ENV directly
Not handling default environment
Loading only one config regardless of env
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.