0
0
FastAPIframework~30 mins

Uvicorn server basics in FastAPI - Mini Project: Build & Apply

Choose your learning style9 modes available
Uvicorn Server Basics with FastAPI
📖 Scenario: You want to create a simple web server that can respond to visitors with a friendly message. This server will use FastAPI, a popular Python framework, and run on Uvicorn, a fast server for Python web apps.
🎯 Goal: Build a basic FastAPI app and run it using Uvicorn. The app will have one route that returns a welcome message when accessed.
📋 What You'll Learn
Create a FastAPI app instance named app
Define a GET route at path / that returns a JSON message
Set a variable host to "127.0.0.1"
Set a variable port to 8000
Use uvicorn.run() to start the server with the app, host, and port
💡 Why This Matters
🌍 Real World
Web developers use FastAPI with Uvicorn to quickly build and run web APIs that respond to user requests.
💼 Career
Knowing how to set up and run a FastAPI app with Uvicorn is essential for backend developers working with modern Python web frameworks.
Progress0 / 4 steps
1
Create the FastAPI app instance
Import FastAPI from fastapi and create an app instance called app.
FastAPI
Need a hint?

Use app = FastAPI() to create the app instance.

2
Add a GET route for the root path
Define a GET route on app for path "/" that returns a dictionary with the key "message" and value "Hello, Uvicorn!".
FastAPI
Need a hint?

Use @app.get("/") decorator and define a function that returns the dictionary.

3
Set host and port variables
Create two variables: host set to "127.0.0.1" and port set to 8000.
FastAPI
Need a hint?

Assign the string "127.0.0.1" to host and the number 8000 to port.

4
Run the app with Uvicorn
Import uvicorn and use uvicorn.run() to start the server with app, host, and port.
FastAPI
Need a hint?

Use uvicorn.run(app, host=host, port=port) to start the server.