一、安裝FastAPI
FastAPI - 是一個(gè)現(xiàn)代的,快速(高性能)python web框架,用于基于標(biāo)準(zhǔn)Python類型提示使用Python 3.6+構(gòu)建API(https://fastapi.tiangolo.com/)。
FastAPI基于以下
使用Uvicorn服務(wù)器ASGI規(guī)范(ASGI是異步服務(wù)器網(wǎng)關(guān)接口 是WSGI的精神繼承者,在具有異步功能的Python Web服務(wù)器,框架和應(yīng)用程序之間提供標(biāo)準(zhǔn)接口。)
Starlette:是一種輕量級(jí)的ASGI框架/工具包,是構(gòu)建高性能異步服務(wù)的理想選擇(https://www.starlette.io/)。
Pydantic:用于驗(yàn)證數(shù)據(jù)(https://pydantic-docs.helpmanual.io/)。
uvicorn:主要用于加載和提供應(yīng)用程序的服務(wù)器(http://www.uvicorn.org/)。
1、通過以下命令,安裝所有安裝包:
# pip3 install fastapi[all]
2、安裝成功后,顯示:

3、查看已經(jīng)安裝好的庫:

二、第一個(gè) Hello World
from fastapi? import FastAPI
import uvicorn
app = FastAPI()
@app.get("/")
async def root():
? ? return {"message":"Hello world, FastAPI"}
# Press the green button in the gutter to run the script.
if __name__ =='__main__':
? ? uvicorn.run(app='main:app', host="127.0.0.1", port=5555, reload=True, debug=True)
三、訪問首頁
http://127.0.0.1:5555/
四、交互API文檔
http://127.0.0.1:5555/docs
你將會(huì)看到自動(dòng)生成的API交互文檔。
五、替代API文檔
http://127.0.0.1:5555/redoc