0
0
Flaskframework~30 mins

Configuration management in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Flask Configuration Management
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use the @app.route('/') decorator and a function that returns the formatted string.