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 ๐Ÿ”ฌ ๐ŸŽ› ๐Ÿ› ๏ธ (โ†–๏ธ โšช๏ธโžก๏ธ ๐Ÿฃ ๐ŸŽญ).