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.

Avoid double formatting in mo.output.replace (#9909)

Xiangyu Li7befad37aee8
diff --git a/marimo/_runtime/output/_output.py b/marimo/_runtime/output/_output.py
index a8ed031c01..cedd23020b 100644
--- a/marimo/_runtime/output/_output.py
+++ b/marimo/_runtime/output/_output.py
@@ -49,8 +49,11 @@ def replace(value: object) -> None:
output = ctx.execution_context.output
output.clear()
if value is not None:
- output.append(formatting.as_html(value))
- write_internal(cell_id=ctx.execution_context.cell_id, value=value)
+ html = formatting.as_html(value)
+ output.append(html)
+ write_internal(cell_id=ctx.execution_context.cell_id, value=html)
+ else:
+ write_internal(cell_id=ctx.execution_context.cell_id, value=value)
@mddoc
diff --git a/tests/_runtime/output/test_output.py b/tests/_runtime/output/test_output.py
index a8ab47deac..6dbf2b60e4 100644
--- a/tests/_runtime/output/test_output.py
+++ b/tests/_runtime/output/test_output.py
@@ -67,6 +67,30 @@ async def test_mutating_appended_outputs(
assert "after" in outputs[1]
+async def test_replace_formats_output_once(
+ mocked_kernel: MockedKernel, exec_req: ExecReqProvider
+) -> None:
+ await mocked_kernel.k.run(
+ [
+ exec_req.get(
+ """
+ import marimo as mo
+
+ class Probe:
+ count = 0
+
+ def _repr_html_(self):
+ Probe.count += 1
+ return f"<div>formatted {Probe.count}</div>"
+
+ mo.output.replace(Probe())
+ assert Probe.count == 1
+ """
+ )
+ ]
+ )
+
+
async def test_nested_output(
mocked_kernel: MockedKernel, exec_req: ExecReqProvider
) -> None: