@ -8,7 +8,7 @@
< a href = "https://fastapi.tiangolo.com/ja" > < img src = "https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png" alt = "FastAPI" > < / a >
< a href = "https://fastapi.tiangolo.com/ja" > < img src = "https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png" alt = "FastAPI" > < / a >
< / p >
< / p >
< p align = "center" >
< p align = "center" >
< em > FastAPI framework, high performance, easy to learn, fast to code, ready for production < / em >
< em > FastAPI フレームワーク、高パフォーマンス、学びやすい、素早くコーディングできる、本番運用に対応 < / em >
< / p >
< / p >
< p align = "center" >
< p align = "center" >
< a href = "https://github.com/fastapi/fastapi/actions?query=workflow%3ATest+event%3Apush+branch%3Amaster" target = "_blank" >
< a href = "https://github.com/fastapi/fastapi/actions?query=workflow%3ATest+event%3Apush+branch%3Amaster" target = "_blank" >
@ -27,7 +27,7 @@
---
---
**ドキュメント**: < a href = "https://fastapi.tiangolo.com/ja " target = "_blank" > https://fastapi.tiangolo.com< / a >
**ドキュメント**: < a href = "https://fastapi.tiangolo.com" target = "_blank" > https://fastapi.tiangolo.com< / a >
**ソースコード**: < a href = "https://github.com/fastapi/fastapi" target = "_blank" > https://github.com/fastapi/fastapi< / a >
**ソースコード**: < a href = "https://github.com/fastapi/fastapi" target = "_blank" > https://github.com/fastapi/fastapi< / a >
@ -161,8 +161,6 @@ $ pip install "fastapi[standard]"
`main.py` ファイルを作成し、以下のコードを入力します。
`main.py` ファイルを作成し、以下のコードを入力します。
```Python
```Python
from typing import Union
from fastapi import FastAPI
from fastapi import FastAPI
app = FastAPI()
app = FastAPI()
@ -174,7 +172,7 @@ def read_root():
@app .get("/items/{item_id}")
@app .get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
def read_item(item_id: int, q: str | None = None):
return {"item_id": item_id, "q": q}
return {"item_id": item_id, "q": q}
```
```
@ -183,9 +181,7 @@ def read_item(item_id: int, q: Union[str, None] = None):
コードで `async` / `await` を使用する場合は、`async def` を使います。
コードで `async` / `await` を使用する場合は、`async def` を使います。
```Python hl_lines="9 14"
```Python hl_lines="7 12"
from typing import Union
from fastapi import FastAPI
from fastapi import FastAPI
app = FastAPI()
app = FastAPI()
@ -197,7 +193,7 @@ async def read_root():
@app .get("/items/{item_id}")
@app .get("/items/{item_id}")
async def read_item(item_id: int, q: Union[str, None] = None):
async def read_item(item_id: int, q: str | None = None):
return {"item_id": item_id, "q": q}
return {"item_id": item_id, "q": q}
```
```
@ -288,9 +284,7 @@ INFO: Application startup complete.
Pydantic によって、標準的な Python の型を使ってボディを宣言します。
Pydantic によって、標準的な Python の型を使ってボディを宣言します。
```Python hl_lines="4 9-12 25-27"
```Python hl_lines="2 7-10 23-25"
from typing import Union
from fastapi import FastAPI
from fastapi import FastAPI
from pydantic import BaseModel
from pydantic import BaseModel
@ -300,7 +294,7 @@ app = FastAPI()
class Item(BaseModel):
class Item(BaseModel):
name: str
name: str
price: float
price: float
is_offer: Union[bool, None] = None
is_offer: bool | None = None
@app .get("/")
@app .get("/")
@ -309,7 +303,7 @@ def read_root():
@app .get("/items/{item_id}")
@app .get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
def read_item(item_id: int, q: str | None = None):
return {"item_id": item_id, "q": q}
return {"item_id": item_id, "q": q}