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: output append should not render lazily (#697)

Akshay Agrawal9074f5e6ed74
diff --git a/marimo/_runtime/output/_output.py b/marimo/_runtime/output/_output.py
index 86523bd2fe..2687765679 100644
--- a/marimo/_runtime/output/_output.py
+++ b/marimo/_runtime/output/_output.py
@@ -39,7 +39,7 @@ def replace(value: object) -> None:
elif value is None:
ctx.kernel.execution_context.output = None
else:
- ctx.kernel.execution_context.output = [value]
+ ctx.kernel.execution_context.output = [formatting.as_html(value)]
write_internal(cell_id=ctx.kernel.execution_context.cell_id, value=value)
@@ -59,9 +59,9 @@ def append(value: object) -> None:
return
if ctx.kernel.execution_context.output is None:
- ctx.kernel.execution_context.output = [value]
+ ctx.kernel.execution_context.output = [formatting.as_html(value)]
else:
- ctx.kernel.execution_context.output.append(value)
+ ctx.kernel.execution_context.output.append(formatting.as_html(value))
write_internal(
cell_id=ctx.kernel.execution_context.cell_id,
value=vstack(ctx.kernel.execution_context.output),
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index fb3a0e28d2..d95c8203dd 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -45,6 +45,7 @@ from marimo._messaging.ops import (
)
from marimo._messaging.streams import Stderr, Stdin, Stdout, Stream
from marimo._output import formatting
+from marimo._output.hypertext import Html
from marimo._output.rich_help import mddoc
from marimo._plugins.core.web_component import JSONType
from marimo._plugins.ui._core.ui_element import MarimoConvertValueException
@@ -143,7 +144,7 @@ class ExecutionContext:
cell_id: CellId_t
setting_element_value: bool
# output object set imperatively
- output: Optional[list[object]] = None
+ output: Optional[list[Html]] = None
@dataclasses.dataclass
diff --git a/marimo/_tutorials/intro.py b/marimo/_tutorials/intro.py
index 481f1ec466..ef211af05c 100644
--- a/marimo/_tutorials/intro.py
+++ b/marimo/_tutorials/intro.py
@@ -1,3 +1,4 @@
+# Copyright 2024 Marimo. All rights reserved.
import marimo
__generated_with = "0.1.88"
diff --git a/tests/_runtime/output/test_output.py b/tests/_runtime/output/test_output.py
index e8e810222f..b0b1c2f6c8 100644
--- a/tests/_runtime/output/test_output.py
+++ b/tests/_runtime/output/test_output.py
@@ -35,3 +35,33 @@ def test_spinner_removed(
mocked_kernel.stream.messages[i + 1][1]["output"]["data"] == ""
)
assert found_progress
+
+
+def test_mutating_appended_outputs(
+ mocked_kernel: MockedKernel, exec_req: ExecReqProvider
+) -> None:
+ # append the same object multiple times, but mutate it in between
+ # appends. make sure the final output message contains the both
+ # versions of the object (before and after mutation)
+ mocked_kernel.k.run(
+ [
+ exec_req.get(
+ """
+ import marimo as mo
+
+ x = ["before"]
+ mo.output.append(x)
+ x[0] = "after"
+ mo.output.append(x)
+ """
+ )
+ ]
+ )
+ outputs: list[str] = []
+ for msg in mocked_kernel.stream.messages:
+ if msg[0] == "cell-op" and msg[1]["output"] is not None:
+ outputs.append(msg[1]["output"]["data"])
+ assert len(outputs) == 2
+ assert "before" in outputs[0]
+ assert "before" in outputs[1]
+ assert "after" in outputs[1]