Skip to content

🥇 🔁

🙅 FastAPI 📁 💪 👀 💖 👉:

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

📁 👈 📁 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.

👆 🔜 👀 🏧 🎓 🛠️ 🧾 (🚚 🦁 🎚):

Swagger UI

🎛 🛠️ 🩺

& 🔜, 🚶 http://127.0.0.1:8000/redoc.

👆 🔜 👀 🎛 🏧 🧾 (🚚 📄):

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

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

FastAPI 🐍 🎓 👈 🚚 🌐 🛠️ 👆 🛠️.

📡 ℹ

FastAPI 🎓 👈 😖 🔗 ⚪️➡️ Starlette.

👆 💪 ⚙️ 🌐 💃 🛠️ ⏮️ FastAPI 💁‍♂️.

🔁 2️⃣: ✍ FastAPI "👐"

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

📥 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)

🚥 👆 ✍ 👆 📱 💖:

from fastapi import FastAPI

my_awesome_api = FastAPI()


@my_awesome_api.get("/")
async def root():
    return {"message": "Hello World"}

& 🚮 ⚫️ 📁 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: ❎ 💽.

, 🗄, 🔠 🇺🇸🔍 👩‍🔬 🤙 "🛠️".

👥 🔜 🤙 👫 "🛠️" 💁‍♂️.

🔬 ➡ 🛠️ 👨‍🎨

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

@app.get("/") 💬 FastAPI 👈 🔢 ▶️️ 🔛 🈚 🚚 📨 👈 🚶:

  • ➡ /
  • ⚙️ get 🛠️

@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("/")).
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

👉 🐍 🔢.

⚫️ 🔜 🤙 FastAPI 🕐❔ ⚫️ 📨 📨 📛 "/" ⚙️ GET 🛠️.

👉 💼, ⚫️ async 🔢.


👆 💪 🔬 ⚫️ 😐 🔢 ↩️ async def:

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def root():
    return {"message": "Hello World"}

Note

🚥 👆 🚫 💭 🔺, ✅ 🔁: "🏃 ❓".

🔁 5️⃣: 📨 🎚

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

👆 💪 📨 dict, list, ⭐ 💲 str, int, ♒️.

👆 💪 📨 Pydantic 🏷 (👆 🔜 👀 🌅 🔃 👈 ⏪).

📤 📚 🎏 🎚 & 🏷 👈 🔜 🔁 🗜 🎻 (🔌 🐜, ♒️). 🔄 ⚙️ 👆 💕 🕐, ⚫️ 🏆 🎲 👈 👫 ⏪ 🐕‍🦺.

🌃

  • 🗄 FastAPI.
  • ✍ app 👐.
  • ✍ ➡ 🛠️ 👨‍🎨 (💖 @app.get("/")).
  • ✍ ➡ 🛠️ 🔢 (💖 def root(): ... 🔛).
  • 🏃 🛠️ 💽 (💖 uvicorn main:app --reload).