Skip to content

Sphinx Python Documentation

Published:

Fast and absolutely NOT exhaustive “guide” on the steps needed to document a python project with sphinx like a ⛺️. In fact, this whole “guide” is meant to remember me how to document a python project.

This “guide” assumes that your code is already documented through comments. If not, you can still use this “guide”, but you will have to do some work editing those .rts files…

Also, it is always assumed that your current working directory is the root directory of the project

Let’s start with pip3

pip3 install sphinx sphinx_rtd_theme sphinx-rtd-dark-mode sphinx_autodoc_typehints

Edit the .gitignore

# Sphinx documentation
docs/build

Whip out the console

Don’t get lost in 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('../..'))  # path to the actual project root folder
# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    'sphinx.ext.autodoc',  # read documentation from the comments
    'sphinx.ext.napoleon',  # to use NumPy and Google style docstrings
    'sphinx.ext.githubpages',  # generates the .nojekyll file
    'sphinx.ext.viewcode',  # add source code links to the documentation
    'sphinx_rtd_dark_mode',  # dark mode for ReadTheDocs
    'sphinx_autodoc_typehints',  # improves the type hinting
]
# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'  # [optional, to use the far superior Read the Docs theme]

If you want to use the dark theme provided in this “guide”, add these lines and insert the two css files in the _static/css folder.
It would be nice to have an image in the _static/img/logo.jpg

css/dark.css

html[data-theme="dark"].writer-html4
  .rst-content
  dl:not(.docutils)
  .descclassname,
html[data-theme="dark"].writer-html4 .rst-content dl:not(.docutils) .descname,
html[data-theme="dark"].writer-html4 .rst-content dl:not(.docutils) .sig-name,
html[data-theme="dark"].writer-html5
  .rst-content
  dl[class]:not(.option-list):not(.field-list):not(.footnote):not(
    .glossary
  ):not(.simple)
  .descclassname,
html[data-theme="dark"].writer-html5
  .rst-content
  dl[class]:not(.option-list):not(.field-list):not(.footnote):not(
    .glossary
  ):not(.simple)
  .descname,
html[data-theme="dark"].writer-html5
  .rst-content
  dl[class]:not(.option-list):not(.field-list):not(.footnote):not(
    .glossary
  ):not(.simple)
  .sig-name {
  color: white;
}
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
html_css_files = [
    'css/dark.css',
]

html_logo = "_static/img/logo.jpg"

Finally, to make your extensions do the dirty work for you, add some good old settings.

# -- Extension configuration -------------------------------------------------

# Configuration of "sphinx_autodoc_typehints"
typehints_use_rtype = False
typehints_defaults = "comma"

RST vs Markdown

For some reason I prefer not to know, Sphinx uses rst format instead of markdown.
This becomes soon a pain when the vscode comment parser uses markdown instead. To make the comments look good in both situations with minimum effort and code duplication, I found the following workaround.
In the config.py file, add the following.

# -- Extension configuration -------------------------------------------------

# Configuration of "sphinx_autodoc_typehints"

def setup(app):
    from sphinx.ext.autodoc import between

    app.connect("autodoc-process-docstring", between("```", exclude=True)) # ignore markdown code blocks
    app.connect("autodoc-process-docstring", between(r"\|(\s*\S+\s*\|)+", exclude=True)) # ignore markdown tables

This way you are free to use the Markdown syntax for the VsCode editor, while being able to render code blocks and tables in the documentation. The only downside is that the comment itself will contain both the rst and the markdown syntax.

Below you can find a sample for the rts style comment for code blocks and tables.

"""
.. code-block:: python

    print("Hello World")

.. table:: Example table

    === === =====
    a   b   sum
    === === =====
    0   0    0
    0   1    1
    === === =====
"""

The show must go on

Haven’t you used your github page yet?

The manual way

The github action way

.git/workflows/documentation.yml

name: Deploy Sphinx Documentation

on:
  push:
    branches: [master, develop]
    paths-ignore:
      - "README.md"
      - "docs/**"

jobs:
  doc:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - name: Set up Python 3.9.0
        uses: actions/setup-python@v2
        with:
          python-version: 3.9.0
      - name: Install dependencies for requirements and testing
        run: |
          python -m pip install --upgrade pip
          pip install sphinx sphinx_rtd_theme sphinx-rtd-dark-mode sphinx_autodoc_typehints
          if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
          if [ -f requirements_dev.txt ]; then pip install -r requirements_dev.txt; fi
      #   - name: Setup environment
      #     run: |
      #       setup
      - name: Sphinx build
        run: |
          sphinx-build -b html docs/source docs/build/docs
      - name: Deploy to GitHub Pages
        uses: JamesIves/github-pages-deploy-action@4.1.0
        with:
          branch: gh-pages
          folder: docs/build
          clean-exclude: |
            README.md

Now you will have a professional looking documentation page that everyone (mostly you from the future) can consult