diff --git a/doc/conf.py b/doc/conf.py index fd29b637..ae71f5ea 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -13,6 +13,7 @@ 'sphinxcontrib.bibtex', # for bibliographic references 'sphinxcontrib.rsvgconverter', # for SVG->PDF conversion in LaTeX output 'sphinx_gallery.load_style', # load CSS for gallery (needs SG >= 0.6) + 'sphinx_togglebutton', ] # Default language for syntax highlighting in reST and Markdown cells: diff --git a/doc/hidden-cells.ipynb b/doc/hidden-cells.ipynb index a848aedc..498e6397 100644 --- a/doc/hidden-cells.ipynb +++ b/doc/hidden-cells.ipynb @@ -79,6 +79,51 @@ "source": [ "This is the cell after the hidden cell." ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Toggling Cells\n", + "\n", + "You can make a cell toggle-able by adding \"hide_input\" to the cell metadata tags\n", + "\n", + "TODO: check how adding a tag is explained in other parts of doc" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "tags": [ + "toggle_input" + ] + }, + "outputs": [], + "source": [ + "1 + 14" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "tags": [ + "hide_output" + ] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "my output is hidden\n" + ] + } + ], + "source": [ + "print(\"my output is hidden\")" + ] } ], "metadata": { @@ -97,9 +142,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.7" + "version": "3.6.8" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/doc/requirements.txt b/doc/requirements.txt index b0bddb43..236fe4ce 100644 --- a/doc/requirements.txt +++ b/doc/requirements.txt @@ -7,4 +7,5 @@ sphinxcontrib-svg2pdfconverter ipywidgets sphinx-copybutton sphinx-gallery +sphinx-togglebutton jupytext diff --git a/src/nbsphinx.py b/src/nbsphinx.py index 63211574..be1a96fc 100644 --- a/src/nbsphinx.py +++ b/src/nbsphinx.py @@ -115,6 +115,9 @@ {%- if not cell.outputs %} :no-output: {%- endif %} +{%- if 'toggle_input' in cell.metadata.tags %} + :class: toggle +{%- endif %} {{ cell.source.strip('\n') | indent }} {% endblock input %} @@ -1045,6 +1048,7 @@ def _create_code_nodes(directive): outer_classes = ['nbinput'] if 'no-output' in directive.options: outer_classes.append('nblast') + outer_classes.append(directive.options.get('class', '')) inner_classes = ['input_area'] prompt_template = config.nbsphinx_input_prompt if not execution_count: @@ -1122,6 +1126,7 @@ class NbInput(rst.Directive): 'empty-lines-before': rst.directives.nonnegative_int, 'empty-lines-after': rst.directives.nonnegative_int, 'no-output': rst.directives.flag, + 'class': rst.directives.unchanged, } has_content = True