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
fix: resolve blank slides and broken links in server-side PDF export (#10501) (#10560)
Jacopos31195e315c129e7
diff --git a/marimo/_export/exporter.py b/marimo/_export/exporter.pyindex d46681a0ff..f8a2313b6c 100644--- a/marimo/_export/exporter.py+++ b/marimo/_export/exporter.py@@ -607,6 +607,10 @@ class Exporter:import nbformatnotebook = nbformat.reads(ipynb_json_str, as_version=4) # type: ignore[no-untyped-call]++ # Playwright loads temporary HTML without access to virtual file URLs.+ self._inline_virtual_files_in_notebook(notebook)+if request.png_fallbacks:from marimo._export._nbformat_png_fallbacks import (inject_png_fallbacks_into_notebook,@@ -625,6 +629,24 @@ class Exporter:notebook, request.options.include_inputs, request.app.filename)+ @staticmethod+ def _inline_virtual_files_in_notebook(notebook: Any) -> None:+ """Inline virtual files in code-cell HTML outputs before PDF rendering."""+ for cell in notebook.cells:+ if cell.cell_type != "code":+ continue+ for output in cell.outputs:+ data = output.get("data", {})+ content = data.get("text/html")+ if not isinstance(content, str) or "./@file/" not in content:+ continue+ data["text/html"], _ = replace_virtual_files_with_data_uris(+ content,+ allowed_tags=VIRTUAL_FILE_ALLOWED_TAGS,+ allowed_attributes=VIRTUAL_FILE_ALLOWED_ATTRIBUTES,+ max_inline_bytes=MAX_VIRTUAL_FILE_INLINE_BYTES,+ )+@staticmethoddef _to_file_uri(path: str) -> str:import osdiff --git a/tests/_export/test_exporter.py b/tests/_export/test_exporter.pyindex 3a3b44e9a4..e30beb0122 100644--- a/tests/_export/test_exporter.py+++ b/tests/_export/test_exporter.py@@ -2725,6 +2725,87 @@ class TestPDFExport:sys.modules.pop("playwright.async_api", None)[email protected](not HAS_NBFORMAT, reason="nbformat not installed")+async def test_export_as_slides_pdf_inlines_images_and_png_fallbacks(+ session_view: SessionView,+) -> None:+ app = App()++ @app.cell()+ def image_slide():+ return "image"++ @app.cell()+ def interactive_slide():+ return "interactive"++ internal_app = InternalApp(app)+ image_id, interactive_id = internal_app.cell_manager.cell_ids()+ image_data = {+ "text/html": '<img src="./@file/4-plot.png">',+ "text/plain": "literal ./@file/4-plot.png",+ "image/png": "bmF0aXZl",+ }+ session_view.add_notification(+ CellNotification(+ cell_id=image_id,+ output=CellOutput(+ channel=CellChannel.OUTPUT,+ mimetype="application/vnd.marimo+mimebundle",+ data=image_data,+ ),+ console=[CellOutput.stdout("console ./@file/4-plot.png")],+ )+ )+ session_view.add_notification(+ CellNotification(+ cell_id=interactive_id,+ output=CellOutput(+ channel=CellChannel.OUTPUT,+ mimetype="text/html",+ data="<marimo-slider></marimo-slider>",+ ),+ )+ )++ with (+ patch.object(DependencyManager.nbconvert, "has", return_value=True),+ patch.object(DependencyManager.playwright, "has", return_value=True),+ patch(+ "marimo._convert.common.dom_traversal.read_virtual_file",+ return_value=b"plot",+ ),+ patch.object(+ Exporter, "_export_slides_as_pdf", new_callable=AsyncMock+ ) as render,+ ):+ render.return_value = b"pdf"+ result = await Exporter().export_as_slides_pdf(+ _pdf_export_request(+ app=internal_app,+ session_view=session_view,+ png_fallbacks={+ interactive_id: "data:image/png;base64,Y2FwdHVyZWQ=",+ },+ preset="slides",+ )+ )++ assert result == b"pdf"+ notebook = render.await_args.args[0]+ assert notebook.cells[0].id == image_id+ assert notebook.cells[0].outputs[1].data == {+ **image_data,+ "text/html": '<img src="data:image/png;base64,cGxvdA==">',+ }+ assert notebook.cells[0].outputs[0].text == "console ./@file/4-plot.png"+ assert notebook.cells[1].id == interactive_id+ assert notebook.cells[1].outputs[0].data == {+ "image/png": "Y2FwdHVyZWQ=",+ }+ assert session_view.cell_notifications[image_id].output.data == image_data++@pytest.mark.skipif(sys.platform == "win32",reason="Unix permission bits not supported on Windows",diff --git a/tests/_server/api/endpoints/test_export.py b/tests/_server/api/endpoints/test_export.pyindex af1b67af2b..cd59da3439 100644--- a/tests/_server/api/endpoints/test_export.py+++ b/tests/_server/api/endpoints/test_export.py@@ -1658,7 +1658,7 @@ def test_export_pdf_endpoint_uses_browser_captured_outputs(output=CellOutput(channel=CellChannel.OUTPUT,mimetype="text/html",- data="<div>current output</div>",+ data="<marimo-slider></marimo-slider>",),status="idle",)@@ -1675,9 +1675,17 @@ def test_export_pdf_endpoint_uses_browser_captured_outputs(assert capture_response.status_code == 200render_pdf_mock = AsyncMock(return_value=b"mock_pdf_content")- with patch(- "marimo._server.api.endpoints.export.render_pdf",- render_pdf_mock,+ with (+ patch(+ "marimo._server.api.endpoints.export.render_pdf",+ render_pdf_mock,+ ),+ patch(+ "marimo._export._pdf_raster.collect_pdf_png_fallbacks",+ AsyncMock(+ return_value={cell_id: "data:image/png;base64,c2VydmVy"}+ ),+ ),):response = client.post("/api/export/pdf",@@ -1689,6 +1697,7 @@ def test_export_pdf_endpoint_uses_browser_captured_outputs(render_request = render_pdf_mock.await_args.args[0]assert isinstance(render_request, PDFExportRequest)assert render_request.session_view is session.session_view+ assert render_request.png_fallbacks is Nonecaptured_output = render_request.session_view.cell_notifications[cell_id].output@@ -1697,7 +1706,7 @@ def test_export_pdf_endpoint_uses_browser_captured_outputs(channel=CellChannel.OUTPUT,mimetype="application/vnd.marimo+mimebundle",data={- "text/html": "<div>current output</div>",+ "text/html": "<marimo-slider></marimo-slider>","image/png": "data:image/png;base64,ZmFrZQ==",},timestamp=captured_output.timestamp,