Skip to content

➡ 🔢

👆 💪 📣 ➡ "🔢" ⚖️ "🔢" ⏮️ 🎏 ❕ ⚙️ 🐍 📁 🎻:

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/{item_id}")
async def read_item(item_id):
    return {"item_id": item_id}

💲 ➡ 🔢 item_id 🔜 🚶‍♀️ 👆 🔢 ❌ item_id.

, 🚥 👆 🏃 👉 🖼 & 🚶 http://127.0.0.1:8000/items/foo, 👆 🔜 👀 📨:

{"item_id":"foo"}

➡ 🔢 ⏮️ 🆎

👆 💪 📣 🆎 ➡ 🔢 🔢, ⚙️ 🐩 🐍 🆎 ✍:

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/{item_id}")
async def read_item(item_id: int):
    return {"item_id": item_id}

👉 💼, item_id 📣 int.

Check

👉 🔜 🤝 👆 👨‍🎨 🐕‍🦺 🔘 👆 🔢, ⏮️ ❌ ✅, 🛠️, ♒️.

💽 🛠️

🚥 👆 🏃 👉 🖼 & 📂 👆 🖥 http://127.0.0.1:8000/items/3, 👆 🔜 👀 📨:

{"item_id":3}

Check

👀 👈 💲 👆 🔢 📨 (& 📨) 3, 🐍 int, 🚫 🎻 "3".

, ⏮️ 👈 🆎 📄, FastAPI 🤝 👆 🏧 📨 "✍".

💽 🔬

✋️ 🚥 👆 🚶 🖥 http://127.0.0.1:8000/items/foo, 👆 🔜 👀 👌 🇺🇸🔍 ❌:

{
    "detail": [
        {
            "loc": [
                "path",
                "item_id"
            ],
            "msg": "value is not a valid integer",
            "type": "type_error.integer"
        }
    ]
}

↩️ ➡ 🔢 item_id ✔️ 💲 "foo", ❔ 🚫 int.

🎏 ❌ 🔜 😑 🚥 👆 🚚 float ↩️ int,: http://127.0.0.1:8000/items/4.2

Check

, ⏮️ 🎏 🐍 🆎 📄, FastAPI 🤝 👆 💽 🔬.

👀 👈 ❌ 🎯 🇵🇸 ⚫️❔ ☝ 🌐❔ 🔬 🚫 🚶‍♀️.

👉 🙃 👍 ⏪ 🛠️ & 🛠️ 📟 👈 🔗 ⏮️ 👆 🛠️.

🧾

& 🕐❔ 👆 📂 👆 🖥 http://127.0.0.1:8000/docs, 👆 🔜 👀 🏧, 🎓, 🛠️ 🧾 💖:

Check

🔄, ⏮️ 👈 🎏 🐍 🆎 📄, FastAPI 🤝 👆 🏧, 🎓 🧾 (🛠️ 🦁 🎚).

👀 👈 ➡ 🔢 📣 🔢.

🐩-⚓️ 💰, 🎛 🧾

& ↩️ 🏗 🔗 ⚪️➡️ 🗄 🐩, 📤 📚 🔗 🧰.

↩️ 👉, FastAPI ⚫️ 🚚 🎛 🛠️ 🧾 (⚙️ 📄), ❔ 👆 💪 🔐 http://127.0.0.1:8000/redoc:

🎏 🌌, 📤 📚 🔗 🧰. ✅ 📟 ⚡ 🧰 📚 🇪🇸.

Pydantic

🌐 💽 🔬 🎭 🔽 🚘 Pydantic, 👆 🤚 🌐 💰 ⚪️➡️ ⚫️. & 👆 💭 👆 👍 ✋.

👆 💪 ⚙️ 🎏 🆎 📄 ⏮️ str, float, bool & 📚 🎏 🏗 📊 🆎.

📚 👫 🔬 ⏭ 📃 🔰.

✔ 🤔

🕐❔ 🏗 ➡ 🛠️, 👆 💪 🔎 ⚠ 🌐❔ 👆 ✔️ 🔧 ➡.

💖 /users/me, ➡️ 💬 👈 ⚫️ 🤚 📊 🔃 ⏮️ 👩‍💻.

& ⤴️ 👆 💪 ✔️ ➡ /users/{user_id} 🤚 💽 🔃 🎯 👩‍💻 👩‍💻 🆔.

