Instructions to build documentation from this repository¶
Clone this repository using
git clone https://git.opendfki.de/dinesh_krishna.natarajan/sphinx-doc-tutorial.git
Install the following packages using
pip.pip install sphinx pip install sphinx_rtd_theme pip install recommonmark
pandocis needed to generate documentation from Jupyter Notebooks using Sphinx. Installation instructions can be found here.Navigate to
sphinx-doc-tutorial/docs/directory and use themakecommand to generate HTML and LaTeX documentation.cd sphinx-doc-tutorial/docs/ make html make latexpdf
The generated documentation can be found in the
docs/build/html/anddocs/build/latex/directories.An online demo of the generated HTML docs and LaTeX docs.
Instructions to generate Python documentation using Sphinx¶
To create your own documentation from scratch, follow the instructions below.
Step 1: Create documented code¶
Create scripts and modules in Python.
For automatic docstring generation in VSCode, use Python Docstring Generator plugin with appropriate convention.
Example docstrings following the sphinx convention is shown below:
def add( a, b ): """ This function computes the sum of the two arguments. :param num1: first argument :type num1: float :param num2: second argument :type num2: float :return: sum of the two arguments :rtype: int or float .. note:: This function can accept :class:`int` parameters too. Example:: result = add(a,b) """ assert type(a) == type(b) return a + b
Note: For other docstring conventions such as the Google Python Style Guide, additional extensions such as
sphinx.ext.napoleonare necessary.Different docstring conventions can be used in the same project and Sphinx (and its extensions) will parse them to generate documentation with a uniform convention.
Step 2: Setup sphinx project¶
Install
sphinxpackage using pip, if not already installed.From your code’s parent directory, create a
docs/sub-directory to build the documentation files.In the
docs/directory, initiate the sphinx project usingsphinx-quickstartwith the default options for the prompts as shown below.Separating source and build directories keeps the
docs/directory structured.$ sphinx-quickstart Welcome to the Sphinx 4.0.3 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]: y The project name will occur in several places in the built documentation. > Project name: Sphinx Doc Tutorial > Author name(s): Dinesh Krishna Natarajan > Project release []: 0.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 https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-language. > Project language [en]: en Creating file sphinx-doc-tutorial/docs/source/conf.py. Creating file sphinx-doc-tutorial/docs/source/index.rst. Creating file sphinx-doc-tutorial/docs/Makefile. Creating file sphinx-doc-tutorial/docs/make.bat. Finished: An initial directory structure has been created. You should now populate your master file sphinx-doc-tutorial/docs/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.
Step 3: Configure sphinx documentation builder¶
In the
docs/source/conf.pyfile, uncomment the path setup comments shown below and edit the absolute path to the modules directory of the project.# -- 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('../../utils'))
In the
docs/source/conf.pyfile, add ‘sphinx.ext.autodoc’ and other necessary extensions to the extensions list.extensions = [ 'sphinx.ext.autodoc', # to autogenerate .rst files 'sphinx.ext.napoleon', # to parse google stye python docstrings 'sphinx.ext.mathjax', # to include math expressions in the .rst files 'recommonmark', # to include markdown files in sphinx documentation 'nbsphinx' # to include jupyter notebooks ]
In the same file, change the html theme if necessary. Note: The default installed theme is ‘alabaster’. Any other theme might have to be installed via
pip install theme_name. Available sphinx themes can be found here.html_theme = 'sphinx_rtd_theme'
Step 4: Configure the .rst files¶
The ‘reStructured Text’ files indicate the contents of the documentation. Here is a primer for the reStructuredText markup language.
The
index.rstacts as the main file (equivalent to a Latex main file). The contents of the documentation can be added directly to theindex.rstfile or individual.rstfiles can be created and then referenced in theindex.rstfile. Each.rstfile gets its own webpage.Documentation for the modules present in the
utils/arithmetic.pyandutils/operations.pycan be automatically generated using thesphinx-apidoccommand. From thedocs/directory, the following command can be used to automatically generate the.rstfiles for the modules inutils/directory. The-oargument points to the output directorysource/.To recursively generate the
.rstfiles for the submodules inside theutils/folder, simply create an__init__.pyfile inside the submodule directories. The-Moption lists the modules before the submodules.$ sphinx-apidoc -M -o docs/source/ utils/ Creating file docs/source/arithmetic.rst. Creating file docs/source/operations.rst. Creating file docs/source/submodule.rst. Creating file docs/source/modules.rst.
The
modules.rstfile includes a Table of Contents which lists all the python scripts in theutils/directory. Each python script leads to an individual.rstfile for that script.utils ===== .. toctree:: :maxdepth: 4 arithmetic operations submodule
The generated
arithmetic.rstfile contains:.. automodule:: arithmetic :members: :undoc-members: :show-inheritance:
For customization of the various
autodoccommands, refer to its documentation. The__init__function is excluded by default, but can be added using the automodule option::special-members: __init__
In the
.rstfiles, additional descriptive text can be directly added without any commands to give more context to the documentation. Images can also be added using theimagecommand. Additional options for the command can be found here... image:: images/image.png :alt: some useful image :width: 300px
The
modules.rstis then included in the parentindex.rstusing theincludecommand... include modules.rst
An alternative to using the
includecommand is to add the names of the.rstfiles (without extension) to the toctree inindex.rst. This method also has the benefit that each.rstfile has an entry (with the title mentioned in its.rstfile) in the Table of Contents with a hyperlink to the module’s individual webpage... toctree:: modules :maxdepth: 2 :caption: Contents
Including math expressions in the
.rstfiles is handled by the extensionsphinx.ext.mathjax. It has to be added to the extensions list inconf.py.Syntax for inline math:
:math:`x^2 + y^2 = z^2`
Symntax for math equations:
.. math:: x^2 + y^2 = z^2 \\ x^2 + y^2 = z^2
In order to include markdown files into the documentation, an extension named
recommonmarkis required. It has to be added to the extensions list inconf.py.pip install recommonmark
Now, any markdown files from the
source/directory can be added to the documentation by including the file in the toctree ofindex.rst... toctree:: Instructions <../README.md> modules :maxdepth: 2 :caption: Contents
Jupyter Notebooks can be added using a Sphinx extension called
nbsphinx. An.rstfile for the notebook can be created to import the contents and also add any descriptive text.Notebooks from the project directory / module directory could not be added using relative path. A solution has to be found. The easiest solution was to place the notebooks were placed inside the
docs/source/directory.In the
.rstfile for the notebook, add the following to create a table of contents linking to the contents of the Jupyter notebook... toctree:: :caption: Contents :maxdepth: 1 notebooks/demo
Step 5: Build the documentation in HTML and/or Latex¶
From the
docs/directory, the documentation can be built using themake buildercommand where the builder is eitherhtmlorlatexorlatexpdf.For HTML, the options are automatically generated in
docs/source/conf.pyby thesphinx-quickstartcommand.make html
LaTeX documentation can be automatically generated without any additions to the
conf.pyfile. If customization is necessary, the Latex options can be added to theconf.pyfile. Example for LaTeX options for customizations can be found here.make latexpdf
The generated HTML or Latex files can be found in
docs/build/htmlordocs/build/latexrespectively.The HTML documentation can be viewed locally via the
docs/build/html/index.htmlfile. The individual webpage can also be found as.htmlfiles.The Latex documentation can be viewed via the
docs/build/latex/[project_name].pdffile.