Challenge - 5 Problems
Flask Env Config Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Flask app configuration?
Given this Flask app code, what will be the value of
app.config['DEBUG'] when FLASK_ENV is set to production?Flask
import os from flask import Flask app = Flask(__name__) if os.getenv('FLASK_ENV') == 'development': app.config['DEBUG'] = True else: app.config['DEBUG'] = False print(app.config['DEBUG'])
Attempts:
2 left
💡 Hint
Think about what happens when the environment variable is not 'development'.
✗ Incorrect
The code sets DEBUG to True only if FLASK_ENV is 'development'. For 'production', DEBUG is set to False.
📝 Syntax
intermediate2:00remaining
Which option correctly loads environment variables into Flask config?
Which code snippet correctly loads environment variables from a .env file into a Flask app configuration using python-dotenv?
Attempts:
2 left
💡 Hint
Look for the method that accepts a dictionary of environment variables.
✗ Incorrect
app.config.from_mapping(os.environ) loads all environment variables into Flask config.
❓ state_output
advanced2:00remaining
What is the value of
app.config['DATABASE_URI'] after running this code?Consider this Flask app configuration code snippet. What will be the value of
app.config['DATABASE_URI'] if the environment variable DATABASE_URI is not set?Flask
import os from flask import Flask app = Flask(__name__) app.config['DATABASE_URI'] = os.getenv('DATABASE_URI', 'sqlite:///default.db') print(app.config['DATABASE_URI'])
Attempts:
2 left
💡 Hint
Check the second argument of os.getenv.
✗ Incorrect
os.getenv returns the default value if the environment variable is not set.
🔧 Debug
advanced2:00remaining
Why does this Flask app fail to load environment config?
This Flask app tries to load config from environment variables but fails. What is the error in this code?
Flask
import os from flask import Flask app = Flask(__name__) app.config.from_envvar('MYAPP_SETTINGS') # Assume MYAPP_SETTINGS is set to 'config.py' file path
Attempts:
2 left
💡 Hint
Check how Flask expects config file paths in from_envvar.
✗ Incorrect
Flask's from_envvar requires the environment variable to point to an absolute path to the config file.
🧠 Conceptual
expert2:00remaining
Which statement about Flask environment-based configuration is correct?
Select the correct statement about managing Flask app configuration using environment variables.
Attempts:
2 left
💡 Hint
Think about how Flask config classes work.
✗ Incorrect
app.config.from_object() lets you load different config classes for different environments easily.