使用sphnix生成python文档

如何使用sphinx生成python代码文档

安装sphinx:

1
pip install sphinx

新建文件结构如下:

1
2
--Project
--code 用来存放源代码

进入Project后执行:

1
sphinx-quickstart doc

会显示如下提示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
[~/Documents/PlayPython/Project]$ sphinx-quickstart doc         
Welcome to the Sphinx 1.8.2 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: doc
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]: y
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: 身份证号和银行卡号生成器
> Author name(s): xdthesun
> Project release []: 0.1
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]: y
> todo: write "todo" entries that can be shown or hidden on build (y/n) [n]: y
> coverage: checks for documentation coverage (y/n) [n]: y
> 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]: y
> 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]: y
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]: y
> Create Windows command file? (y/n) [y]: y
Creating file doc/source/conf.py.
Creating file doc/source/index.rst.
Creating file doc/Makefile.
Creating file doc/make.bat.
Finished: An initial directory structure has been created.
You should now populate your master file doc/source/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.

当前目录结构显示如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[~/Documents/PlayPython/Project]$ tree -L 3                           
.
├── code
│   ├── function
│   │   ├── BankCardNumber.py
│   │   ├── IDNumber.py
│   │   ├── __init__.py
│   │   └── json
│   ├── main.py
│   └── test.py
└── doc
    ├── Makefile
    ├── build
    └── source
        ├── _static
        ├── _templates
      ├── conf.py
        └── index.rst
8 directories, 8 files

其中code目录用来存放源码,doc目录存放用来生成文档的相关文件

进入doc/source/conf.py中修改15-17行:去掉注释,并修改为如下

1
2
3
4
import os
import sys
sys.path.insert(0, os.path.abspath('../../code'))
# ../../code 源码所在的位置

在Project目录下执行如下命令:

1
2
3
4
5
[~/Documents/PlayPython/Project]$ sphinx-apidoc -o ./doc/source ./code                                                    
Creating file ./doc/source/main.rst.
Creating file ./doc/source/test.rst.
Creating file ./doc/source/function.rst.
Creating file ./doc/source/modules.rst.

进入doc目录执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[~/Documents/PlayPython/Project/doc]$ make html  
正在运行的是 Sphinx v1.8.2
正在加载翻译 [zh_CN]... 完成
创建输出目录…
loading intersphinx inventory from https://docs.python.org/objects.inv...
intersphinx inventory has moved: https://docs.python.org/objects.inv -> https://docs.python.org/3/objects.inv
构建 [mo]:0 个 po 文件的目标文件已过期
构建 [html]: 5 个源文件的目标文件已过期
updating environment: 5 added, 0 changed, 0 removed
最后一位结果: 4.. [100%] test                                          
查找当前已过期的文件……没有找到
Pickle 序列化环境……完成
检查一致性……/Users/xdthesun/Documents/PlayPython/Project/doc/source/modules.rst: WARNING: document isn't included in any toctree
完成
准备文档……完成
写入输出……[ 20%] function                                                写入输出……[ 40%] index                                                  写入输出……[ 60%] main                                                   写入输出……[ 80%] modules                                                写入输出……[100%] test                                                   
生成索引…… genindex py-modindex
highlighting module code... [100%] function.IDNumber                  
写入附加页面…… search
复制静态文件……done
复制额外文件……完成
导出 Chinese (code: zh) 的搜索索引……完成
导出对象清单……完成
build 成功, 1 warning.
HTML 页面保存在 build/html 目录。

安装主题:

1
pip install sphinx_rtd_theme

安装成功后修改doc/source/conf.py

1
html_theme = 'sphinx_rtd_theme'

在doc目录下再次执行:

1
make html

若有新增加.py文件,则需要删除build下的doctree和html文件夹下的所有内容以及source文件夹中的除index.rst以外的其他所有.rst文件
再次执行:

1
2
3
4
5
6
7
sphinx-apidoc -o ./doc/source ./code  
cd ProjectDoc
make html
```
若只是修改了现有的module,则可以只执行
``` bash
make html
投食