Including API documentation in docs built with Jupyter Book

The title is chock full of jargon. Let me break it down.

API stands for Application Programming Interface. An API is the user-facing part of a software package. For example, if you use the "numpy" package, "numpy.array" is an API. And APIs need to be documented. A single API can produce different results depending on the inputs so documenting the full capabilities of an API is important.

Jupyter Book is a new tool in the Scientific Python community to create and upload documentation easily. The community as a whole seems to be working towards better documenting their projects and some have chosen to go with Jupyter Book instead of relying on Sphinx.

I personally don't know why the community felt the need to move away from vanilla rst (reStructureText) and Sphinx. One of the advantages of Sphinx is the fact that API documentation for the package can be created automatically (using sphinx-apidoc) and the API docs can be included in the user-facing documentation. This is fairly common practice in the Python community in general. See API documentation in Traits and TraitsUI.

I was recently working with a package that uses Jupyter Book for their documentation needs and what stood out to me was the fact that they were hosting their API documentation on their GitHub Wiki page. They had manually documented and formatted the API (functions) in the wiki page and linked to sections of the wiki page from the Jupyter Book docs where relevant. The formatting of the documentation on the wiki page wasn't ideal so I initially wanted to improve that. But, when I took a step back to look at the big picture, the fact the documentation was linking to the GitHub wiki page felt off to me. So I decided to look into how API documentation can be linked in docs built using Jupyter Book. It's not like I have anything better to do this weekend.

In the end, the solution is non-trivial but it's definitely possible, using the "eval-rst" MyST directive.

  1. Manually create an rst file that uses the "autofunction" Sphinx directive to autogenerate the API document for a function of interest
  2. Use the "eval-rst" MyST directive to include the above rst file into the Jupyter Book markdown docs
  3. Install the package containing the function of interest in the Python environment before building the Jupyter Book documentation

It took a lot of trial and error to get here. I initially tried directly using the "autofunction" sphinx directive within a "eval-rst" MyST directive but that didn't work. Then I thought maybe I could autogenerate the API docs using Sphinx and then include those files in the documentation. This was a step in the right direction, which eventually led to the short and sweet 3 steps you see above. All in all, I think it took me around 6 hours to look into the possibilities, try out a few different things, come across on a solution and refine it to remove unnecessary pieces.

If you know of a cleaner, simpler, better solution, I would love to know!

For a concrete example, see the following Pull Request on GitHub. Now we wait and see if the maintainers of the GitHub repository are open to the changes I introduced in that PR.

Popular posts from this blog

Animation using GNUPlot

Thoughts on the National Deep Tech Startup Policy

Arxiv author affiliations using Python