From 8421e07ed287b2114c0c00288f0499d8ef6ad902 Mon Sep 17 00:00:00 2001 From: Thomas Viehmann Date: Tue, 2 Mar 2021 19:40:45 +0100 Subject: [PATCH 1/3] process cell tags remove-input and remove-output --- doc/hidden-cells.ipynb | 77 +++++++++++++++++++++++++++++++++++++++--- src/nbsphinx.py | 4 +++ 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/doc/hidden-cells.ipynb b/doc/hidden-cells.ipynb index a848aedc..e5622e7e 100644 --- a/doc/hidden-cells.ipynb +++ b/doc/hidden-cells.ipynb @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "nbsphinx": "hidden" }, @@ -47,9 +47,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "42" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "answer" ] @@ -79,6 +90,64 @@ "source": [ "This is the cell after the hidden cell." ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Hidden input and output\n", + "\n", + "You can also only remove input and output of a notebook using the `remove-input` and `remove-output` cell tags.\n", + "This is different from the metadata change for hiding everything, but is compatible with Jupyter Book." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "tags": [ + "remove-output" + ] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'Cell with hidden output'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"Cell with hidden output\"" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "tags": [ + "remove-input" + ] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'Did you see the input?'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"Did you see the input?\"" + ] } ], "metadata": { @@ -97,7 +166,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.7" + "version": "3.9.1+" } }, "nbformat": 4, diff --git a/src/nbsphinx.py b/src/nbsphinx.py index 4482dfcc..a33e45d0 100644 --- a/src/nbsphinx.py +++ b/src/nbsphinx.py @@ -112,6 +112,7 @@ {% block input -%} +{% if 'remove-input' not in cell.metadata.tags %} .. nbinput:: {% if cell.metadata.magics_language -%} {{ cell.metadata.magics_language }} {%- elif nb.metadata.language_info -%} @@ -128,6 +129,7 @@ {%- endif %} {{ cell.source.strip('\n') | indent }} +{% endif %} {% endblock input %} @@ -216,6 +218,7 @@ {% block nboutput -%} +{% if 'remove-output' not in cell.metadata.tags %} .. {# Empty comment to make sure the preceding directive (if any) is closed #} {%- set html_datatype, latex_datatype = output | get_output_type %} @@ -229,6 +232,7 @@ {{ insert_nboutput(latex_datatype, output, cell) | indent }} {% endif %} +{% endif %} {% endblock nboutput %} From 78987c36c9fe4e19812c9c4d729280857407e0b5 Mon Sep 17 00:00:00 2001 From: Thomas Viehmann Date: Mon, 8 Mar 2021 21:00:34 +0100 Subject: [PATCH 2/3] add remove-cell --- src/nbsphinx.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nbsphinx.py b/src/nbsphinx.py index a33e45d0..cd99c199 100644 --- a/src/nbsphinx.py +++ b/src/nbsphinx.py @@ -103,7 +103,7 @@ {% block any_cell %} -{%- if cell.metadata.nbsphinx != 'hidden' %} +{%- if cell.metadata.nbsphinx != 'hidden' and 'remove-cell' not in cell.metadata.tags %} {{ super() }} .. {# Empty comment to make sure the preceding directive (if any) is closed #} From 198ab7802e1e0ff6b3a18f413eae7c4be7441a47 Mon Sep 17 00:00:00 2001 From: Thomas Viehmann Date: Mon, 8 Mar 2021 22:02:16 +0100 Subject: [PATCH 3/3] Use nbconvert's TagRemovePreprocessor\nthank you @mgeier for the suggestion --- doc/hidden-cells.ipynb | 71 +++++++++++++++++------------------------- src/nbsphinx.py | 12 ++++--- 2 files changed, 36 insertions(+), 47 deletions(-) diff --git a/doc/hidden-cells.ipynb b/doc/hidden-cells.ipynb index e5622e7e..ab13eebd 100644 --- a/doc/hidden-cells.ipynb +++ b/doc/hidden-cells.ipynb @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "nbsphinx": "hidden" }, @@ -47,20 +47,29 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "42" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], + "source": [ + "answer" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Another way to hide sphinx is by adding a `remove-cell` cell tag. This is compatible with Jupyter Book." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "remove-cell" + ] + }, + "outputs": [], "source": [ "answer" ] @@ -98,53 +107,31 @@ "## Hidden input and output\n", "\n", "You can also only remove input and output of a notebook using the `remove-input` and `remove-output` cell tags.\n", - "This is different from the metadata change for hiding everything, but is compatible with Jupyter Book." + "Unlike for hiding entire cells, no metadata-based hiding of input and output is provided." ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "tags": [ "remove-output" ] }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Cell with hidden output'" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\"Cell with hidden output\"" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "tags": [ "remove-input" ] }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Did you see the input?'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\"Did you see the input?\"" ] @@ -166,7 +153,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.1+" + "version": "3.6.7" } }, "nbformat": 4, diff --git a/src/nbsphinx.py b/src/nbsphinx.py index cd99c199..83d2b986 100644 --- a/src/nbsphinx.py +++ b/src/nbsphinx.py @@ -103,7 +103,7 @@ {% block any_cell %} -{%- if cell.metadata.nbsphinx != 'hidden' and 'remove-cell' not in cell.metadata.tags %} +{%- if cell.metadata.nbsphinx != 'hidden' %} {{ super() }} .. {# Empty comment to make sure the preceding directive (if any) is closed #} @@ -112,7 +112,6 @@ {% block input -%} -{% if 'remove-input' not in cell.metadata.tags %} .. nbinput:: {% if cell.metadata.magics_language -%} {{ cell.metadata.magics_language }} {%- elif nb.metadata.language_info -%} @@ -129,7 +128,6 @@ {%- endif %} {{ cell.source.strip('\n') | indent }} -{% endif %} {% endblock input %} @@ -218,7 +216,6 @@ {% block nboutput -%} -{% if 'remove-output' not in cell.metadata.tags %} .. {# Empty comment to make sure the preceding directive (if any) is closed #} {%- set html_datatype, latex_datatype = output | get_output_type %} @@ -232,7 +229,6 @@ {{ insert_nboutput(latex_datatype, output, cell) | indent }} {% endif %} -{% endif %} {% endblock nboutput %} @@ -789,6 +785,12 @@ def replace_attachments(text): 'HighlightMagicsPreprocessor': {'enabled': True}, # Work around https://github.com/jupyter/nbconvert/issues/720: 'RegexRemovePreprocessor': {'enabled': False}, + 'TagRemovePreprocessor': { + 'enabled': True, + 'remove_cell_tags': ('remove-cell',), + 'remove_all_outputs_tags': ('remove-output',), + 'remove_input_tags': ('remove-input',), + }, }), filters={ 'convert_pandoc': convert_pandoc,