Playing around with FastAPI
Published on 19 Sep 2024
Trying out FastAPI for rapid prototyping..
FastAPI documentation: https://fastapi.tiangolo.com/tutorial/#run-the-code
Create virtual environment
python -m venv .venv
Activate virtual environment
source venv/bin/activate
Install fastapi
pip install fastapi[all]
To check what packages are installed
pip freeze
Run a sample example.
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
```main.py
Run the program
```bash
fastapi dev main.py
The above code is called a path operation. It has two componenets, functiona nd the decorator.
from fastapi import FastAPI
app = FastAPI()
#Decorator
@app.get("/")
#Function
async def root():
return {"message": "Hello World"}
To enable auto-reload server, pass –reload flag
fastapi dev main.py --reload
or
uvicorn main:app --reload
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "beep boop"}
@app.get("/posts")
def get_posts():
return {"data": "This is the post"}
On http://127.0.0.1:8000/posts we get the following output
{
"data": "This is the post"
}
all tags
EV TIL book-review bookmark bootcamp chi containers embedded frontend gist git github golang homelab imagemagick internet javascript jekyll k8s linux mental-model mgmt mysql opensource paper personal prototyping psychology python review semiconductor social-media sprituality sustainable til tools unix virtualization writing