Search code examples
pythonpython-sphinxtoctree

toctree nested drop down


I have a toctree in my index.rst that looks something like this:

.. toctree::
   :maxdepth: 2

   cat
   dog
   moose

I am looking to to nest the contents of my toctree similar to how it is done here with 'api documentation':

enter image description here

So ultimately make something like:

.. toctree::
   :maxdepth: 2
   :dropdown Animals
     cat
     dog
     moose

But I cannot seem to find anything that does this in the docs.


Solution

  • This behaviour of the toctree in the sidebar is is a feature of the Read the Docs theme (https://github.com/snide/sphinx_rtd_theme)

    Install it with pip install sphinx-rtd-theme

    The theme has the option collapse_navigation that controls whether to auto-collapse the tree when navigating to another part of the documentation or not.

    # -- Options for HTML output ----------------------------------------------
    
    # The theme to use for HTML and HTML Help pages.  See the documentation for
    # a list of builtin themes.
    html_theme = 'sphinx_rtd_theme'
    
    # Theme options are theme-specific and customize the look and feel of a theme
    # further.  For a list of options available for each theme, see the
    # documentation.
    html_theme_options = {
        "collapse_navigation" : False
    }
    

    indexr.rst:

    #################
      Title
    #################
    
    Animals
    =======
    
    .. toctree::
       :maxdepth: 2
    
        animals/index
    
    Flowers
    =======
    
    .. toctree::
       :maxdepth: 2
    
       flowers/index
    

    animals/index.rst:

    ####################
      Animals
    ####################
    
    .. toctree::
       :maxdepth: 2
    
       cat
       dog
       moose