美文网首页
python docString+sphinx构建接口文档步骤

python docString+sphinx构建接口文档步骤

作者: wingood | 来源:发表于2019-11-26 23:38 被阅读0次

python docString+sphinx构建接口文档步骤

效果展示


Snipaste_2019-11-26_23-39-47.png

环境准备:

pip install sphinx
pip install sphinx_rtd_theme  # sphinx 第三方主题

项目准备:

创建项目目录:


Snipaste_2019-11-25_21-42-05.png

api.py

# -*- coding: utf-8 -*-
"""
# @Time    : 2019/11/24 17:30
# @Author  : wingood
# @Email   : 122782387@qq.com
"""
from flask import Flask, request
from utils import add
app = Flask(__name__)


def index():
    """
    :标题: reStructuredText语法注释说明
    :说明: 该部分注释和函数功能无关,主要说明是rst语法作用
    :作者:
    - Seay
    - Seay1
    - Seay2

    :时间: 2016年06月21日

    :概述: 这是一篇
    关于reStructuredText

    语法说明。

    -a            command-line option "a"
    -b file       options can have arguments
                and long descriptions
    --long        options can be long also
    --input=file  long options can also have
                arguments
    /V            DOS/VMS-style options too
    """
    a = request.values.get("a")
    b = request.values.get("b")
    return str(add(a, b))

if __name__ == "__main__":
    app.run()

utils.py

# -*- coding: utf-8 -*-
"""
# @Time    : 2019/11/24 17:30
# @Author  : wingood
# @Email   : 122782387@qq.com
"""


def add(x, y):
    """
    :函数说明:计算两数之和
    :x:加数一
    :y:加数二

    :return 两数之和
    """
    return x+y

sphinx生成文档

  • 进入docs目录
cd docs
  • 执行命令
 sphinx-quickstart
  • 可以参考如下 > autodoc: automatically insert docstrings from modules (y/n) [n]: y 一定要选择y
~/code/docStringsTest/docs » sphinx-quickstart                                                wingood@xuwenhaodeMacBook-Pro
Welcome to the Sphinx 1.8.5 quickstart utility.

Please enter values for the following settings (just press Enter to
accept a default value, if one is given in brackets).

Selected root path: .

You have two options for placing the build directory for Sphinx output.
Either, you use a directory "_build" within the root path, or you separate
"source" and "build" directories within the root path.
> Separate source and build directories (y/n) [n]: 

Inside the root directory, two more directories will be created; "_templates"
for custom HTML templates and "_static" for custom stylesheets and other static
files. You can enter another prefix (such as ".") to replace the underscore.
> Name prefix for templates and static dir [_]: 

The project name will occur in several places in the built documentation.
> Project name: myApiDoc
> Author name(s): wingood
> Project release []: v1.0

If the documents are to be written in a language other than English,
you can select a language here by its language code. Sphinx will then
translate text that it generates into that language.

For a list of supported codes, see
http://sphinx-doc.org/config.html#confval-language.
> Project language [en]: zh_CN

The file name suffix for source files. Commonly, this is either ".txt"
or ".rst".  Only files with this suffix are considered documents.
> Source file suffix [.rst]: 

One document is special in that it is considered the top node of the
"contents tree", that is, it is the root of the hierarchical structure
of the documents. Normally, this is "index", but if your "index"
document is a custom template, you can also set this to another filename.
> Name of your master document (without suffix) [index]: 
Indicate which of the following Sphinx extensions should be enabled:
> autodoc: automatically insert docstrings from modules (y/n) [n]: y
> doctest: automatically test code snippets in doctest blocks (y/n) [n]: y
> intersphinx: link between Sphinx documentation of different projects (y/n) [n]: 
> todo: write "todo" entries that can be shown or hidden on build (y/n) [n]: y
> coverage: checks for documentation coverage (y/n) [n]: 
> imgmath: include math, rendered as PNG or SVG images (y/n) [n]: y
> mathjax: include math, rendered in the browser by MathJax (y/n) [n]: y
> ifconfig: conditional inclusion of content based on config values (y/n) [n]: 
> viewcode: include links to the source code of documented Python objects (y/n) [n]: y
> githubpages: create .nojekyll file to publish the document on GitHub pages (y/n) [n]: 
Note: imgmath and mathjax cannot be enabled at the same time. imgmath has been deselected.

A Makefile and a Windows command file can be generated for you so that you
only have to run e.g. `make html' instead of invoking sphinx-build
directly.
> Create Makefile? (y/n) [y]: 
> Create Windows command file? (y/n) [y]: 

Creating file ./conf.py.
Creating file ./index.rst.
Creating file ./Makefile.
Creating file ./make.bat.

Finished: An initial directory structure has been created.

You should now populate your master file ./index.rst and create other documentation
source files. Use the Makefile to build the docs, like so:
   make builder
where "builder" is one of the supported builders, e.g. html, latex or linkcheck.
  • 修改config.py
import os
import sys
sys.path.insert(0, os.path.abspath('../'))
...
# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
  • 执行命令
~/code/docStringsTest/docs » sphinx-apidoc -o . ../api
  • 清理文件
make clean
  • 生成html
make html
  • html目录
~/code/docStringsTest/docs/_build/html

相关文章

网友评论

      本文标题:python docString+sphinx构建接口文档步骤

      本文链接:https://www.haomeiwen.com/subject/ldglwctx.html