Pages

Thursday, August 15, 2013

You have python code, how to document your API with Sphinx?

Simple four steps to get the HTML files from your python code

You just need four steps.
  • Execute sphinx_quickstart, and make sure that you check autodoc mode on:> autodoc: automatically insert docstrings from modules (y/N) [n]: y
  • In conf.py, add the path to your source code: sys.insert('./../src')
  • Execute sphinx-apidoc -o . ../src/
  • Execute make html
Now, you will have your python code in HTML format.

Include reST documents into index

Topic and comments

topic directive creates

tag in HTML. It seems to be pretty useful in describing what the reST document does.

.. topic:: What is Sphinx and the RST syntax ?
 
   CONTENTS ...
When you just use two dots, it works as a comment.

toctree

When you open the index.html, you will see nothing in the contents. It is because there is nothing under the toctree directive. Open the index.rst file which is the main reST for your project. You need to add the modules.
.. toctree::
   :maxdepth: 2
   
   hello1
You may not want to show toctree. This will notify Sphinx of the document hierarchy, but skip inserting links into the document.
.. toctree::
   :hidden:
You can make toctree numbered with :numbered: field.
.. toctree::
    :maxdepth: 1
    :numbered:

:doc:`…`

In this case, you can explicitly include the reST document with :doc:`…` field.
:doc:`hello1`
  Instructions on how to get the distribution.

API documentation

In step3, we can get (semi) automatically generated API documentation file from Sphinx with sphinx-apidoc command. Whenever you want to add more documentation to it, you have to do it manually.

Including math in the documentation

Add 'sphinx.ext.pngmath' in the extensions list. Then, use math directive or field:
:math:`a^2 + b^2 = c^2`.

.. math::

(a + b)^2 = a^2 + 2ab + b^2
(a - b)^2 = a^2 - 2ab + b^2
You need to specify the latex location in your computer. sphinx-build -b html -D pngmath_latex=`which latex` . _build/html.

Useful Sphinx knowledge

Extensions

In conf.py, you can add more Sphinx features with extensions list.
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.doctest', 'sphinx.ext.viewcode','sphinx.ext.todo']

References

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I am really enjoying reading your well written articles

    Certification CPC

    CPC CERTIFICATION

    The Coder performs a variety of activities involving the coding of medical records as a mechanism for indexing
    A Certified Professional Coder (CPC) is an individual of high professional integrity,
    Medical Coding Transforming coding for healthcare agencies Emergency Department and Ambulatory Surgery providing. Training 100% job support

    http://www.certificationcpc.com/

    ReplyDelete