0
0
Flaskframework~10 mins

Environment variable management in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to load environment variables from a .env file using Flask.

Flask
from flask import Flask
from dotenv import [1]

app = Flask(__name__)

[1]()
Drag options to blanks, or click blank then click option'
Aload_env
Bload_dotenv
Cdotenv
Denv_load
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dotenv()' instead of 'load_dotenv()'.
Trying to call a function that does not exist.
2fill in blank
medium

Complete the code to access the environment variable named 'SECRET_KEY' in Flask.

Flask
import os

secret_key = os.environ.get([1])
Drag options to blanks, or click blank then click option'
A'SECRET_KEY'
B'SECRETKEY'
C'KEY_SECRET'
D'SECRET'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect variable names like 'SECRET' or 'KEY_SECRET'.
Forgetting to put the variable name in quotes.
3fill in blank
hard

Fix the error in the code to set a default value for an environment variable 'DATABASE_URL' if it is not set.

Flask
import os

database_url = os.environ.get('DATABASE_URL', [1])
Drag options to blanks, or click blank then click option'
A0
BNone
C''
D'sqlite:///default.db'
Attempts:
3 left
💡 Hint
Common Mistakes
Using None or empty string which may cause connection errors.
Using numeric zero which is not a valid URL.
4fill in blank
hard

Fill both blanks to create a Flask config from environment variables with a fallback default.

Flask
app.config['DEBUG'] = os.environ.get([1], [2]) == 'True'
Drag options to blanks, or click blank then click option'
A'FLASK_DEBUG'
B'DEBUG'
C'False'
D'True'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'DEBUG' instead of 'FLASK_DEBUG' as environment variable name.
Using boolean False instead of string 'False' as default.
5fill in blank
hard

Fill all three blanks to create a dictionary of environment variables filtered by a prefix.

Flask
filtered_env = {k: v for k, v in os.environ.items() if k.startswith([1]) and v != [2] and len(v) > [3]
Drag options to blanks, or click blank then click option'
A'APP_'
B''
C0
D'ENV_'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong prefix like 'APP_'.
Comparing to None instead of empty string.
Using length check with wrong number.