简体   繁体   English

如何配置Sphinx自动烧瓶来记录烧瓶安息API?

[英]How to configure Sphinx auto flask to document flask-restful API?

I have a flask app that I want to use Sphinx's autoflask directive to document a flask-restful API. 我有一个烧瓶应用程序,我想使用Sphinx的autoflask指令来记录烧瓶 - 宁静的API。

https://pythonhosted.org/sphinxcontrib-httpdomain/#module-sphinxcontrib.autohttp.flask https://pythonhosted.org/sphinxcontrib-httpdomain/#module-sphinxcontrib.autohttp.flask

I have installed the module via pip and run sphinx-quickstart, which gives me a conf.py and index.rst. 我已经通过pip安装了模块并运行了sphinx-quickstart,这给了我一个conf.py和index.rst。

I've tried putting the extension into conf.py: 我已经尝试将扩展名放入conf.py:

extensions = ['sphinxcontrib.autohttp.flask']

and the directive into index.rst as per the documentation: 并根据文档将指令转换为index.rst:

.. autoflask:: autoflask_sampleapp:app
:undos-static:

But I can't get the app:module (autoflask_sampleapp:app) part correct. 但我无法得到应用程序:模块(autoflask_sampleapp:app)部分正确。 As a result, when I run sphinx-build I get an error that either the app or the module are not found. 因此,当我运行sphinx-build时,我收到一个错误,即找不到应用程序或模块。

My app trees looks like this: 我的app树看起来像这样:

.
├── admin
├── apis
├── app
│   ├── static
│   └── templates

and from the app's root directory, I can say: 从应用程序的根目录,我可以说:

from apis import profile

How do I configure auto flask in the index.rst to correctly find and load my app's API modules? 如何在index.rst中配置auto flask以正确查找和加载我的应用程序的API模块?

My code structure, where application.py file with flask app, I run my server python appllication.py runserver 我的代码结构,其中application.py文件与flask app,我运行我的服务器python appllication.py runserver

├── application.py
├── _build
│   ├── doctrees
│   │   ├── environment.pickle
│   │   └── index.doctree
│   └── html
│       ├── genindex.html
│       ├── http-routingtable.html
│       ├── index.html
│       ├── objects.inv
│       ├── search.html
│       ├── searchindex.js
│       ├── _sources
│       │   └── index.txt
│       └── _static
├── conf.py
├── index.rst

In conf.py you should include extensions and include abs path to you application.py or any other main flask app file in your project. 在conf.py中,您应该包含扩展名,并在您的项目中包含您的application.py或任何其他主要烧瓶app文件的abs路径。

import os
import sys
sys.path.insert(0, os.path.abspath('.'))

# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#
# needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    'sphinxcontrib.autohttp.flask',
    'sphinxcontrib.autohttp.flaskqref'
]

After you can use blueprints, views from your flask app 您可以使用蓝图,烧瓶应用程序中的视图

My documentation!
=======================================


.. qrefflask:: application:application
   :undoc-static:

=======================================
Api details!
=======================================

.. autoflask:: application:application
   :undoc-static:

In other words, before run make html, you should add abs path to you root application folder via python sys path sys.path.insert(0, os.path.abspath('/home/myproject/')), where /home/myproject folder with your source code. 换句话说,在运行make html之前,你应该通过python sys path sys.path.insert(0,os.path.abspath('/ home / myproject /'))在你的根应用程序文件夹中添加abs路径,其中/ home / myproject文件夹包含您的源代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM