-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello_world.py
More file actions
29 lines (24 loc) · 1.1 KB
/
hello_world.py
File metadata and controls
29 lines (24 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Import FastAPI to create the API
from fastapi import FastAPI
# Import uvicorn to run the app from this file
import uvicorn
# Create a FastAPI app instance
app = FastAPI(
title="My First API", # Title shown in docs
description="A simple API using FastAPI", # Short description
version="1.0.0" # Version info
)
# Define the root route (homepage)
@app.get("/") # This handles GET requests to "/"
def read_root():
return {"message": "Hello World! Welcome to FastAPI"} # JSON response
# Define a route that takes a name from the URL
@app.get("/hello/{name}") # Handles GET requests like "/hello/Ali"
def read_item(name: str): # Takes 'name' from the URL as a string
return {"message": f"Hello, {name}!"} # Personalized response
# This block runs the server if this file is executed directly
if __name__ == "__main__":
# Start the server using uvicorn
uvicorn.run("hello_world:app", host="127.0.0.1", port=8000, reload=True)
# "main" is the filename (without .py), "app" is the FastAPI instance
# reload=True allows auto-reloading on code changes