FastAPI
🔜--🛠 ↕-🎭 🛠, ⏩ 👨🏫 & 🛠.
📄: https://fastapi.tiangolo.com
ℹ 📟: https://github.com/tiangolo/fastapi
FastAPI — 👉 🏛, ⏩ (↕-🎭) 🕸 🛠 🏗 API ⚙️ Python 3.6+, ❔ ⚓️ 🔛 🐩 🆎 ✍ Python.
🔑 👨👩👦:
- 🚅: 📶 ↕ 🎭, 🎚 NodeJS & Go (👏 Starlette и Pydantic). 1⃣ ⏩ 🛠 Python.
- 🛠 🚅: 📈 🛠 🚅 🔃 2⃣0⃣0⃣–300%. *
- 👩❤👨 ❌: ❎ 🔃 4⃣0⃣% 🔢 ❌ 💬 👨💼 (👨🔬). *
- 🏋️: 👍 👨🎨 🏥. 🚙 🛠 🌐. 🌘 🛠 🕰.
- 🔥: 🔧 ⏩ ⚙️ & ☠️. 🌘 🕰 📘 📄.
- 🍶: ❎ 📟 🔁. 💞 📣 🔢 🆔 📚 🔢. 👩❤👨 ❌.
- ⚖: 🤚 📟 🔜 👷. ⏮ 🏧 🏧 📄.
- 🐩-⚓️: ⚓️ 🔛 🈺 🐩 API & 🍕 🔗 ⏮ 👫: OpenAPI (💭 💭 Swagger) и JSON Schema.
* 🔬 ⚓️ 🔛 🔬 🔗 🏉 👨🔬 🏗 🏭 🈸.
💰
💞 💰
📓
"👤 ✔️ ⚙️ 📚 👂 ⏳ FastAPI. [...] 👐, 👤 📆 ⚙️ 👉 🌐 🎰 🎓 🛃 👇 🏉 Microsoft. 👫 🛠 🔘 👑 🏷 Windows, & — 🏷 Office."
Kabir Khan -
Microsoft (ref)
"👥 ⚙️ 🗃 FastAPI 🏗 💽 REST, ❔ 👆 💪 ⚒ ✔ 🤚 🌦. [〰 Ludwig]"
Piero Molino, Yaroslav Dudin, and Sai Sumanth Miryala -
Uber (ref)
"Netflix 👤 🙏 📣 🚀 🈺 ℹ 🛠 🎶 ☣ 🛠: Dispatch! [🏗 ⏮ FastAPI]"
Kevin Glisson, Marc Vilanova, Forest Monsen -
Netflix (ref)
"👤 ⚡️ 😄 ⏮ FastAPI. 👉 💗 🎊 ❗"
"💛, ⚫️❔ 👆 ✔️ 🏗 👀 📶 🌝 & 🇵🇱. 📚 🌌, 👤 💚 Hug 💖 👈 — 👉 🤙 😍 🕐❔ 🕴 🏗 😍 💖 👉."
"🚥 👆 💚 👨🏫 🏛 🛠 🏗 REST API, ✅ 😱 FastAPI [...] 👉 ⏩, ⏩ & ⏩ 👨🏫 [...]"
"👥 🔀 FastAPI 👨👩👦 API [...] 👤 💭 👆 🔜 💖 👉 💁♂️ [...]"
Typer, 🕹 👯 🔣 FastAPI
🚥 👆 🏗 🈸 CLI ⚙️ 🗼 ⏭ 🕸-API, ✅ 😱 Typer.
Typer — 👦 👬 FastAPI. & 👉 🎯 ⚙️ 🕹 👯 🔣 FastAPI. ⌨️ 🚀
👨📛
Python 3.6+
FastAPI 👥 🔛 ⌚ 🐙:
♻
$ pip install fastapi
---> 100%
👆 🔜 🆘 💽 ASGI 🏭, ✅ Uvicorn ⚖️ Hypercorn.
$ pip install "uvicorn[standard]"
---> 100%
📊
🛠
from typing import Union
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: Union[str, None] = None):
return {"item_id": item_id, "q": q}
⚖️ ⚙️ async def
...
🚥 👆 📟 ⚙️ `async` / `await`, ⚙️ `async def`:
```Python hl_lines="9⃣ 14"
from typing import Union
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
async def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
**🗒**:
🚥 👆 🚫 💭, ✅ 📄 _"👆 🏃 ❓"_ <a href="https://fastapi.tiangolo.com/async/#in-a-hurry" target="_blank">📄 🔃 `async` и `await`</a>.
</details>
### 🚀
▶ 💽 ⚙️:
<div class="termy">
```console
$ uvicorn main:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [28720]
INFO: Started server process [28722]
INFO: Waiting for application startup.
INFO: Application startup complete.
🔃 🏉 uvicorn main:app --reload
...
🏉 `uvicorn main:app` 🗨 :
* `main`: 📂 `main.py` (🕹 Python).
* `app`: 🎚 🏗 🔘 `main.py` ⚙️ 🎻 `app = FastAPI()`.
* `--reload`: 🔁 💽 ⏮ 🔀 📟. 👉 🕴 ↗️ 🛠.
### ✅
🈺 🖥 🔛
http://127.0.0.1:8000/items/5?q=somequery.
👆 🔜 👀 📄 JSON ❔:
{"item_id": 5⃣, "q": "somequery"}
👆 ✔️ ⏪ 🏗 API, ❔:
* 🤚 HTTP-✔ _↖_ `/` & `/items/{item_id}`.
* 👯♂️ 🥇 & 🥈 _🌌_ ⚙️ `GET`
🛠 (💭 HTTP _👩🔬_).
* _🌌_ `/items/{item_id}` ✔️ _↖ 🔢_ `item_id`, ❔ 🔜 `int`.
* _🌌_ `/items/{item_id}` ✔️ ➕ `str` _✔ 🔢_ `q`.
### 🏧 📄 🔛 API
🚶
http://127.0.0.1:8000/docs.
👆 🔜 👀 🏧 🏧 📄 API (🚚
Swagger UI):
![Swagger UI](https://fastapi.tiangolo.com/img/index/index-01-swagger-ui-simple.png)
### 🉑 📄 API
& 😀 🚶
http://127.0.0.1:8000/redoc.
👆 🔜 👀 🉑 🏧 📄 (🚚
ReDoc):
![ReDoc](https://fastapi.tiangolo.com/img/index/index-02-redoc-simple.png)
## 📊 🔧
😀 🔀 📂 `main.py`, 🤚 🆘 💪 ⚪️➡️ `PUT` ✔.
📣 💪 ⚙️ 🐩 ⌨ Python, 👏 Pydantic.
```Python hl_lines="4⃣ 9-12 25-27"
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
is_offer: Union[bool, None] = None
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
return {"item_name": item.name, "item_id": item_id}
💽 🔜 💾 🔁 (↩️ 👆 ➕ `--reload` 🏉 `uvicorn` 🔝).
### 🏧 📄 🔧 API
🚶 <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>.
* 🏧 📄 API 🔜 🔁 🔧, 🔌 🆕 💪:
![Swagger UI](https://fastapi.tiangolo.com/img/index/index-03-swagger-02.png)
* 🖊 🔛 "Try it out", 👉 🔜 🚫 👆 🍷 🔢 & 🔗 🔙 ⏮ API:
![Swagger UI interaction](https://fastapi.tiangolo.com/img/index/index-04-swagger-03.png)
* ⤴ 🖊 "Execute", 👩💻 🔣 🔜 📧 👆 API, 🔜 📨 🔢, 🤚 🏁 & 🖥 👫 🔛 🖥:
![Swagger UI interaction](https://fastapi.tiangolo.com/img/index/index-05-swagger-04.png)
### 🉑 📄 🔧 API
& 😀 🚶 <a href="http://127.0.0.1:8000/redoc" class="external-link" target="_blank">http://127.0.0.1:8000/redoc</a>.
* 🉑 📄 🔜 🤔 🆕 🔢 & ✔ 💪.:
![ReDoc](https://fastapi.tiangolo.com/img/index/index-06-redoc-02.png)
### ➡️ 🔬 🏁
👆 📣 **🕐** 🔢 🆎, 💪, ♒️. 🔢 🔢.
👆 👉 ⚙️ 🐩 🏛 ⌨ Python.
👆 🚫 🆘 👨🏫 🆕 ⁉, 👩🔬 ⚖️ 🏫 🎯 🗃, ♒️.
🐩 🕴 **Python 3.6+**.
📊, `int`:
```Python
item_id: int
⚖️ 💗 🏗 👙 `Item`:
... & ⏮ 👉 👁 🙂📛 👆 🤚:
* 👨🎨 🏥, 🔌:
* 🚙 🛠.
* 🆎 ☑.
* 💽 🛠:
* 🏧 & 🆑 ❌ 🕐❔ 📐 ❌.
* ☑ 🙇 🔁 🎚 JSON.
*
🌐 🔣 💽: 👟 ⚪️➡️ 🕸 🎚 Python ⏮ 🍾 🆎. 📙 ⚪️➡️:
* JSON.
* ↖ 🔢.
* ✔ 🔢.
* Cookies.
* 📰.
* 📄.
* 📂.
*
🌐 🔣 📐: 🎚 🌐 Python 📐 🚥 🌇 🕸 (✅ JSON):
* 🆎 🛠 Python (`str`, `int`, `float`, `bool`, `list`, ♒️.).
* 🎚 `datetime`.
* 🎚 `UUID`.
* 💽 👙.
* ...& 💗 🌖.
* 🏧 🏧 📄 🔛 API, ✅ 2⃣ 🉑 👩💻 🔣:
* Swagger UI.
* ReDoc.
---
🤕 🔙 ⌛️ 📟 📊, **FastAPI** 🔜 :
* ✅ 🚚 `item_id` 🌌 ✔ `GET` и `PUT`.
* ✅ 👈 `item_id` ✔️ 🆎 `int` ✔ `GET` и `PUT`.
* 🚥 👉 🚫 💼, 👩💻 🔜 👀 ⚠ 🆑 ❌.
* ✅ 🚥 📤 ➕ ✔ 🔢 ⏮ 📛 `q` (📊, `http://127.0.0.1:8000/items/foo?q=somequery`) 〰 `GET` ✔.
* ↩️ 🔢 `q` 📣 ⏮ `= None`, 👉 ➕.
* 🚫 `None` 👉 🔜 💪 (💪 💼 `PUT`).
* 〰 `PUT` ✔ `/items/{item_id}` 📘 💪 JSON:
* ✅ 👈 👉 ✔️ 🚫 🔢 `name`, ❔ 🔜 `str`.
* ✅ 👈 👉 ✔️ 🚫 🔢 `price`, ❔ 🔜 `float`.
* ✅ 👈 👉 ✔️ ➕ 🔢 `is_offer`, ❔ 🔜 `bool`, 🚥 👉 🎁.
* 🌐 👉 🔜 👷 🙇 🐦 🎚. JSON.
* 🗜 ⚪️➡️ & JSON 🔁.
* 📄 ⏮ OpenAPI 🌐 👈 💪 ⚙️:
* 🏧 📄 ⚙️.
* 🏧 👩💻 📟 ⚡ ⚙️ 📚 🇬🇧.
* 🔜 🚚 2⃣ 🏧 📄 🕸 🔣 🔙.
---
👥 ✔️ 🕴 🕳 ⤵ 🍖, ✋️ 👆 ✔️ ⏪ 💭 😱 ❔ 👉 🌐 👷.
🔄 🔀 🎻 ⏮:
return {"item_name": item.name, "item_id": item_id}
...⚪️➡️:
... "item_name": item.name ...
...🔘:
... "item_price": item.price ...
... & 👀 ❔ 👆 👨🎨 🔜 🔁 🍷 🔢 & 🤔 👫 🆎:
![editor support](https://fastapi.tiangolo.com/img/vscode-completion.png)
💗 🏁 📊 ⏮ ➕ 🔢, 👀
🔰 - 👩💻 👩🌾.
**🚘 🚨**: 👩💻 👩🌾 🔌:
* 📣 **🔢** ⚪️➡️ 💞 🥉, ✅: **🎚**, **cookies**, **📄 🏑** & **📂**.
* ❔ 🔆 **⚠ ✅** ✅ `maximum_length` ⚖️ `regex`.
* 📶 🏋️ & ⏩--⚙️ ⚙️ **
👨📛 🛠**.
* 💂♂ & 🤝, 🔌 🏥 **OAuth2** ⏮ **🔑 JWT** & **HTTP Basic** 🤝.
* 💗 🏧 (✋️ 🙃 👌) 👩🔬 📣 **🙇 🐦 👙 JSON** (👏 Pydantic).
* **GraphQL** 🛠 ⏮
Strawberry & 💞 🗃.
* 📚 ➕ 👨👩👦 (👏 Starlette), ✅ :
* **🕸 🔌**
* 📶 👌 🔬 ⚓️ 🔛 `requests` и `pytest`
* **CORS**
* **Cookie 💬 (💬)**
* ...& 💗 🌖.
## 🔆
⚖ 🔬 TechEmpower 🕢 🈸 **FastAPI**, 👷 🌂 🛠 Uvicorn, ❔
1⃣ ⏩ 🛠 💪 Python, 🥈 🕴 👫 Starlette и Uvicorn (⚙️ 🔘 FastAPI). (*)
👨🏫 💗 🔃 👉, 👀
🎭 🔬.
## ➕ 👨📛
⚙️ Pydantic:
*
ujson
- ⏩ JSON
"🔢".
*
email_validator
- ✅ 👆 📧.
⚙️ Starlette:
*
requests
- ✔ 🚥 👆 💚 ⚙️ `TestClient`.
*
jinja2
- ✔ 🚥 👆 💚 ⚙️ 🔢 📄 🔗.
*
python-multipart
- 🔜 🚥 👆 💚 🚧 👔
"🔢" ⏮ 🆘 `request.form()`.
*
itsdangerous
- ✔, 🏥 `SessionMiddleware`.
*
pyyaml
- ✔, 🏥 `SchemaGenerator` Starlette (🎲 👆 🚫 🆘 👉 ⏮ FastAPI).
*
ujson
- ✔ 🚥 👆 💚 ⚙️ `UJSONResponse`.
⚙️ FastAPI / Starlette:
*
uvicorn
- 💽 👈 ⏬ & 🚧 👆 🈸.
*
orjson
- ✔ 🚥 👆 💚 ⚙️ `ORJSONResponse`.
👆 💪 🔧 🌐 👉 ⏮ `pip install "fastapi[all]"`.
## 🛂
👉 🏗 🚮 🌂 ⚖ 🛂 MIT.