From ff3d9f82cd009377557131941824abff03b9153c Mon Sep 17 00:00:00 2001 From: Xiangyu Li <3367250374@qq.com> Date: Wed, 17 Jun 2026 21:49:22 +0800 Subject: [PATCH] Avoid double formatting in output replace --- marimo/_runtime/output/_output.py | 7 +++++-- tests/_runtime/output/test_output.py | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/marimo/_runtime/output/_output.py b/marimo/_runtime/output/_output.py index a8ed031c019..cedd23020b8 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 a8ab47deac3..6dbf2b60e43 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"
formatted {Probe.count}
" + + mo.output.replace(Probe()) + assert Probe.count == 1 + """ + ) + ] + ) + + async def test_nested_output( mocked_kernel: MockedKernel, exec_req: ExecReqProvider ) -> None: