gpt4 book ai didi

python - Sphinx Autoflask - 区分同一功能上 GET 和 POST 请求的文档字符串

转载 作者:行者123 更新时间:2023-12-03 17:00:32 25 4
gpt4 key购买 nike

我有一个 Flask 应用程序,我正在通过 python-sphinx 构建文档。

我目前正在使用 sphinxcontrib.autohttp.flask 的 autoflask 扩展

我的问题是:如何准备文档字符串,将不同的信息正确应用到同一路由的 GET 版本和 POST 版本。

例如一个小函数:

@app.route('/add_event', methods=['GET', 'POST'])
def add_event():
"""
..http:get:: /add_event
:return: Test
..http:post:: /add_event
:return: Test2
"""
if request.method == 'GET':
# get some things
person_id = request.args.get('id')
return render_template('create_event.html', race_event_form=test_form)
if request.method == 'POST':
# post some things

return redirect('/person_profile/id/{0}'.format(request.args.get('id')))

conf.py 中我当前的扩展

extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.ifconfig',
'sphinx.ext.viewcode',
'sphinx.ext.githubpages',
'sphinxcontrib.httpdomain',
'sphinxcontrib.autohttp.flask',
'sphinxcontrib.autohttp.flaskqref'
]

sphinx 输出看起来像

enter image description here

是否有可能为 GET 纠正一件事而为 POST 纠正另一件事?我真的很想避免将每个函数拆分为两个单独的 get/post 函数。

此外,是否可以让 autoflask 输出所需参数通过 request.argsrequest.form 传递,例如 person_id 变量获取请求?

最佳答案

尝试从 Flask autodoc 中删除重载的 View 函数并单独定义请求,同时让您的 API 的其余部分自动生成。

这是一个例子...

conf.py 中(插件的顺序很重要):

extensions = [
'sphinx.ext.autodoc',
'sphinxcontrib.httpdomain',
'sphinxcontrib.autohttp.flask',
]

在重组文本文件中:

API
===

.. autoflask:: path.to.your:app
:undoc-static:
:undoc-endpoints: your_view_method

.. http:get:: / your_view_endpoint

Some docstring you wrote.

.. http:post:: / your_view_endpoint

Another docstring you wrote.

关于您的第二个问题,您可以查看httpdomain here 的指令。 .

关于python - Sphinx Autoflask - 区分同一功能上 GET 和 POST 请求的文档字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40744982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com