๐ช - ๐ ๐ท¶
โฎ๏ธ FastAPI, ๐ ๐ช ๐ฌ, โ, ๐, & โ๏ธ ๐ฒ ๐ ๐ฆ ๐ท (๐ Pydantic).
๐ ๐¶
๐ ๐ช ๐ฌ ๐ข ๐พ. ๐ผ, ๐ list
:
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: list = []
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
tags: list = []
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
๐ ๐ โ tags
๐, ๐ โซ๏ธ ๐ซ ๐ฃ ๐ ๐ฃ ๐.
๐ ๐ โฎ๏ธ ๐ ๐ข¶
โ๏ธ ๐ โ๏ธ ๐ฏ ๐ ๐ฃ ๐ โฎ๏ธ ๐ ๐, โ๏ธ "๐ ๐ข":
๐ โจ List
¶
๐ 3๏ธโฃ.9๏ธโฃ & ๐ ๐ ๐ช โ๏ธ ๐ฉ list
๐ฃ ๐ซ ๐ โ ๐ฅ ๐ ๐ ๐. ๐ถ
โ๏ธ ๐ โฌ โญ 3๏ธโฃ.9๏ธโฃ (3๏ธโฃ.6๏ธโฃ & ๐), ๐ ๐ฅ ๐ช ๐ List
โช๏ธโก๏ธ ๐ฉ ๐ typing
๐น:
from typing import List, Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: List[str] = []
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
๐ฃ list
โฎ๏ธ ๐ ๐ข¶
๐ฃ ๐ ๐ โ๏ธ ๐ ๐ข (๐ ๐), ๐ list
, dict
, tuple
:
- ๐ฅ ๐ ๐ โฌ ๐
๐ 3๏ธโฃ.9๏ธโฃ, ๐ ๐ซ ๐ โฌ โช๏ธโก๏ธ
typing
๐น - ๐ถโโ๏ธ ๐ ๐(โ) "๐ ๐ข" โ๏ธ โฌ ๐:
[
&]
๐ 3๏ธโฃ.9๏ธโฃ โซ๏ธ ๐:
my_list: list[str]
โฌ ๐ โญ 3๏ธโฃ.9๏ธโฃ, โซ๏ธ ๐:
from typing import List
my_list: List[str]
๐ ๐ ๐ฉ ๐ โ ๐ ๐.
โ๏ธ ๐ ๐ ๐ฉ โ ๐ท ๐ข โฎ๏ธ ๐ ๐.
, ๐ ๐ผ, ๐ฅ ๐ช โ tags
๐ฏ "๐ ๐ป":
from typing import List, Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: List[str] = []
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: list[str] = []
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
tags: list[str] = []
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
โ ๐¶
โ๏ธ โคด๏ธ ๐ฅ ๐ญ ๐ โซ๏ธ, & ๐ค ๐ ๐ ๐ซ๐ ๐ซ ๐, ๐ซ ๐ ๐ฒ ๐ ๐ป.
& ๐ โ๏ธ ๐ ๐ฝ ๐ โ ๐ ๐ฌ, set
.
โคด๏ธ ๐ฅ ๐ช ๐ฃ tags
โ ๐ป:
from typing import Set, Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: Set[str] = set()
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: set[str] = set()
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
tags: set[str] = set()
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
โฎ๏ธ ๐, ๐ฅ ๐ ๐จ ๐จ โฎ๏ธ โ ๐, โซ๏ธ ๐ ๐ โ ๐ ๐ฌ.
& ๐โ ๐ ๐ข ๐ ๐, ๐ฅ โน โ๏ธ โ, โซ๏ธ ๐ ๐ข โ ๐ ๐ฌ.
& โซ๏ธ ๐ โ / ๐ โก๏ธ ๐โโ๏ธ.
๐ฆ ๐ท¶
๐ ๐ข Pydantic ๐ท โ๏ธ ๐.
โ๏ธ ๐ ๐ ๐ช โซ๏ธ โ1๏ธโฃ Pydantic ๐ท.
, ๐ ๐ช ๐ฃ ๐ ๐ฆ ๐ป "๐" โฎ๏ธ ๐ฏ ๐ข ๐, ๐ & ๐ฌ.
๐ ๐, ๐ฒ ๐ฆ.
๐ฌ ๐¶
๐ผ, ๐ฅ ๐ช ๐ฌ Image
๐ท:
from typing import Set, Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Image(BaseModel):
url: str
name: str
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: Set[str] = set()
image: Union[Image, None] = None
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Image(BaseModel):
url: str
name: str
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: set[str] = set()
image: Union[Image, None] = None
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Image(BaseModel):
url: str
name: str
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
tags: set[str] = set()
image: Image | None = None
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
โ๏ธ ๐ ๐¶
& โคด๏ธ ๐ฅ ๐ช โ๏ธ โซ๏ธ ๐ ๐ข:
from typing import Set, Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Image(BaseModel):
url: str
name: str
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: Set[str] = set()
image: Union[Image, None] = None
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Image(BaseModel):
url: str
name: str
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: set[str] = set()
image: Union[Image, None] = None
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Image(BaseModel):
url: str
name: str
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
tags: set[str] = set()
image: Image | None = None
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
๐ ๐ โ ๐ FastAPI ๐ โ ๐ช ๐:
{
"name": "Foo",
"description": "The pretender",
"price": 42.0,
"tax": 3.2,
"tags": ["rock", "metal", "bar"],
"image": {
"url": "http://example.com/baz.jpg",
"name": "The Foo live"
}
}
๐, ๐คธ ๐ ๐, โฎ๏ธ FastAPI ๐ ๐ค:
- ๐จโ๐จ ๐โ๐ฆบ (๐ ๏ธ, โ๏ธ), ๐ฆ ๐ท
- ๐ฝ ๐ ๏ธ
- ๐ฝ ๐ฌ
- ๐ง ๐งพ
๐ ๐ & ๐ฌ¶
โ๏ธ โช๏ธโก๏ธ ๐ โญ ๐ ๐ str
, int
, float
, โ๏ธ. ๐ ๐ช โ๏ธ ๐
๐ โญ ๐ ๐ ๐ โช๏ธโก๏ธ str
.
๐ ๐ ๐ ๐ โ๏ธ, ๐ ๐ฉบ Pydantic ๐ ๐. ๐ ๐ ๐ ๐ผ โญ ๐.
๐ผ, Image
๐ท ๐ฅ โ๏ธ url
๐, ๐ฅ ๐ช ๐ฃ โซ๏ธ โฉ๏ธ str
, Pydantic HttpUrl
:
from typing import Set, Union
from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Image(BaseModel):
url: HttpUrl
name: str
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: Set[str] = set()
image: Union[Image, None] = None
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Image(BaseModel):
url: HttpUrl
name: str
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: set[str] = set()
image: Union[Image, None] = None
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Image(BaseModel):
url: HttpUrl
name: str
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
tags: set[str] = set()
image: Image | None = None
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
๐ป ๐ โ โ ๐, & ๐ ๐ป ๐ / ๐ โ .
๐ข โฎ๏ธ ๐ ๐¶
๐ ๐ช โ๏ธ Pydantic ๐ท ๐พ list
, set
, โ๏ธ:
from typing import List, Set, Union
from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Image(BaseModel):
url: HttpUrl
name: str
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: Set[str] = set()
images: Union[List[Image], None] = None
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Image(BaseModel):
url: HttpUrl
name: str
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: set[str] = set()
images: Union[list[Image], None] = None
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Image(BaseModel):
url: HttpUrl
name: str
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
tags: set[str] = set()
images: list[Image] | None = None
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
๐ ๐ โ (๐, โ, ๐, โ๏ธ) ๐ป ๐ช ๐:
{
"name": "Foo",
"description": "The pretender",
"price": 42.0,
"tax": 3.2,
"tags": [
"rock",
"metal",
"bar"
],
"images": [
{
"url": "http://example.com/baz.jpg",
"name": "The Foo live"
},
{
"url": "http://example.com/dave.jpg",
"name": "The Baz"
}
]
}
Info
๐ โ images
๐ ๐ โ๏ธ ๐ ๐ผ ๐.
๐ ๐ฆ ๐ท¶
๐ ๐ช ๐ฌ ๐ฒ ๐ ๐ฆ ๐ท:
from typing import List, Set, Union
from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Image(BaseModel):
url: HttpUrl
name: str
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: Set[str] = set()
images: Union[List[Image], None] = None
class Offer(BaseModel):
name: str
description: Union[str, None] = None
price: float
items: List[Item]
@app.post("/offers/")
async def create_offer(offer: Offer):
return offer
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Image(BaseModel):
url: HttpUrl
name: str
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: set[str] = set()
images: Union[list[Image], None] = None
class Offer(BaseModel):
name: str
description: Union[str, None] = None
price: float
items: list[Item]
@app.post("/offers/")
async def create_offer(offer: Offer):
return offer
from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Image(BaseModel):
url: HttpUrl
name: str
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
tags: set[str] = set()
images: list[Image] | None = None
class Offer(BaseModel):
name: str
description: str | None = None
price: float
items: list[Item]
@app.post("/offers/")
async def create_offer(offer: Offer):
return offer
Info
๐ โ Offer
โ๏ธ ๐ Item
โ, โ ๐ โ๏ธ ๐ฆ ๐ Image
โ
๐ช ๐ ๐¶
๐ฅ ๐ ๐ ๐ฒ ๐ป ๐ช ๐ โ ๐ป array
(๐ list
), ๐ ๐ช ๐ฃ ๐ ๐ข ๐ข, ๐ Pydantic ๐ท:
images: List[Image]
โ๏ธ ๐ 3๏ธโฃ.9๏ธโฃ & ๐:
images: list[Image]
:
from typing import List
from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Image(BaseModel):
url: HttpUrl
name: str
@app.post("/images/multiple/")
async def create_multiple_images(images: List[Image]):
return images
from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl
app = FastAPI()
class Image(BaseModel):
url: HttpUrl
name: str
@app.post("/images/multiple/")
async def create_multiple_images(images: list[Image]):
return images
๐จโ๐จ ๐โ๐ฆบ ๐¶
& ๐ ๐ค ๐จโ๐จ ๐โ๐ฆบ ๐.
๐ฌ ๐ ๐:
๐ ๐ซ ๐ซ ๐ค ๐ ๐ ๐จโ๐จ ๐โ๐ฆบ ๐ฅ ๐ ๐ท ๐ โฎ๏ธ dict
โฉ๏ธ Pydantic ๐ท.
โ๏ธ ๐ ๐ซ โ๏ธ ๐ ๐ ๐ซ ๐ฏโโ๏ธ, ๐จ #๏ธโฃ ๐ ๐ & ๐ ๐ข ๐ ๐ ๐ป ๐โโ๏ธ.
๐ช โ dict
โ¶
๐ ๐ช ๐ฃ ๐ช dict
โฎ๏ธ ๐ ๐ & ๐ฒ ๐ ๐.
๐ต โ๏ธ ๐ญ โช โซ๏ธโ โ ๐/๐ข ๐ (๐ ๐ผ โฎ๏ธ Pydantic ๐ท).
๐ ๐ โ ๐ฅ ๐ ๐ ๐จ ๐ ๐ ๐ ๐ซ โช ๐ญ.
๐ โ ๐ผ ๐โ ๐ ๐ โ๏ธ ๐ ๐ ๐, โ
int
.
๐ โซ๏ธโ ๐ฅ ๐ ๐ ๐ฅ.
๐ ๐ผ, ๐ ๐ ๐ซ ๐ dict
๐ โซ๏ธ โ๏ธ int
๐ โฎ๏ธ float
๐ฒ:
from typing import Dict
from fastapi import FastAPI
app = FastAPI()
@app.post("/index-weights/")
async def create_index_weights(weights: Dict[int, float]):
return weights
from fastapi import FastAPI
app = FastAPI()
@app.post("/index-weights/")
async def create_index_weights(weights: dict[int, float]):
return weights
Tip
โ๏ธ ๐คฏ ๐ ๐ป ๐ด ๐โ๐ฆบ str
๐.
โ๏ธ Pydantic โ๏ธ ๐ง ๐ฝ ๐ ๏ธ.
๐ โ ๐, โ๏ธ ๐ ๐ ๏ธ ๐ฉโ๐ป ๐ช ๐ด ๐จ ๐ป ๐, ๐ ๐ ๐ป ๐ ๐ ๐ข, Pydantic ๐ ๐ ๐ซ & โ ๐ซ.
& dict
๐ ๐จ weights
๐ ๐ค โ๏ธ int
๐ & float
๐ฒ.
๐¶
โฎ๏ธ FastAPI ๐ โ๏ธ ๐ ๐ช ๐ Pydantic ๐ท, โช ๐ง ๐ ๐ ๐ , ๐ & ๐.
โ๏ธ โฎ๏ธ ๐ ๐ฐ:
- ๐จโ๐จ ๐โ๐ฆบ (๐ ๏ธ ๐ โ)
- ๐ฝ ๐ ๏ธ (.โ.. โ / ๐ ๏ธ)
- ๐ฝ ๐ฌ
- ๐ ๐งพ
- ๐ง ๐ฉบ