A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
remove input for hidden cells for exports (#9548)
Shahmir Varqha5402c8d7f370
diff --git a/marimo/_convert/ipynb/from_ir.py b/marimo/_convert/ipynb/from_ir.pyindex b330796bb3..cc35b5f9e6 100644--- a/marimo/_convert/ipynb/from_ir.py+++ b/marimo/_convert/ipynb/from_ir.py@@ -34,6 +34,10 @@ if TYPE_CHECKING:# and break reproducibility. The marimo_version in metadata is sufficient._MD_PREFIX_RE = re.compile(r'mo\.md\(([fFrR]*)(?:"""|\'\'\'|"|\')')+# Standard nbconvert cell tag that, in combination with `TagRemovePreprocessor`,+# strips a cell's source from the rendered output.+NBCONVERT_REMOVE_INPUT_TAG = "remove-input"+def _extract_markdown_prefix(code: str) -> str:"""Extract the string prefix from a mo.md() call (e.g. '', 'r', 'f', 'fr')."""@@ -184,6 +188,14 @@ def _create_ipynb_cell()if outputs:node.outputs = outputs+ if config.hide_code:+ node["metadata"].setdefault("jupyter", {})["source_hidden"] = True+ existing_tags = node["metadata"].get("tags") or []+ if NBCONVERT_REMOVE_INPUT_TAG not in existing_tags:+ node["metadata"]["tags"] = [+ *existing_tags,+ NBCONVERT_REMOVE_INPUT_TAG,+ ]_add_marimo_metadata(node, name, config)return nodediff --git a/marimo/_convert/ipynb/to_ir.py b/marimo/_convert/ipynb/to_ir.pyindex 181c5a8f6c..4ac77e3382 100644--- a/marimo/_convert/ipynb/to_ir.py+++ b/marimo/_convert/ipynb/to_ir.py@@ -23,6 +23,7 @@ from marimo._convert.common.format import (DEFAULT_MARKDOWN_PREFIX,markdown_to_marimo,)+from marimo._convert.ipynb.from_ir import NBCONVERT_REMOVE_INPUT_TAGfrom marimo._runtime.dataflow import DirectedGraphfrom marimo._schemas.serialization import (AppInstantiation,@@ -1263,7 +1264,10 @@ def bind_cell_metadata(This marks the boundary between source-only transformations and cell-level transformations.- - If "hide-cell" is present in the tags, the cell is marked hidden (and removed)+ - If "hide-cell" or the standard nbconvert "remove-input" tag is present,+ the cell is marked hidden (and the tag is consumed).+ - The Jupyter UI hint ``metadata.jupyter.source_hidden`` is also treated as+ a hidden-code signal.- Remaining tags (if any) are inserted as a comment at the top of the source.- If marimo-specific metadata is present, it is used to restore cell config."""@@ -1273,6 +1277,14 @@ def bind_cell_metadata():tags: set[str] = set(meta.get("tags", []))+ # The `remove-input` tag and `jupyter.source_hidden` flag are emitted by+ # marimo's ipynb exporter (and recognized by nbconvert / JupyterLab /+ # VS Code) to indicate hidden code. Consume them here so they don't+ # leak back into the .py source as a stray comment.+ had_remove_input = NBCONVERT_REMOVE_INPUT_TAG in tags+ tags.discard(NBCONVERT_REMOVE_INPUT_TAG)+ source_hidden = bool(meta.get("jupyter", {}).get("source_hidden"))+# Extract marimo-specific cell config if presentmarimo_meta = meta.get("marimo", {})marimo_config = marimo_meta.get("config", {})@@ -1284,6 +1296,8 @@ def bind_cell_metadata(elif "hide-cell" in tags:tags.discard("hide-cell")hide_code = True+ elif had_remove_input or source_hidden:+ hide_code = Trueelif "marimo" in meta:# Cell was created by marimo; marimo would have stored hide_code=True# explicitly if needed, so default to False instead of is_markdowndiff --git a/marimo/_server/export/exporter.py b/marimo/_server/export/exporter.pyindex 86cc04dc68..8c84cc3ba7 100644--- a/marimo/_server/export/exporter.py+++ b/marimo/_server/export/exporter.py@@ -25,7 +25,10 @@ from marimo._convert.common.filename import (get_download_filename,get_filename,)-from marimo._convert.ipynb.from_ir import convert_from_ir_to_ipynb+from marimo._convert.ipynb.from_ir import (+ NBCONVERT_REMOVE_INPUT_TAG,+ convert_from_ir_to_ipynb,+)from marimo._dependencies.dependencies import DependencyManagerfrom marimo._messaging.mimetypes import KnownMimeTypefrom marimo._runtime.virtual_file import read_virtual_file@@ -54,6 +57,8 @@ from marimo._version import __version__if TYPE_CHECKING:from collections.abc import Mapping+ from traitlets.config import Config+from marimo._server.export._status import PDFExportStatusCallbackLOGGER = _loggers.marimo_logger()@@ -69,6 +74,20 @@ VIRTUAL_FILE_ALLOWED_TAGS = {"img", "audio", "video"}MAX_VIRTUAL_FILE_INLINE_BYTES = 10 * 1024 * 1024+def _nbconvert_tag_remove_config() -> Config:+ """Build a traitlets config that strips inputs from cells tagged with+ `NBCONVERT_REMOVE_INPUT_TAG`. Used to honor `hide_code=True` in nbconvert+ exports."""+ from traitlets.config import Config++ config = Config()+ config.TagRemovePreprocessor.enabled = True+ config.TagRemovePreprocessor.remove_input_tags = (+ NBCONVERT_REMOVE_INPUT_TAG,+ )+ return config++class Exporter:# Virtual file URL format constants_VIRTUAL_FILE_PATTERN = "./@file/"@@ -445,7 +464,9 @@ class Exporter:phase="render",message="rendering PDF via standard exporter...",)- exporter = PDFExporter() # type: ignore[no-untyped-call]+ exporter = PDFExporter( # type: ignore[no-untyped-call]+ config=_nbconvert_tag_remove_config(),+ )exporter.exclude_input = not include_inputspdf_data, _resources = exporter.from_notebook_node(notebook) # type: ignore[no-untyped-call]if isinstance(pdf_data, bytes):@@ -479,7 +500,9 @@ class Exporter:phase="render",message="rendering PDF via WebPDF...",)- web_exporter = WebPDFExporter() # type: ignore[no-untyped-call]+ web_exporter = WebPDFExporter( # type: ignore[no-untyped-call]+ config=_nbconvert_tag_remove_config(),+ )web_exporter.exclude_input = not include_inputsweb_exporter.allow_chromium_download = Truepdf_data, _resources = web_exporter.from_notebook_node(notebook) # type: ignore[no-untyped-call]@@ -590,7 +613,9 @@ class Exporter:)# Convert to reveal.js HTML- slides_exporter = SlidesExporter() # type: ignore[no-untyped-call]+ slides_exporter = SlidesExporter( # type: ignore[no-untyped-call]+ config=_nbconvert_tag_remove_config(),+ )slides_exporter.exclude_input = not include_inputshtml_data, _resources = slides_exporter.from_notebook_node(notebook)diff --git a/tests/_convert/ipynb/snapshots/cell_names_and_defs_top_down.ipynb.txt b/tests/_convert/ipynb/snapshots/cell_names_and_defs_top_down.ipynb.txtindex e7e45bbb9d..eb944cbd2b 100644--- a/tests/_convert/ipynb/snapshots/cell_names_and_defs_top_down.ipynb.txt+++ b/tests/_convert/ipynb/snapshots/cell_names_and_defs_top_down.ipynb.txt@@ -63,12 +63,18 @@"execution_count": null,"id": "lEQa","metadata": {+ "jupyter": {+ "source_hidden": true+ },"marimo": {"config": {"hide_code": true},"name": "*subtract"- }+ },+ "tags": [+ "remove-input"+ ]},"outputs": [],"source": [@@ -99,12 +105,18 @@"execution_count": null,"id": "Xref","metadata": {+ "jupyter": {+ "source_hidden": true+ },"marimo": {"config": {"hide_code": true},"name": "*HiddenClass"- }+ },+ "tags": [+ "remove-input"+ ]},"outputs": [],"source": [diff --git a/tests/_convert/ipynb/snapshots/complex_file_format_top_down.ipynb.txt b/tests/_convert/ipynb/snapshots/complex_file_format_top_down.ipynb.txtindex a018546952..b61fd3d007 100644--- a/tests/_convert/ipynb/snapshots/complex_file_format_top_down.ipynb.txt+++ b/tests/_convert/ipynb/snapshots/complex_file_format_top_down.ipynb.txt@@ -133,12 +133,18 @@"execution_count": null,"id": "SFPL","metadata": {+ "jupyter": {+ "source_hidden": true+ },"marimo": {"config": {"hide_code": true},"name": "*remove"- }+ },+ "tags": [+ "remove-input"+ ]},"outputs": [],"source": [@@ -172,12 +178,18 @@"execution_count": null,"id": "RGSE","metadata": {+ "jupyter": {+ "source_hidden": true+ },"marimo": {"config": {"hide_code": true},"name": "*MyHiddenClass"- }+ },+ "tags": [+ "remove-input"+ ]},"outputs": [],"source": [diff --git a/tests/_convert/ipynb/test_from_ir.py b/tests/_convert/ipynb/test_from_ir.pyindex 52f5b9f15c..e2ba28dc48 100644--- a/tests/_convert/ipynb/test_from_ir.py+++ b/tests/_convert/ipynb/test_from_ir.py@@ -80,6 +80,33 @@ def test_export_ipynb_sort_modes() -> None:snapshot_test("notebook_topological.ipynb.txt", content)+def test_export_ipynb_marks_hidden_code_cells_for_nbconvert() -> None:+ """Regression test for marimo-team/marimo#9523."""+ app = App()++ @app.cell(hide_code=True)+ def _():+ x = 1+ return (x,)++ @app.cell()+ def _(x):+ print(x)+ return++ ipynb_json = json.loads(+ convert_from_ir_to_ipynb(InternalApp(app), sort_mode="top-down")+ )++ cells = ipynb_json["cells"]+ # `jupyter.source_hidden` is for editor UIs (JupyterLab, VS Code).+ assert cells[0]["metadata"]["jupyter"] == {"source_hidden": True}+ # `remove-input` tag is what nbconvert's TagRemovePreprocessor acts on.+ assert cells[0]["metadata"]["tags"] == ["remove-input"]+ assert "jupyter" not in cells[1]["metadata"]+ assert "tags" not in cells[1]["metadata"]++@pytest.mark.parametrize(("input_data", "expected"),[diff --git a/tests/_convert/ipynb/test_ipynb_to_ir.py b/tests/_convert/ipynb/test_ipynb_to_ir.pyindex 861bafd771..6be236cda2 100644--- a/tests/_convert/ipynb/test_ipynb_to_ir.py+++ b/tests/_convert/ipynb/test_ipynb_to_ir.py@@ -1424,3 +1424,69 @@ def test_convert_from_ipynb_with_star_import():assert len(result.cells) >= 1# The star import cell should be present in the outputassert any("from os.path import *" in c.code for c in result.cells)+++def test_remove_input_tag_marks_hidden_and_does_not_leak_into_source():+ """The `remove-input` nbconvert tag implies hide_code and is consumed."""+ notebook = {+ "cells": [+ {+ "cell_type": "code",+ "source": ["print('hidden')"],+ "metadata": {"tags": ["remove-input"]},+ },+ ],+ "metadata": {},+ "nbformat": 4,+ "nbformat_minor": 2,+ }++ result = convert_from_ipynb_to_notebook_ir(json.dumps(notebook))+ assert len(result.cells) == 1+ cell = result.cells[0]+ assert cell.options.get("hide_code") is True+ # The tag must not be re-emitted as a `# Cell tags:` comment.+ assert "remove-input" not in cell.code+ assert "# Cell tags" not in cell.code+++def test_jupyter_source_hidden_marks_hidden():+ """`metadata.jupyter.source_hidden` is recognized as a hide_code signal."""+ notebook = {+ "cells": [+ {+ "cell_type": "code",+ "source": ["print('hidden via jupyter meta')"],+ "metadata": {"jupyter": {"source_hidden": True}},+ },+ ],+ "metadata": {},+ "nbformat": 4,+ "nbformat_minor": 2,+ }++ result = convert_from_ipynb_to_notebook_ir(json.dumps(notebook))+ assert len(result.cells) == 1+ assert result.cells[0].options.get("hide_code") is True+++def test_remove_input_alongside_other_tags_only_consumes_remove_input():+ """Other tags must still be surfaced as a `# Cell tags:` comment."""+ notebook = {+ "cells": [+ {+ "cell_type": "code",+ "source": ["print('hi')"],+ "metadata": {"tags": ["remove-input", "extra-tag"]},+ },+ ],+ "metadata": {},+ "nbformat": 4,+ "nbformat_minor": 2,+ }++ result = convert_from_ipynb_to_notebook_ir(json.dumps(notebook))+ cell = result.cells[0]+ assert cell.options.get("hide_code") is True+ assert "# Cell tags: extra-tag" in cell.code+ assert "remove-input" not in cell.codediff --git a/tests/_server/export/test_exporter.py b/tests/_server/export/test_exporter.pyindex 51fc7dc2e4..09da723060 100644--- a/tests/_server/export/test_exporter.py+++ b/tests/_server/export/test_exporter.py@@ -1237,6 +1237,55 @@ class TestPDFExport:("complete", "done."),]+ @pytest.mark.skipif(+ not HAS_NBFORMAT or not DependencyManager.nbconvert.has(),+ reason="nbformat and nbconvert are required to render ipynb",+ )+ def test_hide_code_cells_are_stripped_by_nbconvert(self) -> None:+ """End-to-end check that `hide_code=True` cells are excluded from+ rendered nbconvert output (LaTeX/HTML/Slides) via the `remove-input`+ tag wired up in `Exporter`.++ Regression test for marimo-team/marimo#9523.+ """+ import nbformat++ from marimo._convert.ipynb.from_ir import convert_from_ir_to_ipynb+ from marimo._server.export.exporter import (+ _nbconvert_tag_remove_config,+ )++ app = App()++ @app.cell(hide_code=True)+ def _():+ shouldnotappear = "hidden"+ return (shouldnotappear,)++ @app.cell()+ def _():+ shouldappear = "visible"+ return (shouldappear,)++ ipynb = convert_from_ir_to_ipynb(+ InternalApp(app), sort_mode="top-down"+ )+ notebook = nbformat.reads(ipynb, as_version=4)+ config = _nbconvert_tag_remove_config()++ from nbconvert import HTMLExporter, LatexExporter, SlidesExporter++ for exporter_cls in (LatexExporter, HTMLExporter, SlidesExporter):+ rendered, _ = exporter_cls(config=config).from_notebook_node(+ notebook+ )+ assert "shouldnotappear" not in rendered, (+ f"{exporter_cls.__name__} did not strip the hidden cell source"+ )+ assert "shouldappear" in rendered, (+ f"{exporter_cls.__name__} dropped the visible cell source"+ )+@pytest.mark.asyncioasync def test_run_app_then_export_as_pdf_ignores_status_callback_failures(self,