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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
feat: add output replace_by_index function (#1466)
Sean Turnerb470ceef2ad8
diff --git a/frontend/e2e-tests/output.spec.ts b/frontend/e2e-tests/output.spec.tsindex cdba73bf0f..767c46b1fc 100644--- a/frontend/e2e-tests/output.spec.ts+++ b/frontend/e2e-tests/output.spec.ts@@ -25,5 +25,9 @@ test("it can clear and append output", async ({ page }) => {// Test that Cleared does not existawait expect(page.getByText("Cleared!")).not.toBeVisible();+ // Test that Replaced by index is visible and To be replaced is not+ await expect(page.getByText("To be replaced.")).not.toBeVisible();+ await expect(page.getByText("Replaced by index!")).toBeVisible();+await takeScreenshot(page, __filename);});diff --git a/frontend/e2e-tests/py/output.py b/frontend/e2e-tests/py/output.pyindex 3d480abb36..3c3fdf0dc2 100644--- a/frontend/e2e-tests/py/output.py+++ b/frontend/e2e-tests/py/output.py@@ -55,5 +55,12 @@ def __(loop_append, mo):return+def __(mo):+ mo.output.append("To be replaced.")+ mo.output.replace_at_index(mo.md("Replaced by index!"), 0)+ return++if __name__ == "__main__":app.run()diff --git a/marimo/_runtime/output/__init__.py b/marimo/_runtime/output/__init__.pyindex 969121d9ac..bf8998a011 100644--- a/marimo/_runtime/output/__init__.py+++ b/marimo/_runtime/output/__init__.py@@ -5,6 +5,12 @@ __all__ = ["append","clear","replace",+ "replace_at_index",]-from marimo._runtime.output._output import append, clear, replace+from marimo._runtime.output._output import (+ append,+ clear,+ replace,+ replace_at_index,+)diff --git a/marimo/_runtime/output/_output.py b/marimo/_runtime/output/_output.pyindex e3d3c172a6..e7471ab4ed 100644--- a/marimo/_runtime/output/_output.py+++ b/marimo/_runtime/output/_output.py@@ -44,6 +44,36 @@ def replace(value: object) -> None:write_internal(cell_id=ctx.execution_context.cell_id, value=value)+@mddoc+def replace_at_index(value: object, idx: int) -> None:+ """Replace a cell's output at the given index with value.++ Call this function to replace an existing object in a cell's output. If idx+ is equal to the length of the output, this is equivalent to an append.++ **Args:**++ - `value`: new object to replace an existing object+ - `idx`: index of output to replace+ """++ ctx = get_context()+ if ctx.execution_context is None or ctx.execution_context.output is None:+ return+ elif idx > len(ctx.execution_context.output):+ raise IndexError(+ f"idx is {idx}, must be <= {len(ctx.execution_context.output)}"+ )+ elif idx == len(ctx.execution_context.output):+ ctx.execution_context.output.append(formatting.as_html(value))+ else:+ ctx.execution_context.output[idx] = formatting.as_html(value)+ write_internal(+ cell_id=ctx.execution_context.cell_id,+ value=vstack(ctx.execution_context.output),+ )++@mddocdef append(value: object) -> None:"""Append a new object to a cell's output.diff --git a/marimo/_smoke_tests/output.py b/marimo/_smoke_tests/output.pyindex e5cbbdad5a..00c47c145a 100644--- a/marimo/_smoke_tests/output.py+++ b/marimo/_smoke_tests/output.py@@ -107,5 +107,11 @@ def __(time):return+def __(mo):+ mo.output.append(mo.md("To be replaced."))+ mo.output.replace_at_index(mo.md("Replaced at index"), 0)++if __name__ == "__main__":app.run()