↩️ ➡ 🛠️ 🔬 ✔, 👆 💪 ⚒ 💭 👈 ➡ /users/me 📣 ⏭ 1️⃣ /users/{user_id}:

from fastapi import FastAPI

app = FastAPI()


@app.get("/users/me")
async def read_user_me():
    return {"user_id": "the current user"}


@app.get("/users/{user_id}")
async def read_user(user_id: str):
    return {"user_id": user_id}

⏪, ➡ /users/{user_id} 🔜 🏏 /users/me, "💭" 👈 ⚫️ 📨 🔢 user_id ⏮️ 💲 "me".

➡, 👆 🚫🔜 ↔ ➡ 🛠️:

from fastapi import FastAPI

app = FastAPI()


@app.get("/users")
async def read_users():
    return ["Rick", "Morty"]


@app.get("/users")
async def read_users2():
    return ["Bean", "Elfo"]

🥇 🕐 🔜 🕧 ⚙️ ↩️ ➡ 🏏 🥇.

🔁 💲

🚥 👆 ✔️ ➡ 🛠️ 👈 📨 ➡ 🔢, ✋️ 👆 💚 💪 ☑ ➡ 🔢 💲 🔁, 👆 💪 ⚙️ 🐩 🐍 Enum.

Enum 🎓

🗄 Enum & ✍ 🎧-🎓 👈 😖 ⚪️➡️ str & ⚪️➡️ Enum.

😖 ⚪️➡️ str 🛠️ 🩺 🔜 💪 💭 👈 💲 🔜 🆎 string & 🔜 💪 ✍ ☑.

⤴️ ✍ 🎓 🔢 ⏮️ 🔧 💲, ❔ 🔜 💪 ☑ 💲:

from enum import Enum

from fastapi import FastAPI


class ModelName(str, Enum):
    alexnet = "alexnet"
    resnet = "resnet"
    lenet = "lenet"


app = FastAPI()


@app.get("/models/{model_name}")
async def get_model(model_name: ModelName):
    if model_name is ModelName.alexnet:
        return {"model_name": model_name, "message": "Deep Learning FTW!"}

    if model_name.value == "lenet":
        return {"model_name": model_name, "message": "LeCNN all the images"}

    return {"model_name": model_name, "message": "Have some residuals"}

Info

🔢 (⚖️ 🔢) 💪 🐍 ↩️ ⏬ 3️⃣.4️⃣.

Tip

🚥 👆 💭, "📊", "🎓", & "🍏" 📛 🎰 🏫 🏷.

📣 ➡ 🔢

⤴️ ✍ ➡ 🔢 ⏮️ 🆎 ✍ ⚙️ 🔢 🎓 👆 ✍ (ModelName):

from enum import Enum

from fastapi import FastAPI


class ModelName(str, Enum):
    alexnet = "alexnet"
    resnet = "resnet"
    lenet = "lenet"


app = FastAPI()


@app.get("/models/{model_name}")
async def get_model(model_name: ModelName):
    if model_name is ModelName.alexnet:
        return {"model_name": model_name, "message": "Deep Learning FTW!"}

    if model_name.value == "lenet":
        return {"model_name": model_name, "message": "LeCNN all the images"}

    return {"model_name": model_name, "message": "Have some residuals"}

✅ 🩺

↩️ 💪 💲 ➡ 🔢 🔢, 🎓 🩺 💪 🎦 👫 🎆:

👷 ⏮️ 🐍 🔢

💲 ➡ 🔢 🔜 🔢 👨‍🎓.

🔬 🔢 👨‍🎓

👆 💪 🔬 ⚫️ ⏮️ 🔢 👨‍🎓 👆 ✍ 🔢 ModelName:

from enum import Enum

from fastapi import FastAPI


class ModelName(str, Enum):
    alexnet = "alexnet"
    resnet = "resnet"
    lenet = "lenet"


app = FastAPI()


@app.get("/models/{model_name}")
async def get_model(model_name: ModelName):
    if model_name is ModelName.alexnet:
        return {"model_name": model_name, "message": "Deep Learning FTW!"}

    if model_name.value == "lenet":
        return {"model_name": model_name, "message": "LeCNN all the images"}

    return {"model_name": model_name, "message": "Have some residuals"}

