-
Overview — Sphinx 3.0.0+/f7bf66012 documentation
-
Sphinx is a tool that makes it easy to create intelligent and beautiful documentation, written by Georg Brandl and licensed under the BSD license.
-
It was originally created for the Python documentation, and it has excellent facilities for the documentation of software projects in A RANGE OF LANGUAGES. Of course, this site is also created from reStructuredText sources using Sphinx! The following features should be highlighted:
-
Output formats: HTML (including Windows HTML Help), LaTeX (for printable PDF versions), ePub, Texinfo, manual pages, plain text
-
Extensive CROSS-REFERENCES: semantic markup and AUTOMATIC links for functions, classes, citations, glossary terms and similar pieces of information
若改用 Google Style Docstring,這些還會有嗎 ??
-
Hierarchical structure: easy definition of a DOCUMENT TREE, with automatic links to siblings, parents and children
-
Automatic indices: general index as well as a LANGUAGE-SPECIFIC module indices
-
Code handling: automatic highlighting using the Pygments highlighter
-
Extensions: automatic testing of code snippets, inclusion of docstrings from Python modules (API DOCS), and more
API doc 只是 project documentation 的一部份而已。
-
Contributed extensions: more than 50 extensions contributed by users in a second repository; most of them installable from PyPI
-
-
Sphinx uses reStructuredText as its markup language, and many of its strengths come from the power and straightforwardness of reStructuredText and its parsing and translating suite, the Docutils.
就算 Docstring 改用 Google style 寫,API doc 以外的文件還是得用 reStructuredText 寫? 事實上 Google style 裡的 formatting 也是 reStructuredText。
Examples
- Links to documentation generated with Sphinx can be found on the Projects using Sphinx page.
- For examples of how Sphinx SOURCE FILES look, use the “Show source” links on all pages of the documentation apart from this welcome page.
Hosting
- Need a place to host your Sphinx docs? readthedocs.org hosts a lot of Sphinx docs already, and INTEGRATES well with projects' source control. It also features a powerful BUILT-IN SEARCH that exceeds the possibilities of Sphinx' JAVASCRIPT-BASED OFFLINE SEARCH.
-
- Python 2.7.16 documentation / 3.7.3 Documentation
- Projects using Sphinx — Sphinx 3.0.0+/f7bf66012 documentation
這裡以 Python docstring (用 Google style) 為例,說明如何產生 API doc。專案結構如下:
$ tree -F
.
├── docs/
│ ├── api/
│ └── site/
├── pypkg/
│ └── __init__.py
├── setup.py
└── tests/
└── __init__.py
5 directories, 3 files
$ cat pypkg/__init__.py
def hello(who=None):
"""Says hello to someone.
Args:
who (str): Someone you want to say hello to. Defaults to ``World``
Returns:
str: A greeting.
"""
who = 'World' if who is None else who
return 'Hello, %s!' % who
-
用
sphinx-apidoc產生 Sphinx source 在docs/api,裡面會安排好autodoc:$ sphinx-apidoc -o docs/api --full . setup.py tests/ Creating file docs/api/pypkg.rst. Creating file docs/api/conf.py. Creating file docs/api/index.rst. Creating file docs/api/Makefile. Creating file docs/api/make.bat.其中
. setup.py tests會從當前的目錄找 Python package,但不含setup.py與tests/。 -
在
docs/api/conf.py裡啟用 Napoleon plugin:extensions = [ 'sphinx.ext.autodoc', ... 'sphinx.ext.napoleon', ] -
用
sphinx-build產生 HTML API doc 到build/apidocs/:$ PYTHONPATH=$(PWD) sphinx-build -b html docs/api build/apidocs Running Sphinx v1.8.5 loading translations [en]... done making output directory... building [mo]: targets for 0 po files that are out of date building [html]: targets for 2 source files that are out of date updating environment: 2 added, 0 changed, 0 removed reading sources... [100%] pypkg looking for now-outdated files... none found pickling environment... done checking consistency... done preparing documents... done writing output... [100%] pypkg generating indices... genindex py-modindex highlighting module code... [100%] pypkg 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/apidocs.開啟
build/apidocs/index.html就可以看到 API doc,結果就像:pypkg.hello(who=None) [source] Says hello to someone. Parameters: who (str) – Someone you want to say hello to. Defaults to World Returns: A greeting. Return type: str -
首頁的內容來自
docs/api/index.rst,用 reStructuredText 補上一些敘述性的文字,就更完整了。$ cat docs/api/index.rst .. pylib documentation master file, created by sphinx-quickstart on Sat Jun 1 16:43:25 2019. You can adapt this file completely to your liking, but it should at least contain the root `toctree` directive. Welcome to pylib's documentation! ================================= .. toctree:: :maxdepth: 4 :caption: Contents: pypkg Indices and tables ================== * :ref:`genindex` * :ref:`modindex` * :ref:`search`
參考資料:
-
Getting Started — Sphinx 3.0.0+/f7bf66012 documentation #ril
- Once Sphinx is installed, you can proceed with setting up your first Sphinx project. To ease the process of getting started, Sphinx provides a tool,
sphinx-quickstart, which will generate a documentation source directory and populate it with some defaults. We’re going to use thesphinx-quickstarttool here, though its use is by no means necessary.
Setting up the documentation sources
-
The root directory of a Sphinx collection of reStructuredText document sources is called the SOURCE DIRECTORY. This directory also contains the Sphinx configuration file
conf.py, where you can configure ALL ASPECTS of how Sphinx reads your sources and builds your documentation.This is the usual layout. However,
conf.pycan also live in another directory, the configuration directory. Refer to thesphinx-buildman page for more information. -
Sphinx comes with a script called
sphinx-quickstartthat sets up a source directory and creates a defaultconf.pywith the most useful configuration values from a few questions it asks you. To use this, run:$ sphinx-quickstartAnswer each question asked. Be sure to say “yes” to the
autodocextension, as we will use this later. -
There is also an automatic “API documentation” generator called
sphinx-apidoc; seesphinx-apidocfor details.sphinx-apidoc是自動產生內含autodoc的 Sphinx source (並沒有抄寫 docstring),跟把 Sphinx source 視為 input 的sphinx-build不同。
Defining document structure
-
Let’s assume you’ve run
sphinx-quickstart. It created a source directory withconf.pyand a MASTER DOCUMENT,index.rst(if you accepted the defaults).The main function of the master document is to serve as a WELCOME PAGE, and to contain the root of the “table of contents tree” (or toctree). This is one of the main things that Sphinx ADDS to reStructuredText, a way to CONNECT multiple files to a SINGLE HIERARCHY OF DOCUMENTS.
-
The
toctreedirective initially is empty, and looks like so:.. toctree:: :maxdepth: 2You add documents listing them in the CONTENT of the directive:
.. toctree:: :maxdepth: 2 usage/installation usage/quickstart ...(Directive 的 content 與 options 間會隔一行空白)
-
This is exactly how the toctree for this documentation looks. The documents to include are given as DOCUMENT NAMES, which in short means that you leave off the file name extension and use forward slashes (
/) as directory separators. -
You can now create the files you listed in the toctree and add content, and their SECTION TITLES will be inserted (up to the
maxdepthlevel) at the place where thetoctreedirective is placed. Also, Sphinx now knows about the ORDER and HIERARCHY of your documents.(They may contain
toctreedirectives themselves, which means you can create DEEPLY NESTED HIERARCHIES if necessary.)
Adding content
-
In Sphinx source files, you can use most features of standard reStructuredText. There are also several features ADDED BY SPHINX.
For example, you can add cross-file references in a PORTABLE WAY (which works for all output types) using the
refROLE. ?? -
For an example, if you are viewing the HTML version, you can look at the source for this document – use the “Show Source” link in the sidebar. 這就是 cross-file reference ??
Running the build
-
Now that you have added some files and content, let’s make a first build of the docs. A build is started with the
sphinx-buildprogram:$ sphinx-build -b html sourcedir builddirwhere
sourcediris the source directory, andbuilddiris the directory in which you want to place the built documentation. The-boption selects a BUILDER; in this example Sphinx will build HTML files. -
However,
sphinx-quickstartscript creates aMakefileand amake.batwhich make life even easier for you. These can be executed by runningmakewith the name of the builder. For example.$ make htmlThis will build HTML docs in the build directory you chose. Execute
makewithout an argument to SEE WHICH TARGETS ARE AVAILABLE. -
How do I generate PDF documents?
make latexpdfruns the LaTeX builder and readily invokes the pdfTeX toolchain for you.
Documenting objects
-
One of Sphinx’s main objectives is easy documentation of OBJECTS (in a very general sense) in ANY DOMAIN.
A domain is a collection of object types that belong together, complete with markup to create and reference descriptions of these objects.
-
The most prominent domain is the PYTHON DOMAIN. For example, to document Python’s built-in function
enumerate(), you would add this to one of your source files... py:function:: enumerate(sequence[, start=0]) Return an iterator that yields tuples of an index and an item of the *sequence*. (And so on.)This is rendered like this:
enumerate(sequence[, start=0]) Return an iterator that yields tuples of an index and an item of the sequence. (And so on.)The argument of the directive is the SIGNATURE of the object you describe, the content is the documentation for it. MULTIPLE SIGNATURES can be given, each in its own line.
Domains — Sphinx 3.0.0+/f7bf66012 documentation 除了 Python domain,還有 C domain、C++ domain 等。
這是自己用 reStructuredText directive 寫文件,但如果是寫在 Python source code 裡的 docstring 呢 -->
autodocextension + Napoleon pre-processor。 -
The Python domain also happens to be the DEFAULT DOMAIN, so you don’t need to prefix the markup with the domain name.
.. function:: enumerate(sequence[, start=0]) ...does the same job if you keep the default setting for the default domain.
所以
py:function::中的py:function一樣是 directive type,而py:是 domain;當 domain 是 Python (py:) 時,domain 可以省略。 -
There are several more directives for documenting other types of Python objects, for example
py:classorpy:method.There is also a cross-referencing role for each of these object types. This markup will create a link to the documentation of
enumerate().The :py:func:`enumerate` function can be used for ...And here is the proof: A link to enumerate(). 直接連回上述 "This is rendered like this:" 的地方。
-
Again, the
py:can be left out if the Python domain is the default one. It doesn’t matter which file contains the actual documentation forenumerate(); Sphinx will find it and create a link to it. -
Each domain will have special rules for how the signatures can look like, and make the formatted output look pretty, or add specific features like links to parameter types, e.g. in the C/C++ domains.
more info See Domains for all the available domains and their directives/roles.
- Once Sphinx is installed, you can proceed with setting up your first Sphinx project. To ease the process of getting started, Sphinx provides a tool,
-
An idiot’s guide to Python documentation with Sphinx and ReadTheDocs – Samposium (2016-06-15) #ril
- Basic configuration - Getting Started — Sphinx 3.0.0+/f7bf66012 documentation #ril
- Configuration — Sphinx 3.0.0+/f7bf66012 documentation #ril
-
Autodoc - Getting Started — Sphinx 3.0.0+/f7bf66012 documentation
-
When documenting Python code, it is common to put a lot of documentation in the source files, in documentation strings. Sphinx supports the INCLUSION OF DOCSTRINGS FROM YOUR MODULES with an extension (an extension is a Python module that provides additional features for Sphinx projects) called
autodoc.概念上是把 Python source 的 signature 及 docstring 引到 Sphinx 文件裡來,在 build time 將 API doc 併入一般文件內容。
如果單純是引入 docstring,也就預期 docstring 用 reStructuredText 語法寫;所以就算啟用 Napoleon plugin 可以用 Google style 寫 docstring,Sphinx 實際上拿到的還是即時轉換過的 reStructuredText。
-
In order to use
autodoc, you need to ACTIVATE it inconf.pyby putting the stringsphinx.ext.autodocinto the list assigned to theextensionsconfig value. Then, you have a few additional directives at your disposal. -
For example, to document the function
io.open(), READING its signature and docstring from the source file, you’d write this:.. autofunction:: io.openYou can also document whole classes or even modules automatically, using member options for the auto directives, like
.. automodule:: io :members: -
autodocneeds to IMPORT your modules in order to extract the docstrings. Therefore, you must add the appropriate path tosys.pathin yourconf.py.在
sphinx-quickstart產生的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('.'))所謂 another directory 是指 "modules to document with autodoc" 不在 Sphinx 自己的 source directory 底下,也之所以範例才會用
os.path.abspath('.')。用PYTHONPATH環境變數就不用操作sys.path了 (有點髒)。若 Sphinx +
autodoc在 build time 會走過程式 -- 要能 import 成功,就需要執行在 tox 管理的 virtualenv 裡才行;這表示 Sphinx 無法由事先安裝好的 Docker image 提供。
Warning
-
autodocimports the modules to be documented. If any modules have SIDE EFFECTS on import, these will be executed byautodocwhensphinx-buildis run.本來設定上 import time 就不該做事,否則單元測試也會有問題。
-
If you document SCRIPTS (as opposed to library modules), make sure their
mainroutine is protected by a if__name__ == '__main__'condition. -
more info See
sphinx.ext.autodocfor the complete description of the features ofautodoc.
-
-
Release 1.3b1 (released Oct 10, 2014) - Changes in Sphinx — Sphinx 3.0.0+/f7bf66012 documentation
Added
sphinx.ext.napoleonextension for NumPy and Google style docstring support. -
Overview
-
Are you TIRED of writing docstrings that look like this:
:param path: The path of the file to wrap :type path: str :param field_storage: The :class:`FileStorage` instance to wrap :type field_storage: FileStorage :param temporary: Whether or not to delete the file when the File instance is destructed :type temporary: bool :returns: A buffered writable file descriptor :rtype: BufferedFileStoragereStructuredText is great, but it creates VISUALLY DENSE, HARD TO READ docstrings. Compare the jumble above to the same thing rewritten according to the Google Python Style Guide:
Args: path (str): The path of the file to wrap field_storage (FileStorage): The :class:`FileStorage` instance to wrap temporary (bool): Whether or not to delete the file when the File instance is destructed Returns: BufferedFileStorage: A buffered writable file descriptorMuch more legible, no? 原本用一堆 directive option 來描述,確實很亂!!
-
Napoleon is a extension that enables Sphinx to parse both NumPy and Google style docstrings - the style recommended by Khan Academy. #ril
-
Napoleon is a PRE-PROCESSOR that parses NumPy and Google style docstrings and CONVERTS THEM TO reStructuredText before Sphinx attempts to parse them.
This happens in an intermediate step while Sphinx is processing the documentation, so it doesn’t modify any of the docstrings in your actual source code files.
原來如此,用
autodoc引入 Sphinx 文件的,還是 reStructuredText,只不過是動態將 Google style 轉換的結果。
Getting Started
-
After setting up Sphinx to build your docs, enable napoleon in the Sphinx
conf.pyfile:# conf.py # Add napoleon to the extensions list extensions = ['sphinx.ext.napoleon'] -
Use
sphinx-apidocto build your API documentation:$ sphinx-apidoc -f -o docs/source projectdir若用
sphinx-build則要有一份 Sphinx 文件用autodocdirective 引用 docstring。安排在docs/source,看起來就是要進 version control。jcollado: If you take a look at the contents of
Makefileyou'll see something as follows: ... This means thatmake cleanjust removes the build directory so, with regard to version control, ignoring the contents of the build directory should be enough as you already suspected.
Docstrings
-
Napoleon interprets every docstring that
autodoccan find, including docstrings on: modules, classes, attributes, methods, functions, and variables. -
Inside each docstring, specially formatted Sections are parsed and converted to reStructuredText.
All standard reStructuredText formatting still works as expected.
也就是說,Google style 用比較好讀的編排取代 directive options 的用法,其餘還是 reStructuredText 的用法 (可以是 markdown 嗎 ??);在裡面要如何引用其他 Python object ??
Docstring Sections
-
All of the following section headers are supported:
- Args (alias of Parameters)
- Arguments (alias of Parameters)
- Attention
- Attributes
- Caution
- Danger
- Error
- Example
- Examples
- Hint
- Important
- Keyword Args (alias of Keyword Arguments)
- Keyword Arguments
- Methods
- Note
- Notes
- Other Parameters
- Parameters
- Return (alias of Returns)
- Returns
- Raises
- References
- See Also
- Tip
- Todo
- Warning
- Warnings (alias of Warning)
- Warns
- Yield (alias of Yields)
- Yields
Google vs NumPy
-
Napoleon supports two styles of docstrings: Google and NumPy. The main difference between the two styles is that Google uses INDENTATION to separate sections, whereas NumPy uses UNDERLINES.
Google style:
def func(arg1, arg2): """Summary line. Extended description of function. Args: arg1 (int): Description of arg1 arg2 (str): Description of arg2 Returns: bool: Description of return value """ return TrueNumPy style:
def func(arg1, arg2): """Summary line. Extended description of function. Parameters ---------- arg1 : int Description of arg1 arg2 : str Description of arg2 Returns ------- bool Description of return value """ return True -
NumPy style tends to require MORE VERTICAL SPACE, whereas Google style tends to use more horizontal space. Google style tends to be easier to read for SHORT AND SIMPLE DOCSTRINGS, whereas NumPy style tends be easier to read for LONG AND IN-DEPTH DOCSTRINGS.
The Khan Academy recommends using Google style.
-
The choice between styles is largely aesthetic, but the two styles should not be mixed. Choose one style for your project and be consistent with it.
For complete examples:
因為 Google style 用內縮來區分 section,所以水平空間被壓縮了;就 module docstring 而言,NumPy style 比較好讀,但 function/method docstring 則是 Google style 比較好讀,可惜這兩者不能混用。
Type Annotations
-
PEP 484 introduced a standard way to express types in Python code. This is an alternative to expressing types directly in docstrings. One benefit of expressing types according to PEP 484 is that type checkers and IDEs can take advantage of them for STATIC CODE ANALYSIS.
-
Google style with Python 3 type annotations:
def func(arg1: int, arg2: str) -> bool: """Summary line. Extended description of function. Args: arg1: Description of arg1 arg2: Description of arg2 Returns: Description of return value """ return TrueGoogle style with types in docstrings:
def func(arg1, arg2): """Summary line. Extended description of function. Args: arg1 (int): Description of arg1 arg2 (str): Description of arg2 Returns: bool: Description of return value """ return True做為 library,若要同時滿足 Python 2 & 3,也只好將 type 寫在 docstring 裡。
-
Note: Python 2/3 compatible annotations aren’t currently supported by Sphinx and won’t show up in the docs.
-
-
sphinx-apidoc — Sphinx 3.0.0+/ce3c5735c documentation
Synopsis
sphinx-apidoc [OPTIONS] -o <OUTPUT_PATH> <MODULE_PATH> [EXCLUDE_PATTERN, …]Description
-
sphinx-apidocis a tool for automatic generation of SPHINX SOURCES that, using theautodocextension, document a whole package in the style of other automatic API documentation tools.只是產生 Sphinx source,之後還是得交由
sphinx-build去 build;搭配--full就不用自己寫conf.py。 -
MODULE_PATHis the path to a Python package to document, andOUTPUT_PATHis the directory where the generated sources are placed.這裡 "path to a Python package to document" 的說法有點怪,尤其搭配
-a使用時,會將該路徑加入 Python 的 module search path。所以是 package 的上層,例如path/to/myproj/pypkg/__init__.py,MODULE_PATH應指向path/to/myproj/而非path/to/myproj/pypkg/。Any
EXCLUDE_PATTERNs given are fnmatch-style file and/or directory patterns that will be excluded from generation. -
Warning:
sphinx-apidocgenerates source files that usesphinx.ext.autodocto document all found modules. If any modules have side effects on import, these will be executed by autodoc whensphinx-buildis run.If you document scripts (as opposed to library modules), make sure their
mainroutine is protected by aif __name__ == '__main__'condition.
Options
-
-o <OUTPUT_PATH>Directory to place the output files. If it does not exist, it is created.
-
-f, --forceForce overwriting of any existing generated files.
-
-l, --follow-linksFollow symbolic links.
-
-n, --dry-runDo not create any files.
-
-s <suffix>Suffix for the source files generated. Defaults to
rst. -
-d <MAXDEPTH>Maximum depth for the generated TABLE OF CONTENTS file.
-
--tocfileFilename for a table of contents file. Defaults to
modules. -
-T, --no-tocDo not create a table of contents file. Ignored when
--fullis provided. -
-F, --fullGenerate a full Sphinx project (
conf.py,Makefileetc.) using the same mechanism assphinx-quickstart.對於只想拿 Sphinx 來產生 API doc 而非 project site 時,這很方便 (只有第一次會用到);不過因為跟
sphinx-quickstart的機制一樣,所以產生出來的conf.py也很雜。 -
-e, --separatePut documentation for each module on its own page.
New in version 1.2.
-
-E, --no-headingsDo not create headings for the modules/packages. This is useful, for example, when docstrings already contain headings.
-
-P, --privateInclude
_privatemodules.New in version 1.2.
-
--implicit-namespacesBy default
sphinx-apidocprocessessys.pathsearching for modules only. Python 3.3 introduced PEP 420 implicit namespaces that allow module path structures such asfoo/bar/module.pyorfoo/bar/baz/__init__.py(notice that bar and foo are namespaces, not modules). ??Interpret paths recursively according to PEP-0420.
-
-M, --module-firstPut module documentation BEFORE SUBMODULE documentation.
These options are used when
--fullis specified: -
-aAppend
module_pathtosys.path.搭配
-F, --full時才會有作用,產生的conf.py裡,會直接將MODULE_PATH的 "絕對路徑",一開始就加到sys.path裡,所以實用性不高。例如:sys.path.insert(0, u'/path/to/mylib') -
-H <project>Sets the project name to put in generated files (see project).
-
-A <author>Sets the author name(s) to put in generated files (see copyright).
-
-V <version>Sets the project version to put in generated files (see version).
-
-R <release>Sets the project release to put in generated files (see release).
Environment
-
SPHINX_APIDOC_OPTIONSA comma-separated list of option to append to generated
automoduledirectives. Defaults tomembers,undoc-members,show-inheritance.
-
-
sphinx.ext.autodoc – Include documentation from docstrings — Sphinx 3.0.0+/f7bf66012 documentation #ril
-
Generate documentation — tox 3.12.2.dev5 documentation #ril
It’s possible to generate the projects documentation with tox itself. The advantage of this path is that now generating the documentation can be part of the CI, and whenever any validations/checks/operations fail while generating the documentation you’ll catch it within tox.
-
Running unittest2 and sphinx tests in one go - unittest2, discover and tox — tox 3.12.2.dev6 documentation Sphinx test ?? #ril
-
Markdown — Sphinx 3.0.0+/ce3c5735c documentation
-
Markdown is a lightweight markup language with a simplistic plain text formatting syntax. It exists in many syntactically different flavors. To support Markdown-based documentation, Sphinx can use recommonmark.
recommonmark is a Docutils BRIDGE to CommonMark-py, a Python package for parsing the CommonMark Markdown flavor.
Configuration
-
To configure your Sphinx project for Markdown support, proceed as follows:
-
Install the Markdown parser
recommonmark:pip install --upgrade recommonmarkNote: The configuration as explained here requires recommonmark version 0.5.0 or later.
-
Add
recommonmarkto the list of configured extensions:extensions = ['recommonmark']Changed in version 1.8: Version 1.8 deprecates and version 3.0 removes the
source_parsersconfiguration variable that was used by older recommonmark versions. -
If you want to use Markdown files with extensions other than
.md, adjust thesource_suffixvariable. The following example configures Sphinx to parse all files with the extensions.mdand.txtas Markdown:source_suffix = { '.rst': 'restructuredtext', '.txt': 'markdown', '.md': 'markdown', } -
You can further configure recommonmark to allow custom syntax that standard CommonMark doesn’t support. Read more in the recommonmark documentation.
-
-
Installation from PyPI - Installing Sphinx — Sphinx 3.0.0+/f7bf66012 documentation
-
Sphinx packages are published on the Python Package Index. The preferred tool for installing packages from PyPI is
pip. This tool is provided with all modern versions of Python. -
On Linux or MacOS, you should open your terminal and run the following command.
$ pip install -U sphinxOn Windows, you should open Command Prompt (⊞Win-r and type
cmd) and run the same command.C:\> pip install -U sphinx -
After installation, type
sphinx-build --versionon the command prompt. If everything worked fine, you will see the version number for the Sphinx package you just installed.
-
社群:
相關:
- Sphinx 的語法是基於 reStructuredText 再進行擴充。
手冊: