From 99d9afa4730b532604886aadbc1551777aa5d563 Mon Sep 17 00:00:00 2001 From: Michael Chow Date: Wed, 15 Apr 2020 17:02:30 -0400 Subject: [PATCH 1/2] sketch of toggling input --- doc/conf.py | 1 + doc/hidden-cells.ipynb | 60 ++++++++++++++++++++++++++++++++++++++++-- doc/requirements.txt | 1 + src/nbsphinx.py | 5 ++++ 4 files changed, 65 insertions(+), 2 deletions(-) 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..2271bd3f 100644 --- a/doc/hidden-cells.ipynb +++ b/doc/hidden-cells.ipynb @@ -79,6 +79,62 @@ "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": 9, + "metadata": { + "tags": [ + "toggle_input" + ] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "15" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1 + 14" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "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 +153,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 From 4cf34146563a4c2ea0be50a0d514129101ae2f37 Mon Sep 17 00:00:00 2001 From: Michael Chow Date: Wed, 15 Apr 2020 17:10:26 -0400 Subject: [PATCH 2/2] re-run and save doc notebook --- doc/hidden-cells.ipynb | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/doc/hidden-cells.ipynb b/doc/hidden-cells.ipynb index 2271bd3f..498e6397 100644 --- a/doc/hidden-cells.ipynb +++ b/doc/hidden-cells.ipynb @@ -93,31 +93,20 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 3, "metadata": { "tags": [ "toggle_input" ] }, - "outputs": [ - { - "data": { - "text/plain": [ - "15" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "1 + 14" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 4, "metadata": { "tags": [ "hide_output"