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
fix: make _CellsView iterate over NotebookCell objects instead of cell IDs (#9039)
Myles Scolnicke787d1776866
diff --git a/marimo/_code_mode/_context.py b/marimo/_code_mode/_context.pyindex a5608f281f..74c77d7e06 100644--- a/marimo/_code_mode/_context.py+++ b/marimo/_code_mode/_context.py@@ -117,7 +117,7 @@ def get_context(*, skip_validation: bool = False) -> AsyncCodeModeContext:class _CellsView:- """Read-only, ordered-dict-like view over notebook cells.+ """Read-only, ordered view over notebook cells.Supports lookup by integer index, cell ID, or cell name::@@ -126,12 +126,16 @@ class _CellsView:ctx.cells["Abcd1234"] # by cell IDctx.cells["my_cell"] # by cell name- Dict-like iteration::+ Iteration yields ``NotebookCell`` objects directly::++ for cell in ctx.cells:+ print(cell.id, cell.code)++ Dict-like access is also available::- for cid, cell in ctx.cells.items():- ...ctx.cells.keys() # list of CellId_tctx.cells.values() # list of NotebookCell+ ctx.cells.items() # list of (CellId_t, NotebookCell)"my_cell" in ctx.cells # membership test"""@@ -185,8 +189,9 @@ class _CellsView:return self._doc.cells[key]return self._doc.get_cell(self._resolve(key))- def __iter__(self) -> Iterator[CellId_t]:- yield from self._doc+ def __iter__(self) -> Iterator[NotebookCell]:+ for cell_id in self._doc.cell_ids:+ yield self._doc.get_cell(cell_id)def __contains__(self, key: object) -> bool:if isinstance(key, int):diff --git a/tests/_code_mode/test_cells_view.py b/tests/_code_mode/test_cells_view.pyindex 2e0ec0949d..a5eaf3b204 100644--- a/tests/_code_mode/test_cells_view.py+++ b/tests/_code_mode/test_cells_view.py@@ -135,7 +135,7 @@ class TestCellsViewIteration:with _ctx(k) as ctx:assert len(ctx.cells) == 2- async def test_iteration_yields_cell_ids(self, k: Kernel) -> None:+ async def test_iteration_yields_cells(self, k: Kernel) -> None:await k.run([cmd(cell_id="a", code="x = 1"),@@ -143,8 +143,9 @@ class TestCellsViewIteration:])with _ctx(k) as ctx:- ids = list(ctx.cells)- assert ids == ["a", "b"]+ cells = list(ctx.cells)+ assert [c.id for c in cells] == ["a", "b"]+ assert [c.code for c in cells] == ["x = 1", "y = 2"]async def test_keys(self, k: Kernel) -> None:await k.run(@@ -273,6 +274,40 @@ class TestCellsViewRepr:assert "[11]" not in r+# ------------------------------------------------------------------+# NotebookCell.__repr__+# ------------------------------------------------------------------+++class TestNotebookCellRepr:+ def test_repr_basic(self) -> None:+ c = _cell("a1", "x = 1")+ assert repr(c) == "NotebookCell(id='a1', code='x = 1')"++ def test_repr_with_name(self) -> None:+ c = _cell("a1", "import marimo as mo", name="setup")+ assert repr(c) == (+ "NotebookCell(id='a1', name='setup', code='import marimo as mo')"+ )++ def test_repr_long_code_truncated(self) -> None:+ long_code = "x = " + "a" * 100+ c = _cell("a1", long_code)+ r = repr(c)+ assert "..." in r+ assert long_code[:80] in r++ def test_repr_multiline_shows_first_line(self) -> None:+ c = _cell("a1", "line1\nline2\nline3")+ r = repr(c)+ assert "line1..." in r+ assert "line2" not in r++ def test_repr_empty_code(self) -> None:+ c = _cell("a1", "")+ assert repr(c) == "NotebookCell(id='a1', code='')"++# ------------------------------------------------------------------# find()# ------------------------------------------------------------------