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: catch exceptions when updating UIElement value (#77)
Akshay Agrawal04e7353170e4
diff --git a/marimo/_plugins/ui/_core/ui_element.py b/marimo/_plugins/ui/_core/ui_element.pyindex 190dde3601..b85512277c 100644--- a/marimo/_plugins/ui/_core/ui_element.py+++ b/marimo/_plugins/ui/_core/ui_element.py@@ -29,6 +29,10 @@ T = TypeVar("T")LOGGER = _loggers.marimo_logger()+class MarimoConvertValueException(Exception):+ pass++@mddocclass UIElement(Html, Generic[S, T], metaclass=abc.ABCMeta):"""An HTML element with a value@@ -250,7 +254,11 @@ class UIElement(Html, Generic[S, T], metaclass=abc.ABCMeta):Calls the on_change handler with the element's new value as aside-effect."""- self._value = self._convert_value(value)+ try:+ self._value = self._convert_value(value)+ except MarimoConvertValueException:+ raise+if self._on_change is not None:self._on_change(self._value)diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex 764c56826b..b483f11cfa 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -39,6 +39,7 @@ from marimo._messaging.messages import (from marimo._messaging.streams import Stderr, Stdout, Stream, redirect_streamsfrom marimo._output.rich_help import mddocfrom marimo._plugins.ui._core.registry import UIElementRegistry+from marimo._plugins.ui._core.ui_element import MarimoConvertValueExceptionfrom marimo._runtime import cell_runner, dataflowfrom marimo._runtime.complete import completefrom marimo._runtime.context import (@@ -669,7 +670,34 @@ class Kernel:get_context().ui_element_registry.get_cell(object_id),setting_element_value=True,):- component._update(value)+ try:+ component._update(value)+ except MarimoConvertValueException:+ # Internal marimo error+ sys.stderr.write(+ "An exception was raised when updating a UIElement's "+ "value. This is a bug in marimo. Please copy "+ "the below traceback and paste it in an "+ "issue: https://github.com/marimo-team/marimo/issues\n"+ )+ tmpio = io.StringIO()+ traceback.print_exc(file=tmpio)+ tmpio.seek(0)+ sys.stderr.write(tmpio.read())+ # Don't run descendants+ continue+ except Exception:+ # User's on_change handler an exception ...+ sys.stderr.write(+ "An exception was raised by a "+ "UIElement's on_change handler:\n"+ )++ tmpio = io.StringIO()+ traceback.print_exc(file=tmpio)+ tmpio.seek(0)+ sys.stderr.write(tmpio.read())+bound_names = get_context().ui_element_registry.bound_names(object_id)diff --git a/marimo/_smoke_tests/raise_exception_on_change.py b/marimo/_smoke_tests/raise_exception_on_change.pynew file mode 100644index 0000000000..3859f3f339--- /dev/null+++ b/marimo/_smoke_tests/raise_exception_on_change.py@@ -0,0 +1,29 @@+# Copyright 2023 Marimo. All rights reserved.+import marimo++__generated_with = "0.1.5"+app = marimo.App()+++def __():+ import marimo as mo+ return mo,+++def __():+ def error(v):+ raise ValueError(str(v))+ return error,+++def __(error, mo):+ s = mo.ui.slider(1, 10, on_change=lambda v: error(v))+ s+ return s,+++if __name__ == "__main__":+ app.run()diff --git a/marimo/_smoke_tests/state.py b/marimo/_smoke_tests/state.pyindex fa77406212..29e55c5a3e 100644--- a/marimo/_smoke_tests/state.py+++ b/marimo/_smoke_tests/state.py@@ -1,3 +1,4 @@+# Copyright 2023 Marimo. All rights reserved.import marimo__generated_with = "0.1.4"