>
= FastAPI =
* https://fastapi.tiangolo.com/
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python based on standard Python type hints.
FastAPI works OK without uv. You can build, install, and run FastAPI projects using traditional tools like pip, venv, and poetry
== main.py ==
Install
{{{#!highlight sh
# uv An extremely fast Python package and project manager, written in Rust.
# https://docs.astral.sh/uv/getting-started/installation/
curl -LsSf https://astral.sh/uv/install.sh | sh
pipx install uv
pip install uv
uv self update
pip install --upgrade uv
uv add "fastapi[standard]"
pip install fastapi[standard]
# Virtual Environment: Python's built-in venv module
# Dependency Management: requirements.txt or pyproject.toml
}}}
{{{#!highlight python
from 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 = None):
return {"item_id": item_id, "q": q}
}}}
Run
{{{
uv run fastapi dev
}}}
== pip , venv steps ==
{{{#!highlight sh
cd ~/Documents
mkdir -p fastapi-test
cd fastapi-test
mkdir static templates
python3 -m venv .venv
source .venv/bin/activate
cat << 'EOF' > requirements.txt
fastapi[standard]
pyyaml
jinja2
EOF
pip install -r requirements.txt
cat << 'EOF' > templates/index.html
{{ titulo }}
{{ mensagem }}
Este é o lado Web Server do FastAPI.
Experimenta os outros caminhos:
EOF
cat << 'EOF' > main.py
import yaml
from fastapi import FastAPI
from fastapi.responses import Response
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Hello world!"}
@app.get("/openapi.yaml", include_in_schema=False)
def get_openapi_yaml():
openapi_json = app.openapi()
openapi_yaml = yaml.dump(openapi_json, sort_keys=False)
return Response(content=openapi_yaml, media_type="text/yaml")
EOF
fastapi dev main.py
# /home/vitor/Documents/fastapi-test/.venv/bin/python3 \
# /home/vitor/Documents/fastapi-test/.venv/bin/fastapi dev main.py
# http://127.0.0.1:8000
# Server started at http://127.0.0.1:8000
# OpenAPI/Swagger documentation at http://127.0.0.1:8000/docs
# http://127.0.0.1:8000/openapi.yaml
# http://127.0.0.1:8000/openapi.json
uvicorn main:app --reload
}}}