📖 Scenario: You are building a simple Flask web app that needs to manage different settings for development and production environments.
🎯 Goal: Create a Flask app that loads configuration from a dictionary, uses a config variable to select the environment, and applies the correct settings.
📋 What You'll Learn
Create a dictionary called configurations with two keys: development and production, each holding a dictionary of settings.
Add a variable called current_env to select the environment as a string.
Use the current_env variable to load the correct configuration into the Flask app's config.
Add a route / that returns the current environment and a config value.
💡 Why This Matters
🌍 Real World
Managing different settings for development, testing, and production environments is common in real web apps to avoid mistakes and improve security.
💼 Career
Understanding Flask configuration management is essential for backend web developers to build scalable and maintainable applications.
Progress0 / 4 steps
1
Create configuration dictionary
Create a dictionary called configurations with two keys: 'development' and 'production'. Each key should map to a dictionary with these exact entries: 'DEBUG': True for development and 'DEBUG': False for production.
Flask
Hint
Use a dictionary with two keys and nested dictionaries as values.
2
Add environment selector variable
Add a variable called current_env and set it to the string 'development'.
Flask
Hint
Just assign the string 'development' to current_env.
3
Load configuration into Flask app
Create a Flask app instance called app. Then load the configuration from configurations[current_env] into app.config using app.config.from_mapping().
Flask
Hint
Use Flask(__name__) to create the app and from_mapping() to load config.
4
Add route to show current config
Add a route / to the app that returns a string showing the current environment and the value of DEBUG from app.config. Use a function called index and return a string formatted as f"Environment: {current_env}, Debug: {app.config['DEBUG']}".
Flask
Hint
Use the @app.route('/') decorator and a function that returns the formatted string.
Practice
(1/5)
1. What is the primary purpose of app.config in a Flask application?
easy
A. To define URL routes
B. To handle user sessions
C. To render HTML templates
D. To store and manage application settings
Solution
Step 1: Understand the role of app.config
app.config is a special dictionary in Flask used to keep app settings organized.
Step 2: Compare with other Flask features
User sessions, URL routes, and templates are handled by other parts of Flask, not app.config.
Final Answer:
To store and manage application settings -> Option D
Quick Check:
App settings = app.config [OK]
Hint: Remember: config means settings storage [OK]
Common Mistakes:
Confusing config with routing or templates
Thinking config handles user data
Assuming config runs code
2. Which of the following is the correct way to set a configuration value named DEBUG to True in Flask?
Assuming config.py does not exist in the project folder.
medium
A. It will raise a FileNotFoundError
B. It will silently ignore missing file
C. It will set SECRET_KEY before loading file
D. It will overwrite SECRET_KEY with None
Solution
Step 1: Understand from_pyfile behavior
from_pyfile tries to load a Python file and raises an error if missing.
Step 2: Check file existence
Since config.py does not exist, a FileNotFoundError occurs immediately.
Final Answer:
It will raise a FileNotFoundError -> Option A
Quick Check:
Missing config file causes error [OK]
Hint: Missing config files cause errors, not silent skips [OK]
Common Mistakes:
Thinking Flask ignores missing config files
Assuming config keys set before loading file are overwritten
Expecting None values on missing keys
5. You want to load configuration in Flask from environment variables but provide default values if they are missing. Which code snippet correctly achieves this?
hard
A. app.config['DEBUG'] = os.environ['DEBUG'] or False
B. app.config['DEBUG'] = os.getenv('DEBUG') or None
C. app.config['DEBUG'] = os.getenv('DEBUG', False)
D. app.config['DEBUG'] = os.environ.get('DEBUG')
Solution
Step 1: Understand environment variable access
os.getenv('KEY', default) returns the environment value or default if missing.
Step 2: Analyze each option
app.config['DEBUG'] = os.getenv('DEBUG', False) uses os.getenv with default False, correctly providing fallback. app.config['DEBUG'] = os.environ['DEBUG'] or False may raise KeyError if 'DEBUG' missing. app.config['DEBUG'] = os.environ.get('DEBUG') returns None if missing but no default. app.config['DEBUG'] = os.getenv('DEBUG') or None returns None if missing, not False.
Final Answer:
app.config['DEBUG'] = os.getenv('DEBUG', False) -> Option C
Quick Check:
Use getenv with default for safe config [OK]
Hint: Use os.getenv with default to avoid missing keys [OK]
Common Mistakes:
Using os.environ[] without checking existence
Not providing default values
Confusing os.environ.get with os.getenv
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.