Vai al contenuto

Sphinx Python Documentation

Pubblicato:

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

  • Add the following to the .gitignore to avoid committing useless junk (you could also try adding * for a better result)
# Sphinx documentation
docs/build

Whip out the console

  • Run sphinx-quickstart --sep --ext-autodoc ./docs This will create the docs directory and its structure

Don’t get lost in conf.py

  • In docs/source you will find a conf.py file. Edit the following
# -- 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

  • Run sphinx-apidoc -o docs/source . This will try to find all the modules in your project and generate the corresponding .rts file
  • Make sure the docs/source/index.rts file links to all the other .rts files you want to include in the documentation
  • [Optional] Edit the .rts files in the docs/source directory in a way you like. Nobody will judge you (maybe)
  • Run sphinx-build -b html docs/source docs/build/docs. This will generate the final result in docs/build. You can see it for yourself by opening docs/build/docs/index.html in the browser

Haven’t you used your github page yet?

The manual way

  • Make sure the path docs/build is present in the .gitignore
  • Rename the html folder to docs
  • Create a .nojekyll empty file and put it in the docs directory too (because otherwise the github-page isn’t happy or something like that)
  • [Optional] Create a README.md file to explain what are you doing in this branch and you think this was a good idea
  • Run git worktree add ./docs/build gh-pages to add the worktree to the current repository. It is in fact a repository inside the repository
  • Run cd ./docs/build && git add -A && git commit -m "docs: added documentation" to commit the documentation
  • Push it to github and, in the settings tab of your repository, make sure the github-page is looking for the index.html file in the right folder
  • [optional] Add the newly created github-page to the description of your repository
  • ?
  • Profit

The github action way

  • Add the workflow file in the .git/workflows/ directory

.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