sphinx是一种基于Python的文档工具,它能够使人轻松的撰写出清晰且优美的文档,由Georg Brandl在BSD许可证下开发。
新版的Python3文档就是由sphinx生成的,而且它已成为Python项目首选的文档工具,同时它对C/C++项目也有很好的支持。
更多详细特性请参考spinx官方文档html
新建一个项目
目录结构以下,
doc目录使用来存放API文档,
src目录是用来存放项目的源码python
主要使用了两种不一样的Python注释分格。对于简单的例子和简单的函数以及文档说明, 使用google style显得更为简洁, 而对于比较复杂详细的文档说明numpy style更为流行。
#coding=UTF-8 class Demo1(): """类的功能说明""" def add(self,a,b): """两个数字相加,并返回结果""" return a+b def google_style(arg1, arg2): """函数功能. 函数功能说明. Args: arg1 (int): arg1的参数说明 arg2 (str): arg2的参数说明 Returns: bool: 返回值说明 """ return True def numpy_style(arg1, arg2): """函数功能. 函数功能说明. Parameters ---------- arg1 : int arg1的参数说明 arg2 : str arg2的参数说明 Returns ------- bool 返回值说明 """ return True
注释看起来像Python命令行输入的文档字符串, 主要是用来检查命令输出是否匹配下行的内容, 它容许开发人员在源码中嵌入真实的示例和函数的用法,还能确保代码被测试和工做。
#coding=UTF-8 def my_function(a, b): """函数功能说明 >>> my_function(2, 3) 6 >>> my_function('a', 3) 'aaa' """ return a * b
使用sphinx创建API文档项目git
进入到doc目录下github
cd 项目路径/docapi
> Root path for the documentation [.]: sphinx_demo > Separate source and build directories (y/n) [n]: y > Name prefix for templates and static dir [_]: > Project name: sphinx_demo > Author name(s): sphinx demo > Project version []: 1.0 > Project release [1.0]: > Project language [en]: zh_CN > Source file suffix [.rst]: > Name of your master document (without suffix) [index]: > Do you want to use the epub builder (y/n) [n]: > 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]: > viewcode: include links to the source code of documented Python objects (y/n) [n]: > githubpages: create .nojekyll file to publish the document on GitHub pages (y/n) [n]: > Create Makefile? (y/n) [y]: > Create Windows command file? (y/n) [y]:
由于咱们须要从Python代码的注释中自动导出API文档, 因此须要将autodoc: automatically insert docstrings from modules (y/n) [n]: y 若是忘记设置,能够在conf.py中的extensions中添加'sphinx.ext.autodoc'。 选项后面没有输入的,直接按回车键使用默认设置。 选项后面有输入的,按照个人设置便可, 若是不使用中文文档,能够在language配置中使用默认设置。 设置完成以后,能够看到以下的目录结构
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.doctest', 'sphinx.ext.intersphinx', 'sphinx.ext.todo', 'sphinx.ext.coverage', 'sphinx.ext.mathjax']
经过设置conf.py中的extensions,能够为sphinx添加额外的扩展, 若是想要将html文档转换为PDF,只须要先安装扩展,而后再此处添加便可使用。 因为咱们的注释代码主要同时支持google style和numpy style,因此咱们须要添加一个扩展来支持。
sphinx.ext.napoleon
为源码生成html文件函数
import os import sys sys.path.insert(0, os.path.abspath('../../../src')) # 指向src目录
sphinx-apidoc -o sphinx_demo/source ../src/ >Creating file sphinx_demo/source\demo1.rst. >Creating file sphinx_demo/source\demo2.rst. >Creating file sphinx_demo/source\modules.rst.
清理文件工具
cd sphinx_demo make clean >Removing everything under 'build'...
生成html文件测试
make html // 请确保这一步没有输出error和exception
打开build/html/index.htmlui
修改API的主题
打开source/conf.py文件,找到html_theme = 'alabaster',修改便可,sphinx官方提供了几种主题能够进行选择,sphinx主题设置google
SyntaxError:Non-ASCII character '\xba' in file .....py
在*.py文件的第一行添加#coding=UTF-8
Encoding error:'utf8' codec can't decode byte 0xc0 in position 44:invalid start byte
确保*.py文件的编码格式为utf-8,经过notepad++能够进行查看,若是不是请修改成utf-8格式
添加sphinx.ext.napoleon后报Exception occurred ....return translator['sphinx'].ugettext(message) KeyError:'sphinx'
Sphinx1.3,napoleon扩展使用sphinx.ext.napoleon,Sphinx <= 1.2使用sphinxcontrib.napoleon