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.

Add `expand_outputs` as an argument to `ctx.edit_cell` and `ctx.create_cell` (#10974)

Martha Cryan92fca4921a09
diff --git a/marimo/_code_mode/_context.py b/marimo/_code_mode/_context.py
index 14ac4fc9f6..e1f35bebd8 100644
--- a/marimo/_code_mode/_context.py
+++ b/marimo/_code_mode/_context.py
@@ -28,6 +28,8 @@ from dataclasses import dataclass
from enum import Enum
from typing import TYPE_CHECKING, Any, Literal, Protocol, cast, overload
+from msgspec.structs import replace as structs_replace
+
from marimo import _loggers
from marimo._ast.cell import (
CellConfig,
@@ -1056,6 +1058,7 @@ class AsyncCodeModeContext:
after: str | None = None,
hide_code: bool = True,
disabled: bool = False,
+ expand_output: bool = False,
column: int | None = None,
name: str | None = None,
) -> CellId_t:
@@ -1094,6 +1097,8 @@ class AsyncCodeModeContext:
Defaults to True.
disabled (bool): Prevent the cell from executing.
Defaults to False.
+ expand_output (bool): Show the cell's output in full instead of
+ clamping it to a fixed height. Defaults to False.
column (int, optional): Column index for multi-column layouts.
name (str, optional): Cell names are a human-facing label,
reserved for special cases (e.g. `"setup"`). Prefer
@@ -1107,7 +1112,10 @@ class AsyncCodeModeContext:
cell_id, resolved_name = self._resolve_new_cell(name)
config = CellConfig(
- hide_code=hide_code, disabled=disabled, column=column
+ hide_code=hide_code,
+ disabled=disabled,
+ expand_output=expand_output,
+ column=column,
)
before_id = (
@@ -1127,6 +1135,24 @@ class AsyncCodeModeContext:
self._pending_adds[cell_id] = op
return cell_id
+ def _current_config(self, cell_id: CellId_t) -> CellConfig:
+ """The cell's config as of the last op queued in this batch.
+
+ Ops aren't applied to the kernel until the context exits, so a
+ cell created or configured earlier in the same batch isn't in
+ `cell_metadata` yet. Scan the queue first so merging into the
+ existing config doesn't clobber those pending changes.
+ """
+ for op in reversed(self._ops):
+ if op.cell_id != cell_id or not isinstance(
+ op, (_AddOp, _UpdateOp)
+ ):
+ continue
+ if op.config is not None:
+ return op.config
+ meta = self._kernel.cell_metadata.get(cell_id)
+ return meta.config if meta else CellConfig()
+
def edit_cell(
self,
target: str,
@@ -1134,6 +1160,7 @@ class AsyncCodeModeContext:
*,
hide_code: bool | None = None,
disabled: bool | None = None,
+ expand_output: bool | None = None,
column: int | None = None,
name: str | None = None,
) -> None:
@@ -1172,6 +1199,8 @@ class AsyncCodeModeContext:
code (str, optional): New Python source code. None keeps existing.
hide_code (bool, optional): Collapse the code editor. None keeps existing.
disabled (bool, optional): Prevent the cell from executing. None keeps existing.
+ expand_output (bool, optional): Show the cell's output in full
+ instead of clamping it to a fixed height. None keeps existing.
column (int, optional): Column index for multi-column layouts. None keeps existing.
name (str, optional): New name for the cell. None keeps existing.
"""
@@ -1227,21 +1256,22 @@ class AsyncCodeModeContext:
cell_id, tracker.get_stale_cells(self._document)
)
- # Build config only if any config kwarg was explicitly set.
+ # Build config only if any config kwarg was explicitly set,
+ # starting from the existing config so unspecified fields persist.
config: CellConfig | None = None
- if hide_code is not None or disabled is not None or column is not None:
- # Start from existing config and override provided fields.
- meta = self._kernel.cell_metadata.get(cell_id)
- existing = meta.config if meta else CellConfig()
- config = CellConfig(
- hide_code=hide_code
- if hide_code is not None
- else existing.hide_code,
- disabled=disabled
- if disabled is not None
- else existing.disabled,
- column=column if column is not None else existing.column,
- expand_output=existing.expand_output,
+ overrides = {
+ key: value
+ for key, value in (
+ ("hide_code", hide_code),
+ ("disabled", disabled),
+ ("expand_output", expand_output),
+ ("column", column),
+ )
+ if value is not None
+ }
+ if overrides:
+ config = structs_replace(
+ self._current_config(cell_id), **overrides
)
self._ops.append(
diff --git a/tests/_code_mode/test_code_mode_context.py b/tests/_code_mode/test_code_mode_context.py
index 5df4855388..e1ee10a430 100644
--- a/tests/_code_mode/test_code_mode_context.py
+++ b/tests/_code_mode/test_code_mode_context.py
@@ -108,6 +108,15 @@ class TestAddCell:
]
)
+ async def test_add_with_expand_output(self, k: Kernel) -> None:
+ with _ctx(k) as ctx:
+ async with ctx as nb:
+ cid = nb.create_cell("x = 1", expand_output=True)
+ nb.run_cell(cid)
+
+ assert k.cell_metadata[cid].config.expand_output is True
+ assert k.graph.cells[cid].config.expand_output is True
+
async def test_add_appends_by_default(self, k: Kernel) -> None:
await k.run(
[
@@ -332,6 +341,101 @@ class TestUpdateCell:
]
)
+ async def test_update_expand_output(self, k: Kernel) -> None:
+ await k.run([ExecuteCellCommand(cell_id=CellId_t("0"), code="x = 1")])
+
+ with _ctx(k) as ctx:
+ _clear_messages(k)
+
+ async with ctx as nb:
+ nb.edit_cell("0", expand_output=True)
+
+ assert k.cell_metadata["0"].config.expand_output is True
+ assert k.graph.cells["0"].config.expand_output is True
+ assert _tx_ops(k) == snapshot(
+ [
+ {
+ "type": "set-config",
+ "cellId": "0",
+ "column": None,
+ "disabled": False,
+ "hideCode": False,
+ "expandOutput": True,
+ },
+ {"type": "reorder-cells", "cellIds": ("0",)},
+ ]
+ )
+
+ async def test_update_preserves_expand_output(self, k: Kernel) -> None:
+ """Editing another config field leaves expand_output untouched."""
+ await k.run([ExecuteCellCommand(cell_id=CellId_t("0"), code="x = 1")])
+
+ with _ctx(k) as ctx:
+ async with ctx as nb:
+ nb.edit_cell("0", expand_output=True)
+
+ with _ctx(k) as ctx:
+ async with ctx as nb:
+ nb.edit_cell("0", hide_code=True)
+
+ assert k.cell_metadata["0"].config.expand_output is True
+ assert k.cell_metadata["0"].config.hide_code is True
+
+ async def test_update_collapse_output(self, k: Kernel) -> None:
+ """expand_output=False turns off a previously expanded output."""
+ await k.run([ExecuteCellCommand(cell_id=CellId_t("0"), code="x = 1")])
+
+ with _ctx(k) as ctx:
+ async with ctx as nb:
+ nb.edit_cell("0", expand_output=True)
+
+ with _ctx(k) as ctx:
+ async with ctx as nb:
+ nb.edit_cell("0", expand_output=False)
+
+ assert k.cell_metadata["0"].config.expand_output is False
+ assert k.graph.cells["0"].config.expand_output is False
+
+ async def test_update_config_of_pending_create(self, k: Kernel) -> None:
+ """Editing a cell created in the same batch keeps its create config.
+
+ The create is still queued, so the cell has no entry in
+ `cell_metadata` to merge against yet.
+ """
+ with _ctx(k) as ctx:
+ async with ctx as nb:
+ cid = nb.create_cell("x = 1", hide_code=True, disabled=True)
+ nb.edit_cell(cid, expand_output=True)
+
+ cfg = k.cell_metadata[cid].config
+ assert cfg.expand_output is True
+ assert cfg.hide_code is True
+ assert cfg.disabled is True
+
+ # And in the other direction: an unrelated edit must not reset
+ # a config field the create set.
+ with _ctx(k) as ctx:
+ async with ctx as nb:
+ other = nb.create_cell("y = 1", expand_output=True)
+ nb.edit_cell(other, hide_code=False)
+
+ cfg = k.cell_metadata[other].config
+ assert cfg.expand_output is True
+ assert cfg.hide_code is False
+
+ async def test_two_config_edits_in_one_batch(self, k: Kernel) -> None:
+ """A second edit merges into the first edit's queued config."""
+ await k.run([ExecuteCellCommand(cell_id=CellId_t("0"), code="x = 1")])
+
+ with _ctx(k) as ctx:
+ async with ctx as nb:
+ nb.edit_cell("0", hide_code=True)
+ nb.edit_cell("0", expand_output=True)
+
+ cfg = k.cell_metadata["0"].config
+ assert cfg.hide_code is True
+ assert cfg.expand_output is True
+
async def test_update_code_skips_formatting_when_disabled(
self, k: Kernel
) -> None: