FastAPI¶
FastAPI 🛠️, ↕ 🎭, ⏩ 💡, ⏩ 📟, 🔜 🏭
🧾: https://fastapi.tiangolo.com
ℹ 📟: https://github.com/fastapi/fastapi
FastAPI 🏛, ⏩ (↕-🎭), 🕸 🛠️ 🏗 🛠️ ⏮️ 🐍 3️⃣.8️⃣ ➕ ⚓️ 🔛 🐩 🐍 🆎 🔑.
🔑 ⚒:
- ⏩: 📶 ↕ 🎭, 🔛 🇷🇪 ⏮️ ✳ & 🚶 (👏 💃 & Pydantic). 1️⃣ ⏩ 🐍 🛠️ 💪.
- ⏩ 📟: 📈 🚅 🛠️ ⚒ 🔃 2️⃣0️⃣0️⃣ 💯 3️⃣0️⃣0️⃣ 💯. *
- 👩❤👨 🐛: 📉 🔃 4️⃣0️⃣ 💯 🗿 (👩💻) 📉 ❌. *
- 🏋️: 👑 👨🎨 🐕🦺. 🛠️ 🌐. 🌘 🕰 🛠️.
- ⏩: 🔧 ⏩ ⚙️ & 💡. 🌘 🕰 👂 🩺.
- 📏: 📉 📟 ❎. 💗 ⚒ ⚪️➡️ 🔠 🔢 📄. 👩❤👨 🐛.
- 🏋️: 🤚 🏭-🔜 📟. ⏮️ 🏧 🎓 🧾.
- 🐩-⚓️: ⚓️ 🔛 (& 🍕 🔗 ⏮️) 📂 🐩 🔗: 🗄 (⏪ 💭 🦁) & 🎻 🔗.
* ⚖ ⚓️ 🔛 💯 🔛 🔗 🛠️ 🏉, 🏗 🏭 🈸.
💰¶
🤔¶
"[...] 👤 ⚙️ FastAPI 📚 👫 📆. [...] 👤 🤙 📆 ⚙️ ⚫️ 🌐 👇 🏉 ⚗ 🐕🦺 🤸♂. 👫 💆♂ 🛠️ 🔘 🐚 🖥 🏬 & 📠 🏬."
"👥 🛠️ FastAPI 🗃 🤖 🎂 💽 👈 💪 🔢 🚚 🔮. [👨📛]"
"📺 🙏 📣 📂-ℹ 🚀 👆 ⚔ 🧾 🎶 🛠️: 📨❗ [🏗 ⏮️ FastAPI]"
"👤 🤭 🌕 😄 🔃 FastAPI. ⚫️ 🎊 ❗"
"🤙, ⚫️❔ 👆 ✔️ 🏗 👀 💎 💠 & 🇵🇱. 📚 🌌, ⚫️ ⚫️❔ 👤 💚 🤗 - ⚫️ 🤙 😍 👀 👱 🏗 👈."
"🚥 👆 👀 💡 1️⃣ 🏛 🛠️ 🏗 🎂 🔗, ✅ 👅 FastAPI [...] ⚫️ ⏩, ⏩ ⚙️ & ⏩ 💡 [...]"
"👥 ✔️ 🎛 🤭 FastAPI 👆 🔗 [...] 👤 💭 👆 🔜 💖 ⚫️ [...]"
"🚥 🙆 👀 🏗 🏭 🐍 🛠️, 👤 🔜 🏆 👍 FastAPI. ⚫️ 💎 🏗, 🙅 ⚙️ & 🏆 🛠️, ⚫️ ✔️ ▶️️ 🔑 🦲 👆 🛠️ 🥇 🛠️ 🎛 & 🚘 📚 🏧 & 🐕🦺 ✅ 👆 🕹 🔫 👨💻."
🏎, FastAPI 🇳🇨¶
🚥 👆 🏗 ✳ 📱 ⚙️ 📶 ↩️ 🕸 🛠️, ✅ 👅 🏎.
🏎 FastAPI 🐥 👪. & ⚫️ 🎯 FastAPI 🇳🇨. 👶 👶 👶
📄¶
🐍 3️⃣.7️⃣ ➕
FastAPI 🧍 🔛 ⌚ 🐘:
👷♂¶
$ pip install "fastapi[standard]"
---> 100%
👆 🔜 💪 🔫 💽, 🏭 ✅ Uvicorn ⚖️ Hypercorn.
$ pip install "uvicorn[standard]"
---> 100%
🖼¶
✍ ⚫️¶
- ✍ 📁
main.py
⏮️:
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
:
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}
🗒:
🚥 👆 🚫 💭, ✅ _"🏃 ❓" _ 📄 🔃 async
& await
🩺.
🏃 ⚫️¶
🏃 💽 ⏮️:
$ 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
(🐍 "🕹").app
: 🎚 ✍ 🔘main.py
⏮️ ⏸app = FastAPI()
.--reload
: ⚒ 💽 ⏏ ⏮️ 📟 🔀. 🕴 👉 🛠️.
✅ ⚫️¶
📂 👆 🖥 http://127.0.0.1:8000/items/5?q=somequery.
👆 🔜 👀 🎻 📨:
{"item_id": 5, "q": "somequery"}
👆 ⏪ ✍ 🛠️ 👈:
- 📨 🇺🇸🔍 📨 ➡
/
&/items/{item_id}
. - 👯♂️ ➡ ✊
GET
🛠️ (💭 🇺🇸🔍 👩🔬). - ➡
/items/{item_id}
✔️ ➡ 🔢item_id
👈 🔜int
. - ➡
/items/{item_id}
✔️ 📦str
_🔢 =q
.
🎓 🛠️ 🩺¶
🔜 🚶 http://127.0.0.1:8000/docs.
👆 🔜 👀 🏧 🎓 🛠️ 🧾 (🚚 🦁 🎚):
🎛 🛠️ 🩺¶
& 🔜, 🚶 http://127.0.0.1:8000/redoc.
👆 🔜 👀 🎛 🏧 🧾 (🚚 📄):
🖼 ♻¶
🔜 🔀 📁 main.py
📨 💪 ⚪️➡️ PUT
📨.
📣 💪 ⚙️ 🐩 🐍 🆎, 👏 Pydantic.
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
📋 🔛).
🎓 🛠️ 🩺 ♻¶
🔜 🚶 http://127.0.0.1:8000/docs.
- 🎓 🛠️ 🧾 🔜 🔁 ℹ, 🔌 🆕 💪:
- 🖊 🔛 🔼 "🔄 ⚫️ 👅", ⚫️ ✔ 👆 🥧 🔢 & 🔗 🔗 ⏮️ 🛠️:
- ⤴️ 🖊 🔛 "🛠️" 🔼, 👩💻 🔢 🔜 🔗 ⏮️ 👆 🛠️, 📨 🔢, 🤚 🏁 & 🎦 👫 🔛 🖥:
🎛 🛠️ 🩺 ♻¶
& 🔜, 🚶 http://127.0.0.1:8000/redoc.
- 🎛 🧾 🔜 🎨 🆕 🔢 🔢 & 💪:
🌃¶
📄, 👆 📣 🕐 🆎 🔢, 💪, ♒️. 🔢 🔢.
👆 👈 ⏮️ 🐩 🏛 🐍 🆎.
👆 🚫 ✔️ 💡 🆕 ❕, 👩🔬 ⚖️ 🎓 🎯 🗃, ♒️.
🐩 🐍 3️⃣.7️⃣ ➕.
🖼, int
:
item_id: int
⚖️ 🌖 🏗 Item
🏷:
item: Item
...& ⏮️ 👈 👁 📄 👆 🤚:
- 👨🎨 🐕🦺, 🔌:
- 🛠️.
- 🆎 ✅.
- 🔬 💽:
- 🏧 & 🆑 ❌ 🕐❔ 📊 ❌.
- 🔬 🙇 🐦 🎻 🎚.
- 🛠️ 🔢 💽: 👟 ⚪️➡️ 🕸 🐍 💽 & 🆎. 👂 ⚪️➡️:
- 🎻.
- ➡ 🔢.
- 🔢 🔢.
- 🍪.
- 🎚.
- 📨.
- 📁.
- 🛠️ 🔢 📊: 🗜 ⚪️➡️ 🐍 💽 & 🆎 🕸 💽 (🎻):
- 🗜 🐍 🆎 (
str
,int
,float
,bool
,list
, ♒️). datetime
🎚.UUID
🎚.- 💽 🏷.
- ...& 📚 🌖.
- 🗜 🐍 🆎 (
- 🏧 🎓 🛠️ 🧾, 🔌 2️⃣ 🎛 👩💻 🔢:
- 🦁 🎚.
- 📄.
👟 🔙 ⏮️ 📟 🖼, 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}
, ✍ 💪 🎻:- ✅ 👈 ⚫️ ✔️ ✔ 🔢
name
👈 🔜str
. - ✅ 👈 ⚫️ ✔️ ✔ 🔢
price
👈 ✔️float
. - ✅ 👈 ⚫️ ✔️ 📦 🔢
is_offer
, 👈 🔜bool
, 🚥 🎁. - 🌐 👉 🔜 👷 🙇 🐦 🎻 🎚.
- ✅ 👈 ⚫️ ✔️ ✔ 🔢
- 🗜 ⚪️➡️ & 🎻 🔁.
- 📄 🌐 ⏮️ 🗄, 👈 💪 ⚙️:
- 🎓 🧾 ⚙️.
- 🏧 👩💻 📟 ⚡ ⚙️, 📚 🇪🇸.
- 🚚 2️⃣ 🎓 🧾 🕸 🔢 🔗.
👥 🖌 🧽, ✋️ 👆 ⏪ 🤚 💭 ❔ ⚫️ 🌐 👷.
🔄 🔀 ⏸ ⏮️:
return {"item_name": item.name, "item_id": item_id}
...⚪️➡️:
... "item_name": item.name ...
...:
... "item_price": item.price ...
...& 👀 ❔ 👆 👨🎨 🔜 🚘-🏁 🔢 & 💭 👫 🆎:
🌅 🏁 🖼 🔌 🌅 ⚒, 👀 🔰 - 👩💻 🦮.
🚘 🚨: 🔰 - 👩💻 🦮 🔌:
- 📄 🔢 ⚪️➡️ 🎏 🎏 🥉: 🎚, 🍪, 📨 🏑 & 📁.
- ❔ ⚒ 🔬 ⚛
maximum_length
⚖️regex
. - 📶 🏋️ & ⏩ ⚙️ 🔗 💉 ⚙️.
- 💂♂ & 🤝, ✅ 🐕🦺 Oauth2️⃣ ⏮️ 🥙 🤝 & 🇺🇸🔍 🔰 🔐.
- 🌅 🏧 (✋️ 😨 ⏩) ⚒ 📣 🙇 🐦 🎻 🏷 (👏 Pydantic).
- 🕹 🛠️ ⏮️ 🍓 & 🎏 🗃.
- 📚 ➕ ⚒ (👏 💃):
- ** *️⃣ **
- 📶 ⏩ 💯 ⚓️ 🔛 🇸🇲 &
pytest
- ⚜
- 🍪 🎉
- ...& 🌖.
🎭¶
🔬 🇸🇲 📇 🎦 FastAPI 🈸 🏃♂ 🔽 Uvicorn 1️⃣ ⏩ 🐍 🛠️ 💪, 🕴 🔛 💃 & Uvicorn 👫 (⚙️ 🔘 FastAPI). (*)
🤔 🌖 🔃 ⚫️, 👀 📄 📇.
📦 🔗¶
⚙️ Pydantic:
email-validator
- 📧 🔬.
⚙️ 💃:
httpx
- ✔ 🚥 👆 💚 ⚙️TestClient
.jinja2
- ✔ 🚥 👆 💚 ⚙️ 🔢 📄 📳.python-multipart
- ✔ 🚥 👆 💚 🐕🦺 📨 "✍", ⏮️request.form()
.itsdangerous
- ✔SessionMiddleware
🐕🦺.pyyaml
- ✔ 💃SchemaGenerator
🐕🦺 (👆 🎲 🚫 💪 ⚫️ ⏮️ FastAPI).
⚙️ FastAPI / 💃:
👆 💪 ❎ 🌐 👫 ⏮️ pip install "fastapi[all]"
.
🛂¶
👉 🏗 ® 🔽 ⚖ 🇩🇪 🛂.