|
Size: 828
Comment:
|
Size: 2237
Comment:
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 1: | Line 1: |
| <<TableOfContents(2)>> | |
| Line 6: | Line 7: |
| FastAPI works OK without uv. You can build, install, and run FastAPI projects using traditional tools like pip, venv, and poetry | |
| Line 20: | Line 22: |
pip install fastapi[standard] # Virtual Environment: Python's built-in venv module # Dependency Management: requirements.txt or pyproject.toml |
|
| Line 40: | Line 46: |
== pip , venv steps == {{{#!highlight sh cd ~/Documents mkdir -p fastapi-test cd fastapi-test python3 -m venv .venv source .venv/bin/activate cat << 'EOF' > requirements.txt fastapi[standard] pyyaml jinja2 EOF pip install -r requirements.txt 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 }}} |
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 python3 -m venv .venv
5 source .venv/bin/activate
6
7 cat << 'EOF' > requirements.txt
8 fastapi[standard]
9 pyyaml
10 jinja2
11
12 EOF
13
14 pip install -r requirements.txt
15
16 cat << 'EOF' > main.py
17 import yaml
18 from fastapi import FastAPI
19 from fastapi.responses import Response
20
21 app = FastAPI()
22
23 @app.get("/")
24 def read_root():
25 return {"message": "Hello world!"}
26
27 @app.get("/openapi.yaml", include_in_schema=False)
28 def get_openapi_yaml():
29 openapi_json = app.openapi()
30 openapi_yaml = yaml.dump(openapi_json, sort_keys=False)
31 return Response(content=openapi_yaml, media_type="text/yaml")
32
33 EOF
34
35 fastapi dev main.py
36 # /home/vitor/Documents/fastapi-test/.venv/bin/python3 \
37 # /home/vitor/Documents/fastapi-test/.venv/bin/fastapi dev main.py
38 # http://127.0.0.1:8000
39 # Server started at http://127.0.0.1:8000
40 # OpenAPI/Swagger documentation at http://127.0.0.1:8000/docs
41 # http://127.0.0.1:8000/openapi.yaml
42 # http://127.0.0.1:8000/openapi.json
43
44 uvicorn main:app --reload
