FastAPI

FastAPI是一个基于 python 的,高性能Web框架

在Uvicorn下运行的FastAPI应用程序是可用的最快的Python框架之一

安装

pip install fastapi

运行需要ASGI服务器,在生产环境中使用 uvicorn

pip install uvicorn

 eg:

  main.py

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def home():
    return {"Hello": "World"}

运行

uvicorn main:app --reload

eg:

from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()

class Item(BaseModel):
    name: str
    price: float
    is_offer: bool = None

@app.get("/")
def home():
    return {"Hello": "World"}

@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}

自动生成的接口文档

(1)http://localhost:8000/docs

 (2)http://localhost:8000/redoc

 

posted @ 2021-01-04 17:37  慕尘  阅读(1118)  评论(0编辑  收藏  举报