FastApi文档

叶世*

python,web

项目描述

fastapi文档,会持续更新

上传时间

2021.01.08

浏览人数

1147人
叶世*
天津市津南区
Hot:9180

安装fastapipip install fastapi安装uvicorn (生产环境)pip install uvicorn[standard]

FaseApi basic

写法

使用装饰器


import uvicorn # 超快速ASGI服务器
from fastapi import FastAPI

app = FastAPI()

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

if __name__ == '__main__':
   uvicorn.run(app, host="127.0.0.1", port=8002) # 访问接口

使用route

import uvicorn
from fastapi import FastAPI, routing

async def main():
   return {"main": "main"}

async def test():
   return {"test": "test"}


router = [
   routing.APIRoute("/", endpoint=main, methods=["get","post"]),
   routing.APIRoute("/test/", endpoint=test, methods=["get","post"])
]
app = FastAPI(routes=router)

if __name__ == '__main__':
   uvicorn.run(app=app, host="127.0.0.1", port=8003)

async

官方: 如果你不清楚你函数里的调用是否异步,那就定义为普通函数。因为它可以采用多线程的方式解决.

反之,定义了async函数,里面却是同步的调用(第一个函数) 执行非常的慢.

渲染前段页面

FastApi类似Flask 依赖Jinja2语法

在函数当前文件夹新建一个templates 用于存储html文件

from fastapi import FastAPI
import uvicorn
from starlette.templating import Jinja2Templates
from starlette.requests import Request

app = FastAPI()

templates = Jinja2Templates(directory="templates")

@app.get("/")
async def home(request: Request):
   return  templates.TemplateResponse('home.html', {"request": request})
# templates.TemplateResponse 与 flask rander_template类似,会自动从当前文件夹的下的templates目录寻找对应的html文件,  request从前端{{request}}就可以接取到了

if __name__ == '__main__':
   uvicorn.run(app, host="127.0.0.1", port=8002)

项目命令

  • 项目重启

uvicorn main:app --reload 

--reload:让服务器更新代码后重新启动

  • 交互式api文档

在接口后加入/docs 即可连接

  • api文档升级

在接口后加入/redoc 即可连接

*让用户可以和接口之间进行交互

路径参数

fastapi中函数内的参数是可以规定类型的

例如:

def main(test: int):  # test参数必须为int类型,也可以规定为float str类型
return {"test": test}
# 如果参数传递一个不是规定的值
http://127.0.0.1:8003/test/abc <--传递的str类型
  { # 提示参数错误, value 必须是integer类型
   "detail": [
       {
           "loc": [
               "path",
               "item_id"
           ],
           "msg": "value is not a valid integer",
           "type": "type_error.integer"
       }
   ]
  }

包含路径的路径参数

/files/{file_path:path}

结尾部分的 :path 说明该参数应匹配任意的路径

你可能会需要参数包含 /home/johndoe/myfile.txt,以斜杠(/)开头。

在这种情况下,URL 将会是 /files//home/johndoe/myfile.txt,在fileshome 之间有一个双斜杠(//

Enum枚举

from enum import Enum
from fastapi import FastAPI

class ModelName(str, Enum):
   alexnet = "alexnet"
   resnet = "resnet"
   lenet = "lenet"

app = FastAPI()

@app.get("/models/{model_name}")
async def get_model(model_name: ModelName):
   # 限定参数必须是ModelName枚举中的
   if model_name == ModelName.alexnet:# 枚举判断方法1
       return {"model_name": model_name}

   if model_name.value == "lenet":# 枚举判断方法2
       return {"model_name": model_name}

   return {"model_name": model_name, "message": "Have some residuals"}

将已经存在的值存在class类中,这个类中存储的必须是字符串,  通过枚举判断来输出指定的值

Form表单传参

从 body  JSON 里获取参数, 这里说一下从传统的 Form 表单里获取数据(application/x-www-form-urlencoded/multipart/form-data)

想要从 Form 中获取数据,需要导入 Form 模块, 并且安装 python-multipart

from fastapi import FastAPI, Form
@app.post("/login")
async def login(*, name: str = Form(...), pwd: str = Form(...)):  # 函数的接受参数第一个是 * 代表此函数只接受关键字传参
   # Form(...) 必写
   return {
       "name": name,
       "pwd": pwd
   }



叶世*

4.8分

还未获得评语哦~
python python培训 天津python培训 python培训班 python培训多少钱 Django 天津python培训班 天津python培训 天津python培训机构。天津python培训学什么? python python培训 天津python培训 Mysql django Numpy flask anaconda 天津python培训 天津python培训班 python培训 python 天津python培训 天津python培训班 数据爬虫 爬虫技术 python爬虫 python 天津python培训 python培训 python PyCharm Eclipse + 天津python培训 python培训 python PyCharm Eclipse + PyDev python培训 天津python培训 python培训班 python培训多少钱 PyCharm Eclipse + PyDev visual studio 天津python培训 天津python培训班 PyCharm Eclipse + PyDev visual studio 天津python培训 天津python培训班 天津python培训机构 python培训班 C/C++ Java Python 天津python培训 天津python培训班 天津python培训机构 python培训班 C/C++ Java Python 天津python培训 天津python培训班 天津python培训机构 python培训班 C/C++ Java Python 天津python培训 天津python培训班 天津python培训机构 python培训班 C/C++ Java Python 天津python培训 天津python培训班 天津python培训机构 python培训班 python java web 天津python培训 天津python培训哪家好 天津python培训机构 天津python培训班 python java web前端 天津python培训 天津python培训班 天津python培训机构 天津python培训学校 python java web 天津python培训 天津python培训班 天津python培训机构 python培训 python java web 天津python培训 天津python培训班 天津python培训机构 天津python培训学校 python java web 天津python培训 天津python培训班 天津python培训机构 python java web 天津python培训 天津python培训班 天津python培训机构 天津python培训学校 python Java web 天津python培训 天津python培训学校 天津python培训机构 天津python培训班 python java web 天津python培训 天津python培训班 天津python培训机构 python培训 python Java web 天津python培训 天津python培训机构 天津python培训班 python培训 python java web 天津python培训 天津python培训机构 天津python培训学校 python培训 python java web 天津python培训 天津python培训班 天津python培训机构 python培训 python java web 天津python培训 天津python培训班 天津python培训机构 python培训 python java web 天津python培训 天津python培训班 天津python培训机构 python培训学校 python java web 天津python培训 天津python培训机构 python培训班 python java web 天津Java培训 天津Java培训班 Java培训 天津Java培训机构 python web java 天津python培训 天津python培训班 天津python培训机构 python java web 天津python培训 天津python培训班 python培训 天津python培训机构 python java web 天津python培训 天津python培训班 天津python培训机构 python培训 python web java 天津python培训 天津python培训班 python培训 python web java 天津python培训 天津python培训班 python培训 python web java 天津python培训 天津python培训机构 天津python培训学校 python web java 天津python培训班 天津python培训机构 天津python培训学校 python java web 天津python培训 天津python培训机构 python培训 python web java 天津python培训 天津python培训班 python培训 python web java 天津python培训 天津python培训机构 python培训 python web java 天津python培训 天津python培训班 python培训 python web java 天津python培训 天津python培训班
叶世*    9180 天津市津南区 设计师杨冰是女孩 2003.**.**
本网站已在中国版权保护中心登记了美术作品著作权与软件著作权违者将依法追究责任,特此声明! | Copyright©2013-2022,zhuzuoji.com | 诚筑说培训学校(天津)有限公司内容支持 | 京ICP备17020986号-5