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
refactor: consolidate `watch_and_export` implementations (#8000)
Péter Gyarmati47a782d193f6
diff --git a/marimo/_cli/export/commands.py b/marimo/_cli/export/commands.pyindex 94434a3795..5f8b7064ed 100644--- a/marimo/_cli/export/commands.py+++ b/marimo/_cli/export/commands.py@@ -53,6 +53,8 @@ def watch_and_export(watch: bool,export_callback: Callable[[MarimoPath], ExportResult],force: bool,+ *,+ initial_export_in_watch: bool = False,) -> None:if watch and not output:raise click.UsageError(@@ -60,13 +62,13 @@ def watch_and_export(+ "an output file with --output.")- def write_data(data: str) -> None:+ def write_result(result: ExportResult) -> None:if output:# Make dirs if neededmaybe_make_dirs(output)- output.write_text(data, encoding="utf-8")+ output.write_bytes(result.bytez)else:- echo(data)+ echo(result.text)returnif output:@@ -78,102 +80,41 @@ def watch_and_export(# No watch, just run onceif not watch:result = export_callback(marimo_path)- write_data(result.contents)+ write_result(result)if result.did_error:raise click.ClickException("Export was successful, but some cells failed to execute.")return+ # Watch mode: optionally do an initial export before waiting for changes+ if initial_export_in_watch:+ result = export_callback(marimo_path)+ write_result(result)+ if result.did_error:+ echo(+ "Warning: Export was successful, but some cells failed to execute.",+ err=True,+ )+async def on_file_changed(file_path: Path) -> None:if output:echo(f"File {str(file_path)} changed. Re-exporting to {green(str(output))}")- result = export_callback(MarimoPath(file_path))- write_data(result.contents)-- async def start() -> None:- # Watch the file for changes- watcher = FileWatcher.create(marimo_path.path, on_file_changed)- echo(f"Watching {green(marimo_path.relative_name)} for changes...")- watcher.start()- try:- # Run forever- while True: # noqa: ASYNC110- await asyncio.sleep(1)- except KeyboardInterrupt:- watcher.stop()-- asyncio_run(start())---def watch_and_export_bytes(- marimo_path: MarimoPath,- output: Path,- watch: bool,- export_callback: Callable[[MarimoPath], tuple[bytes | None, bool]],- force: bool,-) -> None:- if watch and not output:- raise click.UsageError(- "Cannot use --watch without providing "- + "an output file with --output."- )-- output_path = Path(output)- if not force and not watch:- if not prompt_to_overwrite(output_path):- return-- def write_data(data: bytes) -> None:- maybe_make_dirs(output_path)- output_path.write_bytes(data)-- # No watch, just run once- if not watch:- pdf_bytes, did_error = export_callback(marimo_path)- if pdf_bytes is None:- raise click.ClickException("Failed to export PDF.")- write_data(pdf_bytes)- if did_error:- raise click.ClickException(- "Export was successful, but some cells failed to execute."- )- return-- # Watch mode: do an initial export before waiting for changes- pdf_bytes, did_error = export_callback(marimo_path)- if pdf_bytes is None:- raise click.ClickException("Failed to export PDF.")- write_data(pdf_bytes)- if did_error:- echo(- "Warning: Export was successful, but some cells failed to execute.",- err=True,- )-- async def on_file_changed(file_path: Path) -> None:- echo(- f"File {str(file_path)} changed. Re-exporting to {green(str(output_path))}"- )try:# `export_callback` may call `asyncio_run()` internally. This callback# runs inside the file watcher's event loop, so we must execute the# export in a separate thread to avoid `asyncio.run()` nesting.- pdf_bytes, did_error = await asyncio.to_thread(+ result = await asyncio.to_thread(export_callback, MarimoPath(file_path))except Exception as e:echo(f"Error: {e}", err=True)return- if pdf_bytes is None:- echo("Error: Failed to export PDF.", err=True)- return-- write_data(pdf_bytes)- if did_error:+ write_result(result)+ if result.did_error:echo("Warning: Export was successful, but some cells failed to execute.",err=True,@@ -710,8 +651,23 @@ def pdf(except Exception as e:raise click.ClickException(f"Failed to export PDF: {e}") from None- return watch_and_export_bytes(- MarimoPath(name), output, watch, export_callback, force+ def export_callback_impl(file_path: MarimoPath) -> ExportResult:+ pdf_bytes, did_error = export_callback(file_path)+ if pdf_bytes is None:+ raise click.ClickException("Failed to export PDF.")+ return ExportResult(+ contents=pdf_bytes,+ download_filename=str(output),+ did_error=did_error,+ )++ return watch_and_export(+ MarimoPath(name),+ output,+ watch,+ export_callback_impl,+ force,+ initial_export_in_watch=True,)diff --git a/marimo/_server/export/__init__.py b/marimo/_server/export/__init__.pyindex 04066b95df..c5b8e24e9b 100644--- a/marimo/_server/export/__init__.py+++ b/marimo/_server/export/__init__.py@@ -5,6 +5,7 @@ import asyncioimport osimport sysfrom dataclasses import dataclass, replace+from functools import cached_propertyfrom typing import TYPE_CHECKING, Literal, Optional, castfrom marimo import _loggers@@ -47,10 +48,24 @@ if TYPE_CHECKING:@dataclassclass ExportResult:- contents: str+ contents: bytes | strdownload_filename: strdid_error: bool+ @cached_property+ def bytez(self) -> bytes:+ """Return UTF-8 encoded bytes (cached)."""+ if isinstance(self.contents, bytes):+ return self.contents+ return self.contents.encode("utf-8")++ @cached_property+ def text(self) -> str:+ """Return UTF-8 decoded text (cached)."""+ if isinstance(self.contents, str):+ return self.contents+ return self.contents.decode("utf-8")+def _as_ir(path: MarimoPath) -> NotebookSerialization:if path.is_python():@@ -90,7 +105,7 @@ def export_as_ipynb(app = load_app(path.absolute_name)if app is None:return ExportResult(- contents="",+ contents=b"",download_filename=get_download_filename(path.short_name, "ipynb"),did_error=True,)@@ -130,7 +145,7 @@ def export_as_wasm(_app = load_app(path.absolute_name)if _app is None:return ExportResult(- contents="",+ contents=b"",download_filename=get_download_filename(path.short_name, "wasm.html"),diff --git a/tests/_server/export/test_export_ipynb.py b/tests/_server/export/test_export_ipynb.pyindex dbe31ec4ed..185b7d86f2 100644--- a/tests/_server/export/test_export_ipynb.py+++ b/tests/_server/export/test_export_ipynb.py@@ -73,6 +73,6 @@ async def test_export_ipynb(app_path: Path) -> None:argv=None,)assert result.download_filename == f"{app_path.stem}.ipynb"- content = delete_lines_with_files(result.contents)+ content = delete_lines_with_files(result.text)content = simplify_images(content)snapshot(f"ipynb/{app_path.stem}.ipynb.txt", content)diff --git a/tests/_server/export/test_exporter.py b/tests/_server/export/test_exporter.pyindex a58400b374..6b59fdc632 100644--- a/tests/_server/export/test_exporter.py+++ b/tests/_server/export/test_exporter.py@@ -246,8 +246,8 @@ async def test_export_html_with_layout(tmp_path: Path) -> None:show_code=True,)assert result.did_error is False- assert "layout.json" not in result.contents- assert "data:application/json" in result.contents+ assert "layout.json" not in result.text+ assert "data:application/json" in result.text# HTML export