gpt-3.5-turbo-0125· openai
Suzhen/CodeChat
Wryie a fastapi example
- User
Wryie a fastapi example
- Assistant
Here is an example of a basic FastAPI application:
pythonfrom fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"} @app.get("/items/{item_id}") def read_item(item_id: int, q: str = None): return {"item_id": item_id, "q": q}In this example, we define a FastAPI app object and create two endpoints: a root endpoint that returns a simple JSON response, and an endpoint that takes a parameter
item_idand a query parameterqand returns a JSON response containing these values.To run this FastAPI application, you can save the code in a file (e.g.,
main.py) and run the following command in your terminal:codeuvicorn main:app --reloadThis will start the FastAPI application on
http://127.0.0.1:8000/and you can access the endpoints defined above by visitinghttp://127.0.0.1:8000/andhttp://127.0.0.1:8000/items/1?q=testrespectively.Note: Make sure to install the necessary dependencies by running .