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: share context with post-hooks to prevent id collisions (#4529)

Dylan Madisetti641240d8faa1
diff --git a/marimo/_runtime/runner/cell_runner.py b/marimo/_runtime/runner/cell_runner.py
index 6f770204aa..35b13a8c16 100644
--- a/marimo/_runtime/runner/cell_runner.py
+++ b/marimo/_runtime/runner/cell_runner.py
@@ -586,11 +586,14 @@ class Runner:
with self.execution_context(cell_id) as exc_ctx:
run_result = await self.run(cell_id)
run_result.accumulated_output = exc_ctx.output
+ LOGGER.debug("Running post_execution hooks in context")
+ for post_hook in self.post_execution_hooks:
+ post_hook(cell, self, run_result)
else:
run_result = await self.run(cell_id)
- LOGGER.debug("Running post_execution hooks")
- for post_hook in self.post_execution_hooks:
- post_hook(cell, self, run_result)
+ LOGGER.debug("Running post_execution hooks out of context")
+ for post_hook in self.post_execution_hooks:
+ post_hook(cell, self, run_result)
LOGGER.debug("Running on_finish hooks")
for finish_hook in self.on_finish_hooks:
diff --git a/marimo/_runtime/runner/hooks_post_execution.py b/marimo/_runtime/runner/hooks_post_execution.py
index d7f9c19f3d..7b4b4c14f3 100644
--- a/marimo/_runtime/runner/hooks_post_execution.py
+++ b/marimo/_runtime/runner/hooks_post_execution.py
@@ -246,7 +246,7 @@ def _store_state_reference(
@kernel_tracer.start_as_current_span("broadcast_outputs")
def _broadcast_outputs(
cell: CellImpl,
- runner: cell_runner.Runner,
+ _runner: cell_runner.Runner,
run_result: cell_runner.RunResult,
) -> None:
# TODO: clean this logic up ...
@@ -265,25 +265,14 @@ def _broadcast_outputs(
run_result.success()
or isinstance(run_result.exception, MarimoStopError)
) and should_send_output:
-
- def format_output() -> formatting.FormattedOutput:
- formatted_output = formatting.try_format(run_result.output)
-
- if formatted_output.exception is not None:
- # Try a plain formatter; maybe an opinionated one failed.
- formatted_output = formatting.try_format(
- run_result.output, include_opinionated=False
- )
-
- if formatted_output.traceback is not None:
- write_traceback(formatted_output.traceback)
- return formatted_output
-
- if runner.execution_context is not None:
- with runner.execution_context(cell.cell_id):
- formatted_output = format_output()
- else:
- formatted_output = format_output()
+ formatted_output = formatting.try_format(run_result.output)
+ if formatted_output.exception is not None:
+ # Try a plain formatter; maybe an opinionated one failed.
+ formatted_output = formatting.try_format(
+ run_result.output, include_opinionated=False
+ )
+ if formatted_output.traceback is not None:
+ write_traceback(formatted_output.traceback)
CellOp.broadcast_output(
channel=CellChannel.OUTPUT,
diff --git a/marimo/_smoke_tests/issues/4515_ui_id_collision.py b/marimo/_smoke_tests/issues/4515_ui_id_collision.py
new file mode 100644
index 0000000000..bdc8446171
--- /dev/null
+++ b/marimo/_smoke_tests/issues/4515_ui_id_collision.py
@@ -0,0 +1,34 @@
+import marimo
+
+__generated_with = "0.12.9-dev12"
+app = marimo.App()
+
+
+def _():
+ import marimo as mo
+
+ slider = mo.ui.slider(1, 100)
+
+ class has_mime:
+ @staticmethod
+ def _mime_():
+ # post_execution_hook reuses the name cell index space
+ # as the general cell
+ # The temp value overwrites the one on the generally cell
+ # And because the temp value is "finalized"
+ # The cell reference is deleted
+ return slider._clone()._mime_()
+
+ has_mime()
+ return has_mime, mo, slider
+
+
+def _(slider):
+ slider.value
+ return
+
+
+if __name__ == "__main__":
+ app.run()