34

I've been documenting a software package using Sphinx and reStructuredText.

Within my documents, there are some long code snippets. I want to be able to have them hidden as default, with a little "Show/Hide" button that would expand them (Example).

Is there a standard way to do that?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Adam Matan
  • 128,757
  • 147
  • 397
  • 562

8 Answers8

51

You don't need a custom theme. Use the built-in directive container that allows you to add custom css-classes to blocks and override the existsting theme to add some javascript to add the show/hide-functionality.

This is _templates/page.html:

{% extends "!page.html" %}

{% block footer %}
 <script type="text/javascript">
    $(document).ready(function() {
        $(".toggle > *").hide();
        $(".toggle .header").show();
        $(".toggle .header").click(function() {
            $(this).parent().children().not(".header").toggle(400);
            $(this).parent().children(".header").toggleClass("open");
        })
    });
</script>
{% endblock %}

This is _static/custom.css:

.toggle .header {
    display: block;
    clear: both;
}

.toggle .header:after {
    content: " ▶";
}

.toggle .header.open:after {
    content: " ▼";
}

This is added to conf.py:

def setup(app):
    app.add_css_file('custom.css')

Now you can show/hide a block of code.

.. container:: toggle

    .. container:: header

        **Show/Hide Code**

    .. code-block:: xml
       :linenos:

       from plone import api
       ...

I use something very similar for exercises here: https://training.plone.org/5/mastering-plone/about_mastering.html#exercises

Paul B
  • 7
  • 4
pbauer
  • 635
  • 5
  • 8
  • Beautiful! Would be nice if the toggle solutions showed up as clickable links in vimperator/cvim/pentadactyl though. – The Unfun Cat Jun 16 '17 at 08:01
  • I think that using ▶ and ▼ makes it more intuitive to use. (See [Confluence as an example](https://confluence.atlassian.com/doc/expand-macro-223222352.html)). Get the unicode values [here](https://stackoverflow.com/a/11537979/1398841). – phoenix Apr 08 '19 at 14:00
  • This snippet `{% set css_files = css_files + ["_static/custom.css"] %}` results in a Sphinx warning: `RemovedinSphinx20Warning: builder.css_files is deprecated. Please use app.add_stylesheet() instead`. See [this answer](https://stackoverflow.com/a/37980230/1398841) for a work around. – phoenix Apr 09 '19 at 12:16
  • 1
    Would you mind posting the detailed instructions for the example from your plone course, which is using an admonition? – HBouy Jun 12 '20 at 14:12
  • 3
    How to get the arrow in the same line as the header? – dexteritas Jan 06 '21 at 15:28
  • @dexteritas to keep the arrow on the same line you can simply set ``.toggle .header {display: inline;}`` in css file – matusko Jul 28 '21 at 19:44
  • @matusko Unfortunately, that doesn't change anything for me either. The arrow is still in the next line. – dexteritas Aug 04 '21 at 08:47
  • Change `.toggle .header:after` to `.toggle .header p:after` and `.toggle .header.open:after` to `.toggle .header.open p:after` to avoid this issue @dexteritas – Azrael3000 May 09 '22 at 10:57
26

You can use the built-in HTML collapsible details tag by wrapping the code in two raw HTML directives

.. raw:: html

   <details>
   <summary><a>big code</a></summary>

.. code-block:: python

   lots_of_code = "this text block"

.. raw:: html

   </details>

Produces:

<details>
<summary><a>big code</a></summary>
<pre>lots_of_code = "this text block"</pre>
</details>
Epic Wink
  • 796
  • 13
  • 18
6

I think the easiest way to do this would be to create a custom Sphinx theme in which you tell certain html elements to have this functionality. A little JQuery would go a long way here.

If, however you want to be able to specify this in your reStructuredText markup, you would need to either

  • get such a thing included in Sphinx itself or
  • implement it in a Sphinx/docutils extension...and then create a Sphinx theme which knew about this functionality.

This would be a bit more work, but would give you more flexibility.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Kevin Horn
  • 4,167
  • 28
  • 30
6

There is a very simplistic extension providing exactly that feature: https://github.com/scopatz/hiddencode

It works rather well for me.

Martin Quinson
  • 1,347
  • 8
  • 16
5

The cloud sphinx theme has custom directive html-toggle that provides toggleable sections. To quote from their web page:

You can mark sections with .. rst-class:: html-toggle, which will make the section default to being collapsed under html, with a “show section” toggle link to the right of the title.

Here is a link to their test demonstration page.

phoenix
  • 7,988
  • 6
  • 39
  • 45
Phil
  • 5,822
  • 2
  • 31
  • 60
3

sphinx-togglebutton

Looks like a new sphinx extension has been made to do just this since this question has been answered.


Run: pip install sphinx-togglebutton

Add to conf.py

extensions = [
...
'sphinx_togglebutton'
...
]

In rst source file:

.. admonition:: Show/Hide
  :class: dropdown

  hidden message
1

since none of the above methods seem to work for me, here's how I solved it in the end:

  1. create a file substitutions.rst in your source-directory with the following content:

    .. |toggleStart| raw:: html
    
       <details>
       <summary><a> the title of the collapse-block </a></summary>
    
    .. |toggleEnd| raw:: html
    
       </details>
       <br/>
    
  2. add the following line at the beginning of every file you want to use add collapsible blocks

    ..include:: substitutions.rst
    
  3. now, to make a part of the code collapsible simply use:

    |toggleStart|
    
    the text you want to collapse
    ..code-block:: python 
        x=1
    
    |toggleEnd|
    
raphael
  • 2,159
  • 17
  • 20
  • This is basically the same as https://stackoverflow.com/a/60394068/407651 – mzjn Nov 21 '21 at 07:25
  • @mzjn yes It is... but I thought using substitutions makes it a bit more compact and readable (especially if you have a lot of collapsible segments with the same title-text) – raphael Nov 22 '21 at 09:09
0

Another option is the dropdown directive in the sphinx-design extension. From the docs:

  1. Install sphinx-design
pip install sphinx-design
  1. Add the extension to conf.py in the extensions list
extensions = ["sphinx_design"]
  1. Use the dropdown directive in your rst file:
.. dropdown::

    Dropdown content
Matt Pitkin
  • 3,989
  • 1
  • 18
  • 32