Generating API
==============
You now have some documentation and code. However, you may want to provide
an API (application programming interface) to some code you have been
working on. There are several ways to
do this, but this guide focuses on Sphinx extensions. Alternatively,
you can use third-party extensions such as
`Sphinx AutoAPI `_.
Sphinx has tools for automatically generating API documentation. One
example of this is the ``autodoc`` extension. Here is an example for
how to use it.
.. note::
You will need to write docstrings in reStructuredText format, or see
the next chapter for how to use the ``napoleon`` extension.
Add the following extensions to ``docs/conf.py``.
.. code-block:: python
:caption: conf.py
extensions = [
"sphinx.ext.autodoc",
]
You can get creative here, but I suggest adding your packages,
subpackages, and submodules to an ``api`` directory. Then, for
each package, subpackage, and submodule, create a new RST file.
For example::
cd docs
echo "API\n===" > api.rst
mkdir api; cd api
Then, create a file and directory for ````::
echo "\n============" > .rst
mkdir ; cd
Next, create a file for ````::
echo ".\n=============================" > .rst
The contents of ``.rst`` could be something like this,
.. code-block:: rst
:caption: /.rst
.
===============================
.. automodule:: .
:members:
where ``automodule`` automatically parses ``.``
and generates the API documentation from the docstrings.
Then, we do the same for packages (and subpackages) but follow it with a TOC
for submodules and subpackages.
.. code-block:: rst
:caption: .rst
==============
.. automodule::
:members:
.. toctree::
:caption: Submodules:
/
here ``automodule`` parses ``/__init__.py`` for docstrings.
Now add a TOC to the file ``docs/api.rst`` to contain all your parent packages
and modules,
.. code-block:: rst
:caption: api.rst
API
===
.. toctree::
:caption: Packages:
api/
Finally, add the API file to your TOC in ``docs/index.rst``,
.. code-block:: rst
:caption: index.rst
.. toctree::
:maxdepth: 2
user-guide
api
Then, clean and build the documentation,
.. code-block::
make clean; make html
However, you may see the following warning:
.. code-block::
WARNING: autodoc: failed to import module ''; the following exception was raised:
No module named ''
WARNING: autodoc: failed to import module '' from module ''; the following exception was raised:
No module named ''
This is because you need to :ref:`install your code locally ` for
``autodoc`` to work. Even if you don't see this warning, you need to
:ref:`tell Readthedocs to install your code ` before compiling the
documentation.