对flask提供数据校验和openapi(swagger)文档自动生成的支持
Project description
as_flask_docs
在flask中支持数据校验和openapi(swagger)文档自动生成
flask_docs fork自flask-siwadoc,感谢原作者提供了主要功能的实现,原项目不支持flask中blueprint的使用因此魔改
特性
1、API接口自动生成文档
需要通过CreateDoc(app)
,利用装饰器 siwa.doc()
修饰flask视图函数,即可将该视图对应的路由加入openapi的paths中。
2、支持多种参数指定
可以将请求参数放置在 query
、path
、header
、cookie
、body
5种不同的地方,完全支持openapi规范所定义的5种参数方式。
3、参数校验与自动转换
基于pydantic
,请求参数可自动转换为对应的数据类型
4、ui切换
内置了swagger
(默认)、redoc
、rapidoc
等多种UI界面,通过query参数"ui"控制
5、支持标签与分组
安装
pip install as-flask-docs
快速开始
from flask import Flask
from flask_docs import CreateDoc
app = Flask(__name__)
models = {}
modules = import_module('tests.bps')
for name in dir(modules):
instance = getattr(modules, name)
if isinstance(instance, Blueprint):
app.register_blueprint(instance)
if isinstance(instance, Docs):
models.update(instance.models)
CreateDoc(app, title="flask_docs", description="一个自动生成openapi文档的库", models=models)
from flask import Blueprint
from flask_siwadoc import Docs
from pydantic import BaseModel
test1 = Blueprint("t1", __name__, url_prefix="/api/t1")
d = Docs(tags=["1"])
class A(BaseModel):
a: int = 1
@test1.get('')
@d(body=A)
def fun(body: A):
return 't1'
运行后,访问 http://127.0.0.1:5000/docs 就可以看到openapi文档页面
更多用法介绍请移步原项目
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
as_flask_docs-1.0.5.tar.gz
(11.0 kB
view details)
Built Distribution
File details
Details for the file as_flask_docs-1.0.5.tar.gz
.
File metadata
- Download URL: as_flask_docs-1.0.5.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5bcfca411dca54474e3ce89b3cb9572e9a2d28dbdc8821c26a82ad2eea5a33f8 |
|
MD5 | e4c1c859c835d825bfedbfce3530ff8d |
|
BLAKE2b-256 | 8ccb0b00af6061bee1dc39b1e4eb92602738cd617468f2dd42f2e6929181c693 |
File details
Details for the file as_flask_docs-1.0.5-py3-none-any.whl
.
File metadata
- Download URL: as_flask_docs-1.0.5-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2510f6e9bcfd86f8d30613cca639b6b0ca9655b6e41a1375e37412d7fa06461c |
|
MD5 | 7c59afc1479acf387d5598ac02133365 |
|
BLAKE2b-256 | 5bf175cc11527ed65430a94503d6e404d28f7ac4396e08794a404419e317ef72 |