|
Size: 1714
Comment:
|
Size: 2261
Comment:
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 52: | Line 52: |
| mkdir static templates | |
| Line 54: | Line 55: |
| echo "fastapi[standard]" > requirements.txt | cat << 'EOF' > requirements.txt fastapi[standard] pyyaml jinja2 EOF |
| Line 59: | Line 66: |
| import yaml | |
| Line 60: | Line 68: |
| from fastapi.responses import Response | |
| Line 65: | Line 74: |
| return {"mensagem": "Olá, Mundo! Criado via terminal!"} | 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") |
| Line 69: | Line 85: |
| # /home/vitor/Documents/fastapi-test/.venv/bin/python3 \ # /home/vitor/Documents/fastapi-test/.venv/bin/fastapi dev main.py |
|
| Line 71: | Line 89: |
| # Documentation at http://127.0.0.1:8000/docs |
# 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 |
Contents
FastAPI
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
1 # uv An extremely fast Python package and project manager, written in Rust.
2 # https://docs.astral.sh/uv/getting-started/installation/
3 curl -LsSf https://astral.sh/uv/install.sh | sh
4 pipx install uv
5 pip install uv
6 uv self update
7 pip install --upgrade uv
8
9 uv add "fastapi[standard]"
10
11 pip install fastapi[standard]
12 # Virtual Environment: Python's built-in venv module
13 # Dependency Management: requirements.txt or pyproject.toml
14
Run
uv run fastapi dev
pip , venv steps
1 cd ~/Documents
2 mkdir -p fastapi-test
3 cd fastapi-test
4 mkdir static templates
5 python3 -m venv .venv
6 source .venv/bin/activate
7
8 cat << 'EOF' > requirements.txt
9 fastapi[standard]
10 pyyaml
11 jinja2
12
13 EOF
14
15 pip install -r requirements.txt
16
17 cat << 'EOF' > main.py
18 import yaml
19 from fastapi import FastAPI
20 from fastapi.responses import Response
21
22 app = FastAPI()
23
24 @app.get("/")
25 def read_root():
26 return {"message": "Hello world!"}
27
28 @app.get("/openapi.yaml", include_in_schema=False)
29 def get_openapi_yaml():
30 openapi_json = app.openapi()
31 openapi_yaml = yaml.dump(openapi_json, sort_keys=False)
32 return Response(content=openapi_yaml, media_type="text/yaml")
33
34 EOF
35
36 fastapi dev main.py
37 # /home/vitor/Documents/fastapi-test/.venv/bin/python3 \
38 # /home/vitor/Documents/fastapi-test/.venv/bin/fastapi dev main.py
39 # http://127.0.0.1:8000
40 # Server started at http://127.0.0.1:8000
41 # OpenAPI/Swagger documentation at http://127.0.0.1:8000/docs
42 # http://127.0.0.1:8000/openapi.yaml
43 # http://127.0.0.1:8000/openapi.json
44
45 uvicorn main:app --reload