🤚 🔢 💲

👆 💪 🤚 ☑ 💲 ( str 👉 💼) ⚙️ model_name.value, ⚖️ 🏢, your_enum_member.value:

from enum import Enum

from fastapi import FastAPI


class ModelName(str, Enum):
    alexnet = "alexnet"
    resnet = "resnet"
    lenet = "lenet"


app = FastAPI()


@app.get("/models/{model_name}")
async def get_model(model_name: ModelName):
    if model_name is ModelName.alexnet:
        return {"model_name": model_name, "message": "Deep Learning FTW!"}

    if model_name.value == "lenet":
        return {"model_name": model_name, "message": "LeCNN all the images"}

    return {"model_name": model_name, "message": "Have some residuals"}

Tip

👆 💪 🔐 💲 "lenet" ⏮️ ModelName.lenet.value.

📨 🔢 👨‍🎓

👆 💪 📨 🔢 👨‍🎓 ⚪️➡️ 👆 ➡ 🛠️, 🐦 🎻 💪 (✅ dict).

👫 🔜 🗜 👫 🔗 💲 (🎻 👉 💼) ⏭ 🛬 👫 👩‍💻:

from enum import Enum

from fastapi import FastAPI


class ModelName(str, Enum):
    alexnet = "alexnet"
    resnet = "resnet"
    lenet = "lenet"


app = FastAPI()


@app.get("/models/{model_name}")
async def get_model(model_name: ModelName):
    if model_name is ModelName.alexnet:
        return {"model_name": model_name, "message": "Deep Learning FTW!"}

    if model_name.value == "lenet":
        return {"model_name": model_name, "message": "LeCNN all the images"}

    return {"model_name": model_name, "message": "Have some residuals"}

👆 👩‍💻 👆 🔜 🤚 🎻 📨 💖:

{
  "model_name": "alexnet",
  "message": "Deep Learning FTW!"
}

➡ 🔢 ⚗ ➡

➡️ 💬 👆 ✔️ ➡ 🛠️ ⏮️ ➡ /files/{file_path}.

✋️ 👆 💪 file_path ⚫️ 🔌 , 💖 home/johndoe/myfile.txt.

, 📛 👈 📁 🔜 🕳 💖: /files/home/johndoe/myfile.txt.

🗄 🐕‍🦺

🗄 🚫 🐕‍🦺 🌌 📣 ➡ 🔢 🔌 🔘, 👈 💪 ↘️ 😐 👈 ⚠ 💯 & 🔬.

👐, 👆 💪 ⚫️ FastAPI, ⚙️ 1️⃣ 🔗 🧰 ⚪️➡️ 💃.

& 🩺 🔜 👷, 👐 🚫 ❎ 🙆 🧾 💬 👈 🔢 🔜 🔌 ➡.

➡ 🔌

⚙️ 🎛 🔗 ⚪️➡️ 💃 👆 💪 📣 ➡ 🔢 ⚙️ 📛 💖:

/files/{file_path:path}

👉 💼, 📛 🔢 file_path, & 🏁 🍕, :path, 💬 ⚫️ 👈 🔢 🔜 🏏 🙆 .

, 👆 💪 ⚙️ ⚫️ ⏮️:

from fastapi import FastAPI

app = FastAPI()


@app.get("/files/{file_path:path}")
async def read_file(file_path: str):
    return {"file_path": file_path}

Tip

👆 💪 💪 🔢 🔌 /home/johndoe/myfile.txt, ⏮️ 🏁 🔪 (/).

👈 💼, 📛 🔜: /files//home/johndoe/myfile.txt, ⏮️ 2️⃣✖️ 🔪 (//) 🖖 files & home.

🌃

⏮️ FastAPI, ⚙️ 📏, 🏋️ & 🐩 🐍 🆎 📄, 👆 🤚:

  • 👨‍🎨 🐕‍🦺: ❌ ✅, ✍, ♒️.
  • 💽 ""
  • 💽 🔬
  • 🛠️ ✍ & 🏧 🧾

& 👆 🕴 ✔️ 📣 👫 🕐.

👈 🎲 👑 ⭐ 📈 FastAPI 🔬 🎛 🛠️ (↖️ ⚪️➡️ 🍣 🎭).