0
0
Flaskframework~20 mins

Environment-based configuration in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Env Config Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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'])
ATrue
BRaises KeyError
CNone
DFalse
Attempts:
2 left
💡 Hint
Think about what happens when the environment variable is not 'development'.
📝 Syntax
intermediate
2: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?
A
from dotenv import load_dotenv
load_dotenv()
app.config.from_envvar('MY_CONFIG')
B
from dotenv import load_dotenv
load_dotenv()
app.config.from_prefixed_env('MYAPP_')
C
from dotenv import load_dotenv
load_dotenv()
app.config.from_mapping(os.environ)
D
from dotenv import load_dotenv
load_dotenv()
app.config.from_object('config')
Attempts:
2 left
💡 Hint
Look for the method that accepts a dictionary of environment variables.
state_output
advanced
2: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'])
A'sqlite:///default.db'
BNone
CRaises KeyError
DEmpty string ''
Attempts:
2 left
💡 Hint
Check the second argument of os.getenv.
🔧 Debug
advanced
2: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
AThe config file path must be absolute, relative paths cause failure
BMYAPP_SETTINGS must be a Python module, not a file path
Cfrom_envvar expects the environment variable to contain a file path to a config file, so this is correct
DThe config file must be imported with importlib before calling from_envvar
Attempts:
2 left
💡 Hint
Check how Flask expects config file paths in from_envvar.
🧠 Conceptual
expert
2:00remaining
Which statement about Flask environment-based configuration is correct?
Select the correct statement about managing Flask app configuration using environment variables.
AFlask requires restarting the server to detect changes in environment variables
BUsing app.config.from_object() allows switching configs by passing different config classes
CEnvironment variables cannot override values set in config.py files
DFlask automatically loads .env files without any code when FLASK_ENV is set
Attempts:
2 left
💡 Hint
Think about how Flask config classes work.