FastAPI Web框架 [1.1]

Posted Ch4536251

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FastAPI Web框架 [1.1]相关的知识,希望对你有一定的参考价值。

学习,记录一下
官方文档:https://fastapi.tiangolo.com/

路径参数:

from fastapi import FastAPI     #导入 FastAPI
from enum import Enum		

app = FastAPI()         #创建一个FastAPI实例
@app.get("/")           #路径
async def root():
    return "message": "Hello World"

@app.get("/items/item_id")
async def read_item(item_id:int):
    return "item_id":item_id

@app.get("/users/me")
async def read_user_name():
    return "user_id":"the current user"

@app.get("/users/user_id")
async def read_user(user_id:str):
    return "user_id":user_id

class ModelName(str, Enum):            #路径类
    alexnet = "alexnet"
    resnet = "resnet"
    lenet = "lenet"

@app.get("/models/model_name")
async def get_model(model_name: ModelName):         #声明路径参数
    if model_name == ModelName.alexnet:
        return "model_name": model_name, "message": "alexnet!!!!!"
    if model_name.value == "lenet":
        return "model_name": model_name, "message": "lenet!!!!!!!"

    return "model_name": model_name, "message": "resnet!!!!!!!!!"

@app.get("/files/file_path:path")
async def read_file(file_path:str):
    return "file_path":file_path

查询参数

from fastapi import FastAPI
from typing import Optional
app = FastAPI()

fake_item_db = ["item_name":"foo","item_name":"Bar","item_name":"Baz"]

@app.get("/items/")
async def read_item(skip:int = 0,limit:int = 10):     #默认值为0的参数skip;和默认值为10的参数limit
    return fake_item_db[skip:skip + limit]
# http://127.0.0.1:8000/items/?skip=0&limit=10              http://127.0.0.1:8000/items/

@app.get("/items/item_id")
async def read_item(item_id:str,q:Optional[str] = None):         #函数参数 q 将是可选的,并且默认值为 None
    if q:
        return "item_id":item_id,"q":q
    return "item_id":item_id
# http://127.0.0.1:8000/items/123?q=1

@app.get("/items/item_id")                  #bool
async def read_item(item_id:str,q:Optional[str]=None,short:bool=False):
    item = "item_id": item_id
    if q:
        item.update("q": q)
    if not short:
        item.update(
            "description":"This is an amazing item that has a long description"
        )
    return item
# http://127.0.0.1:8000/items/123?q=1&short=Yes

@app.get("/users/user_id/items/item_id")        #bool
async def read_user_item(
        user_id:int,item_id:str,q:Optional[str]=None,short:bool=False
):
    item = "item_id":item_id,"owner_id":user_id
    if q:
        item.update("q":q)
    if not short:
        item.update(
            "description": "This is an amazing item that has a long description"
        )
    return item
# http://127.0.0.1:8000/users/123/items/123?q=1&short=True

@app.get("/items/item_id")            #必须存在needy查询参数!
async def read_user_item(item_id:str,needy:str):
    item = "item_id":item_id,"needy":needy
    return item
# http://127.0.0.1:8000/items/123?needy=123

@app.get("/items/item_id")
async def read_user_item(
        item_id:str,needy:str,skip:int=0,limit:Optional[int]=None   #必须的needy参数,默认值为0的skip参数;可选的limit
):
    item = "item_id":item_id,"needy":needy,"skip":skip,"limit":limit
    return item
# http://127.0.0.1:8000/items/123?needy=123&limit=123

# 如果你不想添加一个特定的值,而只是想使该参数成为可选的,则将默认值设置为 None
# 但当你想让一个查询参数成为必需的,不声明任何默认值就可以:

以上是关于FastAPI Web框架 [1.1]的主要内容,如果未能解决你的问题,请参考以下文章

三分钟了解 Python3 的异步 Web 框架 FastAPI

FastAPI Web框架 [1.4]

FastAPI Web框架 [依赖项]

FastAPI Web框架 [依赖项]

FastAPI Web框架 [1.10]

FastAPI Web框架 [1.9]