25日

使用sphinx为python项目生成API及在readthedocs上的配置

备注

整个项目的根目录中不要有init.py文件,否则API文档不能自动生成!

conf.py中的配置

 1# Configuration file for the Sphinx documentation builder.
 2#
 3# For the full list of built-in configuration values, see the documentation:
 4# https://www.sphinx-doc.org/en/master/usage/configuration.html
 5
 6# -- Path setup --------------------------------------------------------------
 7
 8# If extensions (or modules to document with autodoc) are in another directory,
 9# add these directories to sys.path here. If the directory is relative to the
10# documentation root, use os.path.abspath to make it absolute, like shown here.
11#
12
13import os
14import sys
15
16current_dir = os.path.dirname(__file__)
17target_dir = os.path.abspath(os.path.join(current_dir, "../.."))
18sys.path.insert(0, target_dir)
19
20import pk4adi
21
22# -- Project information -----------------------------------------------------
23# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
24
25project = 'PK4ADI'
26copyright = '2024, Zhejiang University'
27author = 'Silence Jiang'
28release = pk4adi.__version__
29
30# -- General configuration ---------------------------------------------------
31# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
32
33extensions = [
34    'autoapi.extension',
35    'recommonmark',
36    'sphinx.ext.autodoc',
37    'sphinx.ext.todo',
38    'sphinx.ext.viewcode',
39    'sphinx.ext.napoleon',
40    'sphinx.ext.githubpages',
41    'sphinx.ext.intersphinx']
42
43
44templates_path = ['_templates']
45exclude_patterns = []
46
47# configuration for 'autoapi.extension'
48# autoapi_type = 'python'
49autoapi_dirs = ['../../pk4adi']
50# autoapi_template_dir = './_autoapi_templates'
51autoapi_generate_api_docs = True
52add_module_names = False  # makes Sphinx render package.module.Class as Class
53
54# Napoleon settings
55napoleon_google_docstring = True
56napoleon_numpy_docstring = True
57
58# Add more mapping for 'sphinx.ext.intersphinx'
59intersphinx_mapping = {'python': ('https://docs.python.org/3', None),
60                    'numpy': ('https://numpy.org/doc/stable/', None),
61                    'pandas': ('https://pandas.pydata.org/docs/', None),
62                    'scipy': ('https://docs.scipy.org/doc/scipy/', None),
63                    }
64
65# -- Options for locale output -------------------------------------------------
66
67# multi-language docs
68language = 'en'
69locale_dirs = ['../locales/']   # path is example but recommended.
70gettext_compact = False     # optional.
71gettext_uuid = True     # optional
72
73# -- Options for HTML output -------------------------------------------------
74# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
75
76html_theme = 'furo'
77html_static_path = ['_static']

在上述文件中:

  1. 6-20行将需要生成API的源文件加入路径

  2. 28行将文档版本号与代码版本号关联

  3. 34行使用自动化插件,参见 autoapi文档

  4. 39行使用格式插件

  5. 41行使用引用其他工程文档的插件

  6. 47-56行配置自动化插件,包括设置代码源文件的相对路径

  7. 58-63行配置需要引用的其他工程文档的网址

在readthedocs上的配置

为使readthedocs正确生成项目的API,编译之前还需在`.readthedocs.yaml`文件中配置好项目依赖。而依赖一般位于python项目的 'requirements.txt'文件中。具体如下:

29python:
30    install:
31    - requirements: docs/requirements.txt
32    - requirements: requirements.txt

备注

区分python项目的依赖文件与sphinx项目的依赖文件!

win下设置python的源

命令行下运行下面的命令,结果会写入'C:UsersSilenceAppDataRoamingpippip.ini'文件中。

pip config set global.index-url https://pypi.mirrors.ustc.edu.cn/simple/