Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to register a startup event handler in FastAPI.
FastAPI
from fastapi import FastAPI app = FastAPI() @app.[1]() async def startup_event(): print("App is starting")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @app.startup() instead of @app.on_startup()
Misspelling the decorator name
✗ Incorrect
The correct decorator to register a startup event in FastAPI is @app.on_startup().
2fill in blank
mediumComplete the code to register a shutdown event handler in FastAPI.
FastAPI
from fastapi import FastAPI app = FastAPI() @app.[1]() async def shutdown_event(): print("App is shutting down")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @app.shutdown() instead of @app.on_shutdown()
Misspelling the decorator name
✗ Incorrect
The correct decorator to register a shutdown event in FastAPI is @app.on_shutdown().
3fill in blank
hardFix the error in the code to properly register a startup event handler.
FastAPI
from fastapi import FastAPI app = FastAPI() @app.[1] async def startup_event(): print("Starting app")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses after the decorator name
Using incorrect decorator names
✗ Incorrect
The decorator must be called with parentheses: @app.on_startup() to register the event.
4fill in blank
hardFill both blanks to create a FastAPI app with startup and shutdown event handlers.
FastAPI
from fastapi import FastAPI app = FastAPI() @app.[1]() async def startup_event(): print("Starting app") @app.[2]() async def shutdown_event(): print("Shutting down app")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up startup and shutdown decorator names
Omitting parentheses after decorators
✗ Incorrect
Use @app.on_startup() for startup and @app.on_shutdown() for shutdown events.
5fill in blank
hardFill all three blanks to create a FastAPI app that prints messages on startup and shutdown, and runs a main route.
FastAPI
from fastapi import FastAPI app = FastAPI() @app.[1]() async def startup_event(): print("App starting") @app.[2]() async def shutdown_event(): print("App shutting down") @app.get("/[3]") async def read_root(): return {"message": "Hello World"}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving the route path blank
Using incorrect decorator names
Omitting parentheses after decorators
✗ Incorrect
Use @app.on_startup() and @app.on_shutdown() for events, and define the route path as 'home'.