FastAPI 快速搭建一個(gè)REST API 服務(wù)
最近正好在看好的接口文檔方便的工具, 突然看到這個(gè), 試了一下確實(shí)挺方便
快速示例
from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
app = FastAPI()
class Item(BaseModel):
name: str
price: float
is_offer: bool = None
@app.get("/")
def read_root():
return {"Hello": "FastAPI"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q }
# 更新示例
@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
return {"item_name": item.name, "item_id": item_id}
if __name__ == '__main__':
uvicorn.run("main:app", port=5000, reload = True )
啟動(dòng)訪問
- 可以直接調(diào)用測(cè)試API
- 提供兩種在線接口文檔可以預(yù)覽
- 查看 Swagger UI 文檔 http://127.0.0.1:5000/docs
- 查看 ReDoc 文檔 http://127.0.0.1:5000/redoc
Docker 部署
TODO

浙公網(wǎng)安備 33010602011771號(hào)