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: cloning UI elements (#1818)

Akshay Agrawal251239e8798b
diff --git a/marimo/_plugins/ui/_core/ui_element.py b/marimo/_plugins/ui/_core/ui_element.py
index 12c809ae07..fa19a3d5c0 100644
--- a/marimo/_plugins/ui/_core/ui_element.py
+++ b/marimo/_plugins/ui/_core/ui_element.py
@@ -23,12 +23,15 @@ from marimo._output.hypertext import Html
from marimo._output.rich_help import mddoc
from marimo._plugins.core.web_component import JSONType, build_ui_plugin
from marimo._plugins.ui._core import ids
-from marimo._runtime.context import ContextNotInitializedError, get_context
+from marimo._runtime.context import (
+ ContextNotInitializedError,
+ RuntimeContext,
+ get_context,
+)
from marimo._runtime.functions import Function
if TYPE_CHECKING:
from marimo._plugins.ui._impl.input import form as form_plugin
- from marimo._runtime.context.types import RuntimeContext
# S: Type of frontend value
# - the initial value sent to the frontend must be of type S
@@ -429,6 +432,20 @@ class UIElement(Html, Generic[S, T], metaclass=abc.ABCMeta):
"""Callback to run after the kernel has processed a value update."""
return
+ def __deepcopy__(self, memo: dict[int, Any]) -> UIElement[S, T]:
+ # Custom deepcopy that excludes elements that can't be deepcopied
+ cls = self.__class__
+ result = cls.__new__(cls)
+ memo[id(self)] = result
+ for k, v in self.__dict__.items():
+ if isinstance(v, RuntimeContext):
+ setattr(result, k, v)
+ else:
+ setattr(result, k, copy.deepcopy(v, memo))
+ # Get a new object ID and function namespace
+ result._initialize(*self._args)
+ return result
+
def _clone(self) -> UIElement[S, T]:
"""Clone a UIElement, returning one with a different id
@@ -437,14 +454,4 @@ class UIElement(Html, Generic[S, T], metaclass=abc.ABCMeta):
Composite UIElement may need to override this method to run
their own side-effects.
"""
- # context is not deepcopy-able because it has a thread lock;
- # temporarily unset it during the clone.
- ctx = self._ctx
- try:
- self._ctx = None
- duplicate = copy.deepcopy(self)
- duplicate._ctx = ctx
- duplicate._initialize(*self._args)
- finally:
- self._ctx = ctx
- return duplicate
+ return copy.deepcopy(self)
diff --git a/marimo/_smoke_tests/bugs/1816.py b/marimo/_smoke_tests/bugs/1816.py
new file mode 100644
index 0000000000..9545e806a1
--- /dev/null
+++ b/marimo/_smoke_tests/bugs/1816.py
@@ -0,0 +1,29 @@
+# Copyright 2024 Marimo. All rights reserved.
+import marimo
+
+__generated_with = "0.7.6"
+app = marimo.App(width="medium")
+
+
+def __():
+ import marimo as mo
+ return mo,
+
+
+def __(mo):
+ dict1={"hello": mo.ui.text(label="world")}
+ dict2=mo.ui.dictionary({k: v.form() for k, v in dict1.items()})
+ dict2
+ return dict1, dict2
+
+
+def __(dict2):
+ dict2.value
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_plugins/ui/_impl/test_input.py b/tests/_plugins/ui/_impl/test_input.py
index 5a694640fd..3c370c05d5 100644
--- a/tests/_plugins/ui/_impl/test_input.py
+++ b/tests/_plugins/ui/_impl/test_input.py
@@ -426,6 +426,13 @@ def test_form_in_array_retains_on_change() -> None:
assert array[0]._on_change == on_change
+def test_form_in_dictionary_allowed() -> None:
+ checkbox = ui.checkbox()
+ form = checkbox.form()
+ d = ui.dictionary({"form": form})
+ assert checkbox._id != d["form"].element._id
+
+
# TODO(akshayka): test file