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
feat: allow excluding code blocks when exporting to PDF (#8362)
Péter Gyarmati4503fa8bc502
diff --git a/docs/guides/exporting.md b/docs/guides/exporting.mdindex 77c62301c0..a8f47a89ea 100644--- a/docs/guides/exporting.md+++ b/docs/guides/exporting.md@@ -131,6 +131,14 @@ enables exporting to PDF and other formats with Pandoc. See this [publishing](./### Export PDF from the command line+You can export directly with marimo:++```bash+marimo export pdf notebook.py -o notebook.pdf --no-include-inputs+```++The `--no-include-inputs` flag hides Python code cells while keeping markdown and cell outputs in the generated PDF.+If you export to a Jupyter notebook, you can leverage various Jupyter ecosystem tools. For PDFs, you willneed to have [Pandoc](https://nbconvert.readthedocs.io/en/latest/install.html#installing-pandoc) and [TeX](https://nbconvert.readthedocs.io/en/latest/install.html#installing-tex) installed. The examples below use `uvx`, which you can obtain by [installing `uv`](https://docs.astral.sh/uv/getting-started/installation/).diff --git a/marimo/_cli/export/commands.py b/marimo/_cli/export/commands.pyindex ce61d55a7f..7a525aeaf8 100644--- a/marimo/_cli/export/commands.py+++ b/marimo/_cli/export/commands.py@@ -528,6 +528,12 @@ Requires nbformat and nbconvert to be installed.type=bool,help="Run the notebook and include outputs in the exported PDF file.",)+ "--include-inputs/--no-include-inputs",+ default=True,+ type=bool,+ help="Include code cell inputs in the exported PDF file.",+)@click.option("--webpdf/--no-webpdf",default=True,@@ -575,6 +581,7 @@ def pdf(output: Path,watch: bool,include_outputs: bool,+ include_inputs: bool,webpdf: bool,sandbox: Optional[bool],force: bool,@@ -630,6 +637,7 @@ def pdf(run_app_then_export_as_pdf(file_path,include_outputs=include_outputs,+ include_inputs=include_inputs,webpdf=webpdf,cli_args=cli_args,argv=list(args) if include_outputs else None,diff --git a/marimo/_server/export/__init__.py b/marimo/_server/export/__init__.pyindex 7a1715da15..631422e908 100644--- a/marimo/_server/export/__init__.py+++ b/marimo/_server/export/__init__.py@@ -212,6 +212,7 @@ async def run_app_then_export_as_pdf(webpdf: bool,cli_args: SerializedCLIArgs,argv: list[str] | None,+ include_inputs: bool = True,) -> tuple[bytes | None, bool]:file_router = AppFileRouter.from_filename(filepath)file_key = file_router.get_unique_file_key()@@ -235,6 +236,7 @@ async def run_app_then_export_as_pdf(pdf_data = Exporter().export_as_pdf(app=file_manager.app,session_view=session_view,+ include_inputs=include_inputs,webpdf=webpdf,)return pdf_data, did_errordiff --git a/marimo/_server/export/exporter.py b/marimo/_server/export/exporter.pyindex abba9c61a2..960f31a770 100644--- a/marimo/_server/export/exporter.py+++ b/marimo/_server/export/exporter.py@@ -315,12 +315,14 @@ class Exporter:app: InternalApp,session_view: SessionView | None,webpdf: bool,+ include_inputs: bool = True,) -> bytes | None:"""Export notebook as a PDF.Args:app: The app to exportsession_view: The session view to export. If None, outputs are not included.+ include_inputs: Whether to include code cell inputs in the export.webpdf: If False, tries standard PDF export (pandoc + TeX) first,falling back to webpdf on failure. If True, uses webpdfdirectly.@@ -356,6 +358,7 @@ class Exporter:)exporter = PDFExporter()+ exporter.exclude_input = not include_inputspdf_data, _resources = exporter.from_notebook_node(notebook)if isinstance(pdf_data, bytes):return pdf_data@@ -370,6 +373,7 @@ class Exporter:from nbconvert import WebPDFExporter # type: ignore[import-not-found]web_exporter = WebPDFExporter()+ web_exporter.exclude_input = not include_inputsweb_exporter.allow_chromium_download = Truepdf_data, _resources = web_exporter.from_notebook_node(notebook)diff --git a/tests/_cli/test_cli_export.py b/tests/_cli/test_cli_export.pyindex 37d8ee5286..57e2617f27 100644--- a/tests/_cli/test_cli_export.py+++ b/tests/_cli/test_cli_export.py@@ -855,7 +855,12 @@ class TestExportPDF:) -> None:output_file = temp_marimo_file.replace(".py", ".pdf")p = _run_export(- "pdf", temp_marimo_file, "--output", output_file, "--no-sandbox"+ "pdf",+ temp_marimo_file,+ "--output",+ output_file,+ "--no-sandbox",+ "--no-include-inputs",)_assert_failure(p)stderr = p.stderr.decode()diff --git a/tests/_server/export/test_exporter.py b/tests/_server/export/test_exporter.pyindex 36f66c5c83..52d34d87db 100644--- a/tests/_server/export/test_exporter.py+++ b/tests/_server/export/test_exporter.py@@ -946,6 +946,7 @@ class TestPDFExport:assert result == b"mock_pdf_data"mock_pdf_exporter.assert_called_once()mock_exporter_instance.from_notebook_node.assert_called_once()+ assert mock_exporter_instance.exclude_input is False@pytest.mark.skipif(not DependencyManager.nbformat.has()@@ -991,9 +992,56 @@ class TestPDFExport:assert result == b"mock_webpdf_data"mock_webpdf_exporter.assert_called_once()+ assert mock_exporter_instance.exclude_input is False# Verify allow_chromium_download is setassert mock_exporter_instance.allow_chromium_download is True+ @pytest.mark.skipif(+ not DependencyManager.nbformat.has()+ or not DependencyManager.nbconvert.has(),+ reason="nbformat or nbconvert not installed",+ )+ def test_export_as_pdf_webpdf_mode_without_inputs(+ self,+ session_view: SessionView,+ ) -> None:+ """Test WebPDF export suppressing code inputs."""++ app = App()++ @app.cell()+ def test_cell():+ return "test"++ file_manager = AppFileManager.from_app(InternalApp(app))+ exporter = Exporter()++ mock_exporter_instance = MagicMock()+ mock_exporter_instance.from_notebook_node.return_value = (+ b"mock_webpdf_data_no_inputs",+ {},+ )++ with (+ patch.object(+ DependencyManager.playwright, "has", return_value=True+ ),+ patch("nbconvert.WebPDFExporter") as mock_webpdf_exporter,+ ):+ mock_webpdf_exporter.return_value = mock_exporter_instance++ result = exporter.export_as_pdf(+ app=file_manager.app,+ session_view=session_view,+ include_inputs=False,+ webpdf=True,+ )++ assert result == b"mock_webpdf_data_no_inputs"+ mock_webpdf_exporter.assert_called_once()+ assert mock_exporter_instance.exclude_input is True+ assert mock_exporter_instance.allow_chromium_download is True+@pytest.mark.skipif(not DependencyManager.nbformat.has()or not DependencyManager.nbconvert.has(),@@ -1073,6 +1121,7 @@ class TestPDFExport:result = exporter.export_as_pdf(app=file_manager.app,session_view=session_view,+ include_inputs=False,webpdf=False, # Request standard PDF, but it should fall back)@@ -1081,9 +1130,11 @@ class TestPDFExport:# PDFExporter was tried firstmock_pdf_exporter.assert_called_once()mock_pdf_exporter_instance.from_notebook_node.assert_called_once()+ assert mock_pdf_exporter_instance.exclude_input is True# WebPDFExporter was used as fallbackmock_webpdf_exporter.assert_called_once()mock_webpdf_exporter_instance.from_notebook_node.assert_called_once()+ assert mock_webpdf_exporter_instance.exclude_input is True# Verify allow_chromium_download is set on fallbackassert (mock_webpdf_exporter_instance.allow_chromium_download is True