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(export): wrap long code lines in WebPDF export (#10295)
Abdul Rehman909526250cfc
diff --git a/marimo/_export/exporter.py b/marimo/_export/exporter.pyindex e406f65d3b..cae503faf4 100644--- a/marimo/_export/exporter.py+++ b/marimo/_export/exporter.py@@ -98,6 +98,38 @@ def _nbconvert_tag_remove_config() -> Config:return config+# JupyterLab's stylesheet (shipped with nbconvert) styles the code input area+# with `overflow: hidden` and leaves the `<pre>` at the default `white-space:+# pre`. In JupyterLab that is fine because the editor scrolls; in a PDF there is+# no scrolling, so long lines are clipped and the text is lost entirely.+# Outputs already wrap (`.jp-OutputArea-output pre` sets `word-break`), so this+# only targets the input area.+#+# `!important` is required because this is inlined ahead of the JupyterLab+# rules; the slides PDF path overrides nbconvert styling the same way.+WEBPDF_CODE_WRAP_CSS = """\+/* marimo: wrap long code lines instead of clipping them (#9421) */+.jp-InputArea-editor {+ overflow: visible !important;+}+.jp-InputArea-editor .highlight pre {+ white-space: pre-wrap !important;+ overflow-wrap: anywhere !important;+}+"""+++def _inline_code_wrap_css(nb: Any, resources: Any) -> tuple[Any, Any]:+ """Inline `WEBPDF_CODE_WRAP_CSS`, as an nbconvert preprocessor.++ Preprocessors run after nbconvert populates `resources`, so appending here+ is what gets the stylesheet into the rendered HTML.+ """+ inlining = resources.setdefault("inlining", {})+ inlining.setdefault("css", []).append(WEBPDF_CODE_WRAP_CSS)+ return nb, resources++def _render_webpdf_with_nbconvert(notebook: Any, include_inputs: bool) -> Any:if sys.platform == "win32":# marimo installs the Selector policy during import. The spawned render@@ -111,6 +143,9 @@ def _render_webpdf_with_nbconvert(notebook: Any, include_inputs: bool) -> Any:)web_exporter.exclude_input = not include_inputsweb_exporter.allow_chromium_download = True+ web_exporter.register_preprocessor( # type: ignore[no-untyped-call]+ _inline_code_wrap_css, enabled=True+ )pdf_data, _resources = web_exporter.from_notebook_node(notebook) # type: ignore[no-untyped-call]return pdf_datadiff --git a/tests/_export/test_exporter.py b/tests/_export/test_exporter.pyindex 4887eb682f..16fd7f94b8 100644--- a/tests/_export/test_exporter.py+++ b/tests/_export/test_exporter.py@@ -1851,6 +1851,62 @@ class TestPDFExport:f"{exporter_cls.__name__} dropped the visible cell source")+ @pytest.mark.skipif(+ not HAS_NBFORMAT or not DependencyManager.nbconvert.has(),+ reason="nbformat and nbconvert are required to render ipynb",+ )+ def test_webpdf_inlines_code_wrapping_css(self) -> None:+ """WebPDF export inlines CSS that wraps long code lines.++ JupyterLab's stylesheet clips the code input area, so long lines were+ silently truncated in the PDF instead of wrapping.++ Regression test for marimo-team/marimo#9421.+ """+ import nbformat+ from nbconvert import WebPDFExporter++ from marimo._export.exporter import (+ _render_webpdf_with_nbconvert,+ )++ notebook = nbformat.v4.new_notebook()+ notebook.cells = [+ nbformat.v4.new_code_cell(+ "# a comment long enough to run past the printable width of a "+ "PDF page and get clipped"+ )+ ]++ # `run_playwright` receives the fully rendered HTML, so stubbing it+ # captures what Chromium would have been handed without needing a+ # browser. Going through `_render_webpdf_with_nbconvert` means the+ # test fails if the production code stops registering the+ # preprocessor.+ rendered: list[str] = []++ def fake_run_playwright(self: Any, html: str) -> bytes:+ del self+ rendered.append(html)+ return b"mock_pdf_data"++ with patch.object(+ WebPDFExporter, "run_playwright", fake_run_playwright+ ):+ pdf_data = _render_webpdf_with_nbconvert(+ notebook, include_inputs=True+ )++ assert pdf_data == b"mock_pdf_data"+ assert len(rendered) == 1+ # Assert on the distinctive selector and properties rather than the+ # whole CSS constant, so the test is not brittle to nbconvert+ # whitespace/comment handling when inlining the stylesheet.+ html = rendered[0]+ assert ".jp-InputArea-editor .highlight pre" in html+ assert "white-space: pre-wrap !important" in html+ assert "overflow-wrap: anywhere !important" in html+@pytest.mark.asyncioasync def test_export_pdf_ignores_status_callback_failures(self,@@ -2009,6 +2065,10 @@ class TestPDFExport:class WebPDFExporter:def __init__(self, config):self.config = config+ self.preprocessors = []++ def register_preprocessor(self, preprocessor, enabled=False):+ self.preprocessors.append((preprocessor, enabled))def from_notebook_node(self, notebook):return b"mock_webpdf_data", {}