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.

Variable messages (#125)

Akshay Agrawal09197aa33cfd
diff --git a/frontend/e2e-tests/cells.spec.ts b/frontend/e2e-tests/cells.spec.ts
index 06dcb404d7..9866bcbe78 100644
--- a/frontend/e2e-tests/cells.spec.ts
+++ b/frontend/e2e-tests/cells.spec.ts
@@ -32,7 +32,7 @@ test("keeps re-renders from growing", async ({ page }) => {
// unexpectedly, it is a sign that something is causing cells to re-render.
// It is also ok to decrease the count if we find a way to reduce the number
// of renders.
- await expect(cellRenderCount).toBe("10");
+ await expect(cellRenderCount).toBe("8");
});
/**
diff --git a/marimo/_messaging/message_types.py b/marimo/_messaging/message_types.py
index 14f3f20a5d..617f18aa1c 100644
--- a/marimo/_messaging/message_types.py
+++ b/marimo/_messaging/message_types.py
@@ -71,6 +71,40 @@ class CompletionResult:
options: list[CompletionOption]
+@dataclass
+class VariableDeclaration:
+ name: str
+ declared_by: list[CellId_t]
+ used_by: list[CellId_t]
+
+
+@dataclass
+class VariableValue:
+ name: str
+ datatype: Optional[str]
+ value: Optional[str]
+
+ def __init__(self, name: str, value: object):
+ self.name = name
+ self.datatype = str(type(value)) if value is not None else None
+ try:
+ self.value = str(value)[:50]
+ except Exception:
+ self.value = None
+
+
+@dataclass
+class Variables:
+ name: ClassVar[str] = "variables"
+ variables: list[VariableDeclaration]
+
+
+@dataclass
+class VariableValues:
+ name: ClassVar[str] = "variable-values"
+ variables: list[VariableValue]
+
+
MessageType = Union[
CellOp,
RemoveUIElements,
@@ -78,6 +112,8 @@ MessageType = Union[
CompletedRun,
KernelReady,
CompletionResult,
+ Variables,
+ VariableValues,
]
diff --git a/marimo/_messaging/messages.py b/marimo/_messaging/messages.py
index 40da7b02d2..c09f541128 100644
--- a/marimo/_messaging/messages.py
+++ b/marimo/_messaging/messages.py
@@ -12,6 +12,10 @@ from marimo._messaging.message_types import (
CompletedRun,
Interrupted,
RemoveUIElements,
+ VariableDeclaration,
+ Variables,
+ VariableValue,
+ VariableValues,
serialize,
)
from marimo._messaging.streams import OUTPUT_MAX_BYTES
@@ -124,6 +128,20 @@ def write_marimo_error(
)
+def write_variables(data: list[VariableDeclaration]) -> None:
+ get_context().stream.write(
+ op=Variables.name,
+ data=serialize(Variables(variables=data)),
+ )
+
+
+def write_variable_values(data: list[VariableValue]) -> None:
+ get_context().stream.write(
+ op=VariableValues.name,
+ data=serialize(VariableValues(variables=data)),
+ )
+
+
def write_interrupted() -> None:
get_context().stream.write(op=Interrupted.name, data={})
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index 9083f8900c..b5d5846b68 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -27,6 +27,7 @@ from marimo._messaging.errors import (
MarimoSyntaxError,
UnknownError,
)
+from marimo._messaging.message_types import VariableDeclaration, VariableValue
from marimo._messaging.messages import (
write_completed_run,
write_disabled_transitively,
@@ -38,6 +39,8 @@ from marimo._messaging.messages import (
write_queued,
write_remove_ui_elements,
write_stale,
+ write_variable_values,
+ write_variables,
)
from marimo._messaging.streams import Stderr, Stdout, Stream, redirect_streams
from marimo._output.rich_help import mddoc
@@ -507,6 +510,16 @@ class Kernel:
status=None,
)
+ write_variables(
+ [
+ VariableDeclaration(
+ name=variable,
+ declared_by=list(declared_by),
+ used_by=list(self.graph.get_referring_cells(variable)),
+ )
+ for variable, declared_by in self.graph.definitions.items()
+ ]
+ )
return descendants
def _run_cells(self, cell_ids: set[CellId_t]) -> None:
@@ -568,6 +581,20 @@ class Kernel:
with self._install_execution_context(cell_id):
run_result = runner.run(cell_id)
+ write_variable_values(
+ [
+ VariableValue(
+ name=variable,
+ value=(
+ self.globals[variable]
+ if variable in self.globals
+ else None
+ ),
+ )
+ for variable in self.graph.cells[cell_id].defs
+ ]
+ )
+
cell.set_status(status="idle")
if run_result.success():
formatted_output = run_result.format_output()