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: don't broadcast variables from embedded apps (#7896)

Akshay Agrawal794b05f40d7f
diff --git a/marimo/_runtime/runner/hooks_post_execution.py b/marimo/_runtime/runner/hooks_post_execution.py
index bb52d8620f..617722f206 100644
--- a/marimo/_runtime/runner/hooks_post_execution.py
+++ b/marimo/_runtime/runner/hooks_post_execution.py
@@ -33,7 +33,10 @@ from marimo._messaging.variables import create_variable_value
from marimo._output import formatting
from marimo._plugins.ui._core.ui_element import UIElement
from marimo._runtime.context.kernel_context import KernelRuntimeContext
-from marimo._runtime.context.types import get_context, get_global_context
+from marimo._runtime.context.types import (
+ get_context,
+ get_global_context,
+)
from marimo._runtime.control_flow import MarimoInterrupt, MarimoStopError
from marimo._runtime.runner import cell_runner
from marimo._runtime.side_effect import SideEffect
@@ -111,14 +114,24 @@ def _set_run_result_status(
cell.set_run_result_status("success")
+def _should_broadcast_data() -> bool:
+ ctx = get_context()
+ is_edit_mode = (
+ isinstance(ctx, KernelRuntimeContext)
+ and ctx.session_mode == SessionMode.EDIT
+ )
+ # We don't broadcast data in embedded contexts, since the variables panel,
+ # etc. should only show the top-level graph's variables.
+ return is_edit_mode and not ctx.is_embedded()
+
+
@kernel_tracer.start_as_current_span("broadcast_variables")
def _broadcast_variables(
cell: CellImpl,
runner: cell_runner.Runner,
run_result: cell_runner.RunResult,
) -> None:
- # Skip if not in edit mode
- if not _is_edit_mode():
+ if not _should_broadcast_data():
return
del run_result
@@ -141,8 +154,7 @@ def _broadcast_datasets(
runner: cell_runner.Runner,
run_result: cell_runner.RunResult,
) -> None:
- # Skip if not in edit mode
- if not _is_edit_mode():
+ if not _should_broadcast_data():
return
del run_result
@@ -164,8 +176,7 @@ def _broadcast_data_source_connection(
runner: cell_runner.Runner,
run_result: cell_runner.RunResult,
) -> None:
- # Skip if not in edit mode
- if not _is_edit_mode():
+ if not _should_broadcast_data():
return
del run_result
@@ -197,8 +208,7 @@ def _broadcast_duckdb_datasource(
runner: cell_runner.Runner,
run_result: cell_runner.RunResult,
) -> None:
- # Skip if not in edit mode
- if not _is_edit_mode():
+ if not _should_broadcast_data():
return
del run_result
@@ -439,14 +449,6 @@ def _reset_matplotlib_context(
exec("__marimo__._output.mpl.close_figures()", runner.glbls)
-def _is_edit_mode() -> bool:
- ctx = get_context()
- return (
- isinstance(ctx, KernelRuntimeContext)
- and ctx.session_mode == SessionMode.EDIT
- )
-
-
POST_EXECUTION_HOOKS: list[PostExecutionHookType] = [
_set_imported_defs,
_set_run_result_status,
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index 7995ff43c7..e904b0284b 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -1332,22 +1332,23 @@ class Kernel:
# Always broadcast Variables message after graph mutation to ensure
# frontend has the latest dependency information
- broadcast_notification(
- VariablesNotification(
- variables=[
- VariableDeclarationNotification(
- name=variable,
- declared_by=list(declared_by),
- used_by=list(
- self.graph.get_referring_cells(
- variable, language="python"
- )
- ),
- )
- for variable, declared_by in self.graph.definitions.items()
- ]
- ),
- )
+ if not get_context().is_embedded():
+ broadcast_notification(
+ VariablesNotification(
+ variables=[
+ VariableDeclarationNotification(
+ name=variable,
+ declared_by=list(declared_by),
+ used_by=list(
+ self.graph.get_referring_cells(
+ variable, language="python"
+ )
+ ),
+ )
+ for variable, declared_by in self.graph.definitions.items()
+ ]
+ ),
+ )
stale_cells = (
set(
@@ -1945,7 +1946,7 @@ class Kernel:
# Entering undefined behavior territory ...
continue
- if variable_values:
+ if variable_values and not ctx.is_embedded():
broadcast_notification(
VariableValuesNotification(variables=variable_values),
self.stream,