使用sphinx构建文档的第一步

时间:2018-04-05 13:06:41

标签: python python-sphinx

我正在尝试使用sphinx为我的python项目构建文档,但我还不知道该怎么做。

首先我的项目位于此文件夹中:

(scientific_python_2_7) lpuggini@lpuggini-T3420:~/docs$ ls ~/mlp/trunk/
applearning                  clean_only.sh  mlearncnepack     SecurityReport
build_only_docker.sh         collection     mlearn.egg-info   setup.py
build_only.sh                common         peers_peak        transaction_tracking
build.sh                     iplearning     requirements.txt  uba_sam_integration
clean_checkout_and_build.sh  jenkins        scripts           user_behaviour
(scientific_python_2_7) lpuggini@lpuggini-T3420:~/docs$ 

这里的大多数文件都是包含python代码的文件夹。这个项目很大。

按照一些教程,我使用默认设置安装了sphinx。 作为:

(scientific_python_2_7) lpuggini@lpuggini-T3420:~/docs$ sphinx-quickstart 
Welcome to the Sphinx 1.7.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: .

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: mlp
> Author name(s): luca
> Project release []: 

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]: 

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]: 

Sphinx can also add configuration for epub output:
> Do you want to use the epub builder (y/n) [n]: 
Indicate which of the following Sphinx extensions should be enabled:
> autodoc: automatically insert docstrings from modules (y/n) [n]: 
> doctest: automatically test code snippets in doctest blocks (y/n) [n]: 
> 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]: 
> coverage: checks for documentation coverage (y/n) [n]: 
> imgmath: include math, rendered as PNG or SVG images (y/n) [n]: 
> mathjax: include math, rendered in the browser by MathJax (y/n) [n]: 
> 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]: 

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.

(scientific_python_2_7) lpuggini@lpuggini-T3420:~/docs$ 

之后我将conf.py文件的第一行编辑为

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
sys.path.insert(0, os.path.abspath('/home/local/CORVIL/lpuggini/mlp/trunk/'))


# -- Project information -----------------------------------------------------

project = u'mlp'
copyright = u'2018, luca'
author = u'luca'

然后:

(scientific_python_2_7) lpuggini@lpuggini-T3420:~/docs$ make html
Running Sphinx v1.7.2
making output directory...
loading pickled environment... not yet created
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 1 source files that are out of date
updating environment: 1 added, 0 changed, 0 removed
reading sources... [100%] index                                                                          
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] index                                                                           
generating indices... genindex
writing additional pages... search
copying static files... done
copying extra files... done
dumping search index in English (code: en) ... done
dumping object inventory... done
build succeeded.

The HTML pages are in _build/html.

之后,如果我在_build中打开htmls文件,则它们不包含对我的trunk代码的任何引用。

我想知道我错过了什么步骤或者应该做些什么。

1 个答案:

答案 0 :(得分:0)

Sphinx并不像JavaDoc那样工作。你必须实际编写文档。支持使用autodoc扩展从源代码中提取文档,但即便如此,我也不会尝试像JavaDoc一样使用它,而是编写文档并有选择地从源代码中提取文档,以避免在Python源代码和文档源中重复文本。