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

FastAPI

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

FastAPI

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

FastAPI

 

相关文章:

  • 2022-12-23
  • 2021-12-19
  • 2021-11-19
  • 2021-11-13
  • 2021-11-28
  • 2022-02-24
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-27
  • 2021-12-10
  • 2022-01-03
  • 2021-12-29
  • 2021-08-25
  • 2021-12-18
相关资源
相似解决方案