|
Size: 3486
Comment:
|
← Revision 19 as of 2026-08-23 16:46:09 ⇥
Size: 3349
Comment:
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 71: | Line 71: |
| <title>{{ titulo }}</title> | <title>{{ title }}</title> |
| Line 74: | Line 74: |
| <h1>{{ mensagem }}</h1> <p>Este é o lado <strong>Web Server</strong> do FastAPI.</p> <p>Experimenta os outros caminhos:</p> |
<h1>{{ message }}</h1> <p><strong>Web Server</strong> side of FastAPI.</p> <p>Try other paths:</p> |
| Line 78: | Line 78: |
| <li>Aceder à API: <a href="/api/v1/items">/api/v1/items</a></li> <li>Ver documentação interativa: <a href="/docs">/docs</a></li> <li>Descarregar Contrato OpenAPI YAML: <a href="/openapi.yaml">/openapi.yaml</a></li> |
<li>Check docs: <a href="/docs">/docs</a></li> <li>OpenAPI YAML contract: <a href="/openapi.yaml">/openapi.yaml</a></li> |
| Line 84: | Line 83: |
| Line 107: | Line 107: |
| "titulo": "FastAPI Tudo-Em-Um", "mensagem": "Bem-vindo ao Servidor Web + API!" |
"title": "FastAPI All-in-one", "message": "Welcome to Web server + API!" |
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
18 cat << 'EOF' > templates/index.html
19 <!DOCTYPE html>
20 <html lang="pt">
21 <head>
22 <meta charset="UTF-8">
23 <title>{{ title }}</title>
24 </head>
25 <body>
26 <h1>{{ message }}</h1>
27 <p><strong>Web Server</strong> side of FastAPI.</p>
28 <p>Try other paths:</p>
29 <ul>
30 <li>Check docs: <a href="/docs">/docs</a></li>
31 <li>OpenAPI YAML contract: <a href="/openapi.yaml">/openapi.yaml</a></li>
32 </ul>
33 </body>
34 </html>
35
36 EOF
37
38
39
40 cat << 'EOF' > main.py
41 import yaml
42 from fastapi import FastAPI, Request
43 from fastapi.responses import HTMLResponse, Response
44 from fastapi.staticfiles import StaticFiles
45 from fastapi.templating import Jinja2Templates
46
47 app = FastAPI()
48
49 app.mount("/static", StaticFiles(directory="static"), name="static")
50 templates = Jinja2Templates(directory="templates")
51
52 @app.get("/", response_class=HTMLResponse, include_in_schema=False)
53 def render_web_page(request: Request):
54 """serves index.html."""
55 return templates.TemplateResponse(
56 request, "index.html",
57 {
58 "request": request,
59 "title": "FastAPI All-in-one",
60 "message": "Welcome to Web server + API!"
61 }
62 )
63
64 @app.get("/message")
65 def read_root():
66 return {"message": "Hello world!"}
67
68 @app.get("/openapi.yaml", include_in_schema=False)
69 def get_openapi_yaml():
70 openapi_json = app.openapi()
71 openapi_yaml = yaml.dump(openapi_json, sort_keys=False)
72 return Response(content=openapi_yaml, media_type="text/yaml")
73
74 EOF
75
76 fastapi dev main.py
77 # /home/vitor/Documents/fastapi-test/.venv/bin/python3 \
78 # /home/vitor/Documents/fastapi-test/.venv/bin/fastapi dev main.py
79 # http://127.0.0.1:8000
80 # Server started at http://127.0.0.1:8000
81 # OpenAPI/Swagger documentation at http://127.0.0.1:8000/docs
82 # http://127.0.0.1:8000/openapi.yaml
83 # http://127.0.0.1:8000/openapi.json
84
85 uvicorn main:app --reload
