Documentation is awesome. You should write it. Here’s how.
All the CommCare HQ docs are stored in a
docs/ folder in the root of the repo.
To add a new doc, make an appropriately-named rst file in the
For the doc to appear in the table of contents, add it to the
toctree list in
Sooner or later we’ll probably want to organize the docs into sub-directories,
that’s fine, you can link to specific locations like so:
Sphinx builds the documentation and extends the functionality of rst a bit for stuff like pointing to other files and modules.
To build a local copy of the docs (useful for testing changes), navigate to the
docs/ directory and run
make html. Open
<path_to_commcare-hq>/docs/_build/html/index.html in your browser and you
should have access to the docs for your current version (I bookmarked it on my
Read the Docs
Dimagi maintains the hosted version of the documentation at readthedocs.io. For Dimagi employees, the credentials are maintained in our internal password manager under the “readthedocs” entry.
The configuration for Read the Docs lives in
.readthedocs.yml, which calls the
Due to problematic dependencies that need to be mocked, we cannot properly setup django apps until after
docs/conf.py has been applied. We then must be aware that we are performing a docs build, at which point we can run
corehq/__init__.py. We use an environment variable (DOCS_BUILD) to convey this information,
which is set in the Admin UI of our readthedocs.io account.
The docs are built with every new merge to master. This build can fail
completely, or “succeed” with errors. If you made a change that’s not appearing,
autodoc doesn’t seem to be working properly, you should check the build.
On Read the Docs, in the bottom left, you should see “v: latest”. Click to expand, then click “Builds”. There you should see a build history (you don’t need to log in for this). Click on the latest build. I find the “view raw” display to be more useful. That should show logs and any tracebacks.
automodule requires that sphinx be able to load the
code to import docstrings. This means that ~all of the source code’s
requirements to be installed, and the code cannot do complex stuff like database
queries on module load. Build failures are likely caused by issues there.
Replicating the build environment
Read the Docs builds in an environment that doesn’t have any support services, so turn those off. Next, make a new virtual environment with just the docs requirements. Finally, build the docs, which should surface any errors that’d appear on the build server.
$ cd commcare-hq/
$ mkvirtualenv --python=python3.9 hq-docs
$ pip install -r requirements/docs-requirements.txt
$ cd docs/
$ make html
Use short sentences and paragraphs
Break your documentation into sections to avoid text walls
Avoid making assumptions about your reader’s background knowledge
Consider three types of documentation:
Tutorials - quick introduction to the basics
Topical Guides - comprehensive overview of the project; everything but the dirty details
Reference Material - complete reference for the API
One aspect that Kaplan-Moss doesn’t mention explicitly (other than advising us to “Omit fluff” in his Technical style piece) but is clear from both his documentation series and the Django documentation, is what not to write. It’s an important aspect of the readability of any written work, but has other implications when it comes to technical writing.
Antoine de Saint Exupéry wrote, “… perfection is attained not when there is nothing more to add, but when there is nothing more to remove.”
Keep things short and take stuff out where possible. It can help to get your point across, but, maybe more importantly with documentation, means there is less that needs to change when the codebase changes.
Think of it as an extension of the DRY principle.
reStructuredText is a markup language that is commonly used for Python documentation. You can view the source of this document or any other to get an idea of how to do stuff (this document has hidden comments). Here are some useful links for more detail:
While you can use any text editor for editing RestructuredText documents, I find two particularly useful:
PyCharm (or other JetBrains IDE, like IntelliJ), which has great syntax highlighting and linting.
Sublime Text, which has a useful plugin for hard-wrapping lines called Sublime Wrap Plus. Hard-wrapped lines make documentation easy to read in a console, or editor that doesn’t soft-wrap lines (i.e. most code editors).
Vim has a command
gqto reflow a block of text (
:help gq). It uses the value of
textwidthto wrap (
:setl tw=75). Also check out
:help autoformat. Syntastic has a rst linter. To make a line a header, just
yypVr=(or whatever symbol you want).
Some basic examples adapted from 2 Scoops of Django:
Sections are explained well here
Simple link: http://commcarehq.org
Inline link: CommCare HQ
Fancier Link: CommCare HQ
An enumerated list item
- Second bullet
Note carriage return and indents
Literal code block:
print("I like Ice Cream")
for i in range(10):
Python colored code block (requires pygments):
# You need to "pip install pygments" to make this work.
for i in range(10):
console.log("Don't use alert()");