8.1 KiB
🥇 🔁
🙅 FastAPI 📁 💪 👀 💖 👉:
{!../../../docs_src/first_steps/tutorial001.py!}
📁 👈 📁 main.py
.
🏃 🖖 💽:
$ uvicorn main:app --reload
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
<span style="color: green;">INFO</span>: Started reloader process [28720]
<span style="color: green;">INFO</span>: Started server process [28722]
<span style="color: green;">INFO</span>: Waiting for application startup.
<span style="color: green;">INFO</span>: Application startup complete.
!!! note
📋 uvicorn main:app
🔗:
* `main`: 📁 `main.py` (🐍 "🕹").
* `app`: 🎚 ✍ 🔘 `main.py` ⏮️ ⏸ `app = FastAPI()`.
* `--reload`: ⚒ 💽 ⏏ ⏮️ 📟 🔀. 🕴 ⚙️ 🛠️.
🔢, 📤 ⏸ ⏮️ 🕳 💖:
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
👈 ⏸ 🎦 📛 🌐❔ 👆 📱 ➖ 🍦, 👆 🇧🇿 🎰.
✅ ⚫️
📂 👆 🖥 http://127.0.0.1:8000.
👆 🔜 👀 🎻 📨:
{"message": "Hello World"}
🎓 🛠️ 🩺
🔜 🚶 http://127.0.0.1:8000/docs.
👆 🔜 👀 🏧 🎓 🛠️ 🧾 (🚚 🦁 🎚):
🎛 🛠️ 🩺
& 🔜, 🚶 http://127.0.0.1:8000/redoc.
👆 🔜 👀 🎛 🏧 🧾 (🚚 📄):
🗄
FastAPI 🏗 "🔗" ⏮️ 🌐 👆 🛠️ ⚙️ 🗄 🐩 ⚖ 🔗.
"🔗"
"🔗" 🔑 ⚖️ 📛 🕳. 🚫 📟 👈 🛠️ ⚫️, ✋️ 📝 📛.
🛠️ "🔗"
👉 💼, 🗄 🔧 👈 🤔 ❔ 🔬 🔗 👆 🛠️.
👉 🔗 🔑 🔌 👆 🛠️ ➡, 💪 🔢 👫 ✊, ♒️.
💽 "🔗"
⚖ "🔗" 💪 🔗 💠 💽, 💖 🎻 🎚.
👈 💼, ⚫️ 🔜 ⛓ 🎻 🔢, & 📊 🆎 👫 ✔️, ♒️.
🗄 & 🎻 🔗
🗄 🔬 🛠️ 🔗 👆 🛠️. & 👈 🔗 🔌 🔑 (⚖️ "🔗") 📊 📨 & 📨 👆 🛠️ ⚙️ 🎻 🔗, 🐩 🎻 📊 🔗.
✅ openapi.json
🚥 👆 😟 🔃 ❔ 🍣 🗄 🔗 👀 💖, FastAPI 🔁 🏗 🎻 (🔗) ⏮️ 📛 🌐 👆 🛠️.
👆 💪 👀 ⚫️ 🔗: http://127.0.0.1:8000/openapi.json.
⚫️ 🔜 🎦 🎻 ▶️ ⏮️ 🕳 💖:
{
"openapi": "3.0.2",
"info": {
"title": "FastAPI",
"version": "0.1.0"
},
"paths": {
"/items/": {
"get": {
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
...
⚫️❔ 🗄
🗄 🔗 ⚫️❔ 🏋️ 2️⃣ 🎓 🧾 ⚙️ 🔌.
& 📤 💯 🎛, 🌐 ⚓️ 🔛 🗄. 👆 💪 💪 🚮 🙆 📚 🎛 👆 🈸 🏗 ⏮️ FastAPI.
👆 💪 ⚙️ ⚫️ 🏗 📟 🔁, 👩💻 👈 🔗 ⏮️ 👆 🛠️. 🖼, 🕸, 📱 ⚖️ ☁ 🈸.
🌃, 🔁 🔁
🔁 1️⃣: 🗄 FastAPI
{!../../../docs_src/first_steps/tutorial001.py!}
FastAPI
🐍 🎓 👈 🚚 🌐 🛠️ 👆 🛠️.
!!! note "📡 ℹ"
FastAPI
🎓 👈 😖 🔗 ⚪️➡️ Starlette
.
👆 💪 ⚙️ 🌐 <a href="https://www.starlette.io/" class="external-link" target="_blank">💃</a> 🛠️ ⏮️ `FastAPI` 💁♂️.
🔁 2️⃣: ✍ FastAPI
"👐"
{!../../../docs_src/first_steps/tutorial001.py!}
📥 app
🔢 🔜 "👐" 🎓 FastAPI
.
👉 🔜 👑 ☝ 🔗 ✍ 🌐 👆 🛠️.
👉 app
🎏 1️⃣ 🔗 uvicorn
📋:
$ uvicorn main:app --reload
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
🚥 👆 ✍ 👆 📱 💖:
{!../../../docs_src/first_steps/tutorial002.py!}
& 🚮 ⚫️ 📁 main.py
, ⤴️ 👆 🔜 🤙 uvicorn
💖:
$ uvicorn main:my_awesome_api --reload
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
🔁 3️⃣: ✍ ➡ 🛠️
➡
"➡" 📥 🔗 🏁 🍕 📛 ▶️ ⚪️➡️ 🥇 /
.
, 📛 💖:
https://example.com/items/foo
...➡ 🔜:
/items/foo
!!! info "➡" 🛎 🤙 "🔗" ⚖️ "🛣".
⏪ 🏗 🛠️, "➡" 👑 🌌 🎏 "⚠" & "ℹ".
🛠️
"🛠️" 📥 🔗 1️⃣ 🇺🇸🔍 "👩🔬".
1️⃣:
POST
GET
PUT
DELETE
...& 🌅 😍 🕐:
OPTIONS
HEAD
PATCH
TRACE
🇺🇸🔍 🛠️, 👆 💪 🔗 🔠 ➡ ⚙️ 1️⃣ (⚖️ 🌅) 👫 "👩🔬".
🕐❔ 🏗 🔗, 👆 🛎 ⚙️ 👫 🎯 🇺🇸🔍 👩🔬 🎭 🎯 🎯.
🛎 👆 ⚙️:
POST
: ✍ 💽.GET
: ✍ 💽.PUT
: ℹ 💽.DELETE
: ❎ 💽.
, 🗄, 🔠 🇺🇸🔍 👩🔬 🤙 "🛠️".
👥 🔜 🤙 👫 "🛠️" 💁♂️.
🔬 ➡ 🛠️ 👨🎨
{!../../../docs_src/first_steps/tutorial001.py!}
@app.get("/")
💬 FastAPI 👈 🔢 ▶️️ 🔛 🈚 🚚 📨 👈 🚶:
- ➡
/
- ⚙️
get
🛠️
!!! info "@decorator
ℹ"
👈 @something
❕ 🐍 🤙 "👨🎨".
👆 🚮 ⚫️ 🔛 🔝 🔢. 💖 📶 📔 👒 (👤 💭 👈 🌐❔ ⚖ 👟 ⚪️➡️).
"👨🎨" ✊ 🔢 🔛 & 🔨 🕳 ⏮️ ⚫️.
👆 💼, 👉 👨🎨 💬 **FastAPI** 👈 🔢 🔛 🔗 **➡** `/` ⏮️ **🛠️** `get`.
⚫️ "**➡ 🛠️ 👨🎨**".
👆 💪 ⚙️ 🎏 🛠️:
@app.post()
@app.put()
@app.delete()
& 🌅 😍 🕐:
@app.options()
@app.head()
@app.patch()
@app.trace()
!!! tip 👆 🆓 ⚙️ 🔠 🛠️ (🇺🇸🔍 👩🔬) 👆 🎋.
**FastAPI** 🚫 🛠️ 🙆 🎯 🔑.
ℹ 📥 🎁 📄, 🚫 📄.
🖼, 🕐❔ ⚙️ 🕹 👆 🛎 🎭 🌐 🎯 ⚙️ 🕴 `POST` 🛠️.
🔁 4️⃣: 🔬 ➡ 🛠️ 🔢
👉 👆 "➡ 🛠️ 🔢":
- ➡:
/
. - 🛠️:
get
. - 🔢: 🔢 🔛 "👨🎨" (🔛
@app.get("/")
).
{!../../../docs_src/first_steps/tutorial001.py!}
👉 🐍 🔢.
⚫️ 🔜 🤙 FastAPI 🕐❔ ⚫️ 📨 📨 📛 "/
" ⚙️ GET
🛠️.
👉 💼, ⚫️ async
🔢.
👆 💪 🔬 ⚫️ 😐 🔢 ↩️ async def
:
{!../../../docs_src/first_steps/tutorial003.py!}
!!! note 🚥 👆 🚫 💭 🔺, ✅ 🔁: "🏃 ❓"{.internal-link target=_blank}.
🔁 5️⃣: 📨 🎚
{!../../../docs_src/first_steps/tutorial001.py!}
👆 💪 📨 dict
, list
, ⭐ 💲 str
, int
, ♒️.
👆 💪 📨 Pydantic 🏷 (👆 🔜 👀 🌅 🔃 👈 ⏪).
📤 📚 🎏 🎚 & 🏷 👈 🔜 🔁 🗜 🎻 (🔌 🐜, ♒️). 🔄 ⚙️ 👆 💕 🕐, ⚫️ 🏆 🎲 👈 👫 ⏪ 🐕🦺.
🌃
- 🗄
FastAPI
. - ✍
app
👐. - ✍ ➡ 🛠️ 👨🎨 (💖
@app.get("/")
). - ✍ ➡ 🛠️ 🔢 (💖
def root(): ...
🔛). - 🏃 🛠️ 💽 (💖
uvicorn main:app --reload
).