code-snippetsshow-hidepython-sphinxrestructuredtext

Sphinx, reStructuredText show/hide code snippets


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?


Solution

  • 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

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