0
0
FastAPIframework~10 mins

Environment variable management in FastAPI - 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 using Pydantic BaseSettings.

FastAPI
from pydantic import BaseSettings

class Settings(BaseSettings):
    app_name: str

settings = Settings()
print(settings.[1])
Drag options to blanks, or click blank then click option'
Aenv_var
Bload_env
Capp_name
Dconfig
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call a method that does not exist on the settings instance.
Using a wrong attribute name that is not defined in the Settings class.
2fill in blank
medium

Complete the code to specify a custom environment file for Pydantic BaseSettings.

FastAPI
from pydantic import BaseSettings

class Settings(BaseSettings):
    debug: bool

    class Config:
        env_file = [1]

settings = Settings()
print(settings.debug)
Drag options to blanks, or click blank then click option'
A"variables.txt"
B"config.json"
C"settings.yaml"
D".env"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a file format that Pydantic does not parse by default.
Forgetting to include quotes around the filename string.
3fill in blank
hard

Fix the error in the code to correctly read an integer environment variable.

FastAPI
from pydantic import BaseSettings

class Settings(BaseSettings):
    port: int

settings = Settings()
print(settings.[1])
Drag options to blanks, or click blank then click option'
Aport
BPORT
Cport_number
Dget_port
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase or different names that do not match the attribute.
Trying to call a method instead of accessing the attribute.
4fill in blank
hard

Fill both blanks to create a Pydantic Settings class that reads a string and a boolean from environment variables.

FastAPI
from pydantic import BaseSettings

class Settings(BaseSettings):
    [1]: str
    [2]: bool

settings = Settings()
print(settings.api_key, settings.debug_mode)
Drag options to blanks, or click blank then click option'
Aapi_key
Bdebug
Cdebug_mode
Dapi_secret
Attempts:
3 left
💡 Hint
Common Mistakes
Using attribute names that do not match the environment variables.
Mixing up the types or names for the variables.
5fill in blank
hard

Fill all three blanks to create a Settings class that reads a host string, a port integer, and a debug boolean from environment variables.

FastAPI
from pydantic import BaseSettings

class Settings(BaseSettings):
    [1]: str
    [2]: int
    [3]: bool

settings = Settings()
print(settings.host, settings.port, settings.debug)
Drag options to blanks, or click blank then click option'
Ahost
Bport
Cdebug
Dhostname
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent or incorrect attribute names.
Not matching the type annotations to the variable types.