0
0
FastAPIframework~10 mins

Configuration 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 import the BaseSettings class from Pydantic for configuration.

FastAPI
from pydantic import [1]
Drag options to blanks, or click blank then click option'
AConfig
BBaseSettings
CField
DBaseModel
Attempts:
3 left
💡 Hint
Common Mistakes
Importing BaseModel instead of BaseSettings
Using Field which is for defining fields, not settings
Trying to import a non-existent Config class
2fill in blank
medium

Complete the code to define a configuration class that reads the app name from environment variables.

FastAPI
class Settings(BaseSettings):
    app_name: str = [1]
Drag options to blanks, or click blank then click option'
A"DefaultApp"
B"MyApp"
C"AppName"
D"FastAPI App"
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving the value empty
Using a variable name instead of a string
Using incorrect quotes
3fill in blank
hard

Fix the error in the code to create a singleton settings instance for reuse.

FastAPI
def get_settings():
    if not hasattr(get_settings, "_settings"):
        get_settings._settings = [1]()
    return get_settings._settings
Drag options to blanks, or click blank then click option'
ASettings
BBaseSettings
CConfig
DSettingsConfig
Attempts:
3 left
💡 Hint
Common Mistakes
Using BaseSettings directly which is a base class
Using a non-existent class name
Forgetting to instantiate the class
4fill in blank
hard

Fill both blanks to load environment variables from a .env file using Pydantic settings.

FastAPI
class Settings(BaseSettings):
    class Config:
        env_file = [1]
        env_file_encoding = [2]
Drag options to blanks, or click blank then click option'
A".env"
B"utf-8"
C"config.env"
D"ascii"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong file names like "config.env"
Using incorrect encoding like "ascii"
Omitting the quotes around strings
5fill in blank
hard

Fill all three blanks to create a FastAPI app that uses dependency injection to get settings.

FastAPI
from fastapi import FastAPI, Depends

app = FastAPI()

@app.get("/")
async def read_root(settings: Settings = Depends([1])):
    return {"app_name": settings.[2]

settings_instance = [3]()
Drag options to blanks, or click blank then click option'
Aget_settings
Bapp_name
CSettings
DSettingsConfig
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name instead of the function in Depends
Accessing a wrong attribute name
Instantiating a wrong class