Skip to content

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.

fix: give real UIElementRegistry when running as a script (#1218)

Akshay Agrawal7104261cf470
diff --git a/marimo/_plugins/ui/_core/registry.py b/marimo/_plugins/ui/_core/registry.py
index 60ec15a9a2..955e506b01 100644
--- a/marimo/_plugins/ui/_core/registry.py
+++ b/marimo/_plugins/ui/_core/registry.py
@@ -172,41 +172,3 @@ class UIElementRegistry:
del self._bindings[object_id]
if object_id in self._constructing_cells:
del self._constructing_cells[object_id]
-
-
-class NoopUIElementRegistry(UIElementRegistry):
- def __init__(self) -> None:
- pass
-
- def register(
- self,
- object_id: UIElementId,
- ui_element: UIElement[Any, Any],
- ) -> None:
- del object_id
- del ui_element
- pass
-
- def bound_names(self, object_id: UIElementId) -> Iterable[str]:
- del object_id
- return []
-
- def get_object(self, object_id: UIElementId) -> UIElement[Any, Any]:
- del object_id
- raise NotImplementedError
-
- def get_cell(self, object_id: UIElementId) -> CellId_t:
- del object_id
- raise NotImplementedError
-
- def resolve_lens(
- self, object_id: UIElementId, value: LensValue[T]
- ) -> tuple[str, LensValue[T]]:
- del object_id
- del value
- raise NotImplementedError
-
- def delete(self, object_id: UIElementId, python_id: int) -> None:
- del object_id
- del python_id
- pass
diff --git a/marimo/_runtime/context/script_context.py b/marimo/_runtime/context/script_context.py
index 571e831b61..3376cbcc75 100644
--- a/marimo/_runtime/context/script_context.py
+++ b/marimo/_runtime/context/script_context.py
@@ -8,6 +8,7 @@ from typing import TYPE_CHECKING, Any, Iterator, Optional
from marimo._ast.app import InternalApp
from marimo._cli.parse_args import args_from_argv
from marimo._plugins.ui._core.ids import NoIDProviderException
+from marimo._plugins.ui._core.registry import UIElementRegistry
from marimo._runtime.cell_lifecycle_registry import CellLifecycleRegistry
from marimo._runtime.context.types import (
ExecutionContext,
@@ -83,12 +84,11 @@ def initialize_script_context(app: InternalApp, stream: Stream) -> None:
Must be called exactly once for each client thread.
"""
- from marimo._plugins.ui._core.registry import NoopUIElementRegistry
from marimo._runtime.virtual_file import VirtualFileRegistry
runtime_context = ScriptRuntimeContext(
_app=app,
- ui_element_registry=NoopUIElementRegistry(),
+ ui_element_registry=UIElementRegistry(),
function_registry=FunctionRegistry(),
cell_lifecycle_registry=CellLifecycleRegistry(),
virtual_file_registry=VirtualFileRegistry(),
diff --git a/tests/_cli/snapshots/script.txt b/tests/_cli/snapshots/script.txt
index 72e63c6357..bd0e2479fc 100644
--- a/tests/_cli/snapshots/script.txt
+++ b/tests/_cli/snapshots/script.txt
@@ -1,5 +1,5 @@
-__generated_with = "0.4.2"
+__generated_with = "0.4.3"
import marimo as mo
diff --git a/tests/_smoke_tests/test_run_as_script.py b/tests/_smoke_tests/test_run_as_script.py
new file mode 100644
index 0000000000..734a85102c
--- /dev/null
+++ b/tests/_smoke_tests/test_run_as_script.py
@@ -0,0 +1,121 @@
+from __future__ import annotations
+
+import inspect
+import subprocess
+import sys
+from typing import TYPE_CHECKING
+
+import pytest
+from marimo._dependencies.dependencies import DependencyManager
+
+if TYPE_CHECKING:
+ import pathlib
+
+
+class TestRunTutorialsAsScripts:
+ def assert_not_errored(self, p: subprocess.CompletedProcess[bytes]) -> None:
+ assert p.returncode == 0
+ assert not any(
+ line.startswith("Traceback")
+ for line in p.stderr.decode().splitlines()
+ )
+ assert not any(
+ line.startswith("Traceback")
+ for line in p.stdout.decode().splitlines()
+ )
+
+ def assert_errored(
+ self, p: subprocess.CompletedProcess[bytes], reason: str
+ ) -> None:
+ assert p.returncode != 0
+ assert reason in p.stderr.decode()
+
+ @pytest.mark.skipif(
+ condition=sys.platform == "win32", reason="Encoding error"
+ )
+ def test_run_intro_tutorial(self, tmp_path: pathlib.Path) -> None:
+ from marimo._tutorials import intro
+
+ file = tmp_path / "intro.py"
+ file.write_text(inspect.getsource(intro))
+ p = subprocess.run(
+ ["python", str(file)],
+ capture_output=True,
+ )
+ self.assert_not_errored(p)
+
+ @pytest.mark.skipif(
+ condition=sys.platform == "win32", reason="Encoding error"
+ )
+ def test_run_ui_tutorial(self, tmp_path: pathlib.Path) -> None:
+ from marimo._tutorials import ui as mod
+
+ file = tmp_path / "mod.py"
+ file.write_text(inspect.getsource(mod))
+ p = subprocess.run(
+ ["python", str(file)],
+ capture_output=True,
+ )
+ self.assert_not_errored(p)
+
+ @pytest.mark.skipif(
+ condition=sys.platform == "win32", reason="Encoding error"
+ )
+ def test_run_dataflow_tutorial(self, tmp_path: pathlib.Path) -> None:
+ from marimo._tutorials import dataflow as mod
+
+ file = tmp_path / "mod.py"
+ file.write_text(inspect.getsource(mod))
+ p = subprocess.run(
+ ["python", str(file)],
+ capture_output=True,
+ )
+ self.assert_errored(p, reason="CycleError")
+
+ @pytest.mark.skipif(
+ condition=sys.platform == "win32", reason="Encoding error"
+ )
+ def test_run_layout_tutorial(self, tmp_path: pathlib.Path) -> None:
+ from marimo._tutorials import layout as mod
+
+ file = tmp_path / "mod.py"
+ file.write_text(inspect.getsource(mod))
+ p = subprocess.run(
+ ["python", str(file)],
+ capture_output=True,
+ )
+ self.assert_not_errored(p)
+
+ @pytest.mark.skipif(
+ condition=not DependencyManager.has_matplotlib(),
+ reason="requires matplotlib",
+ )
+ @pytest.mark.skipif(
+ condition=sys.platform == "win32", reason="Encoding error"
+ )
+ def test_run_plots_tutorial(self, tmp_path: pathlib.Path) -> None:
+ from marimo._tutorials import plots as mod
+
+ file = tmp_path / "mod.py"
+ file.write_text(inspect.getsource(mod))
+ p = subprocess.run(
+ ["python", str(file)],
+ capture_output=True,
+ )
+ self.assert_not_errored(p)
+
+ @pytest.mark.skipif(
+ condition=sys.platform == "win32", reason="Encoding error"
+ )
+ def test_run_marimo_for_jupyter_users_tutorial(
+ self, tmp_path: pathlib.Path
+ ) -> None:
+ from marimo._tutorials import marimo_for_jupyter_users as mod
+
+ file = tmp_path / "mod.py"
+ file.write_text(inspect.getsource(mod))
+ p = subprocess.run(
+ ["python", str(file)],
+ capture_output=True,
+ )
+ self.assert_errored(p, reason="MultipleDefinitionError")