Motivation
Do you document your code? Do you think it is important?
Benefits of documenting your code:
- if you will get back to your code in 6 month, you will not become a detective to find out how this code works
- easy to onboard new team members
- easy to share knowledge
- if your code is open source - easy to start contributing
- easy to see purpose and motivation of each piece of code
I love this quote from The Documentation System:
There is a secret that needs to be understood in order to write good software documentation: there isn’t one thing called documentation, there are four.
They are: tutorials, how-to guides, technical reference and explanation. They represent four different purposes or functions, and require four different approaches to their creation. Understanding the implications of this will help improve most documentation - often immensely.
How to start documenting your code?
Start easy with Sphinx and then go to version controlled docs with Read The Docs
I would suggest first check official Sphinx tutorial and than add more features into your setup.
Let's take a look into basic setup. You can find all examples of how to setup your Sphinx docs in Demo Project I have created.
Basic Sphinx setup:
- add Sphinx and recommonmark (Markdown support into your requirements file)
- install Sphinx package
pip install sphinx
- create a new docs directory in your project
mkdir docs
- go to docs directory
cd docs
- follow basic Sphinx setup by running
sphinx-quickstart
And now edit your docs Makefile by adding commands at the end of the file:
show:
open _build/html/index.html
docs:
rm -rf source/code_reference
make clean
make html
After this modification we will be able to run commands:
make show
- to open our documentation (this command works on mac only)make docs
- to cleanup and rebuild our docs
It's still too early to run build, we need to setup our configuration to make sure that we are able to generate code_reference
.
Next step - edit your Sphinx documentation configuration file conf.py
:
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- 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
from sphinx.ext import apidoc
import sys
from pathlib import Path
current_dir = Path(__file__).parent.absolute()
code_dir = current_dir.parents[1] / "app"
sys.path.insert(0, os.path.abspath('../../app'))
# -- Project information -----------------------------------------------------
project = 'SimpleDocTutor'
copyright = '2020, Anastasiia Tymoshchuk'
author = 'Anastasiia Tymoshchuk'
# The full version, including alpha/beta/rc tags
release = '0.1'
# -- 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.napoleon",
"sphinx.ext.autodoc",
"recommonmark",
]
source_suffix = [".rst", ".md"]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
# -- 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 = 'alabaster'
# 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']
def run_apidoc(_):
exclude = []
argv = [
"--doc-project",
"Code Reference",
"-M",
"-f",
"-d",
"3",
"--tocfile",
"index",
"-o",
str(current_dir / "code_reference"),
str(code_dir),
] + exclude
apidoc.main(argv)
def setup(app):
app.connect("builder-inited", run_apidoc)
In this configuration you can see run_apidoc
command, which will run apidoc to generate your code reference auto
documentation and will store all generated files in code_reference
directory.
And one more step to go - add generated code_reference
into our documentation in index.rst
file:
.. SimpleDocTutor documentation master file, created by
sphinx-quickstart on Mon May 11 23:46:38 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to SimpleDocTutor's documentation!
=============================================
.. toctree::
:maxdepth: 2
code_reference/index.rst
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
Don't forget to put all dependencies into requirements.txt file:
Sphinx==3.0.3
m2r==0.2.1
sphinx-autodoc-typehints==1.10.3
recommonmark~=0.6.0
And we are finished with the simple setup. Now you can run in your terminal (your current directory should be docs
:
make docs
make show
Read The docs setup
Next step is to setup our continuous documentation. In order to add your github repo into readthedocs.org, you need to be administrator of this repo and it has to be public. If you would like to setup private documentation for your private repo - you can use Read The Docs for Business.
I will explain how to setup free open source solution from Read The Docs. First of all register on readthedocs.org, then add your repo on Projects page and add readthedocs.yaml into root of your repo:
# .readthedocs.yml
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
# Required
version: 2
# Build documentation in the docs/ directory with Sphinx
sphinx:
configuration: docs/source/conf.py
# Build documentation with MkDocs
#mkdocs:
# configuration: mkdocs.yml
# Optionally build your docs in additional formats such as PDF and ePub
formats: all
# Optionally set the version of Python and requirements required to build your docs
python:
version: 3.7
install:
- requirements: docs/requirements.txt
Then go to setup of your project on readthedocs.org
-> Admin -> Advanced Settings -> Default branch.
Set your working branch for testing your setup before pushing to main branch. After you applied changes,
it will trigger a new build from this branch. Go to Builds and check status.
Enjoy your documentation ;-)