FastAPI启动

  开发


main.py

from fastapi import FastAPI

app = FastAPI()


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


@app.get("/items/{item_id}")
async 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}

运行

uvicorn main:app --reload

在pycharm中直接跑:

from fastapi import FastAPI
import uvicorn

app = FastAPI()


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


@app.get("/items/{item_id}")
async 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(app)

进入swaggerUI文档 ,Interactive API docs

http://127.0.0.1:8000/docs

进入 替代API文档 ,Alternative API docs。

http://127.0.0.1:8000/redoc

LEAVE A COMMENT

Captcha Code