Ben Hoff Ben Hoff - 9 days ago 6
Python Question

Automatically create a toctree for auto doc classes in Sphinx

I'm looking to increase the documentation in one of my libraries. I've been using sphinx to help build the documentation and recently started exploring the autodoc extension.

It seems like in most professional documentation, each class documentation page has a list of all the documented methods with links at the top. Or, in other words, a toctree at the top with hyperlinks to each of the more in depth method documentation.

Is there a way to automatically create this toctree for each of the classes being documented with autodoc?

Answer

In your conf.py file for sphinx add 'sphinx.ext.autosummary' to your extensions, set autosummary_generate to True and set the autodoc_default_flags = ['members'].

I put the toctree in my index.rst

..autosummary::
     :toctree: stubs

     Class1
     Class2
     Class3

See this example for the conf.py settings

and this example for an example of the toctree.

Hope that helps!

Comments