MCPcopy
hub / github.com/marimo-team/marimo / _ctx

Function _ctx

tests/_code_mode/test_code_mode_context.py:33–55  ·  view source on GitHub ↗

Build an AsyncCodeModeContext with a document snapshot from the kernel. `extra_doc_cells` adds cells to the document that are *not* in the kernel graph, simulating cells that exist on disk but were never run.

(
    k: Kernel,
    extra_doc_cells: list[NotebookCell] | None = None,
)

Source from the content-addressed store, hash-verified

31
32@contextmanager
33def _ctx(
34 k: Kernel,
35 extra_doc_cells: list[NotebookCell] | None = None,
36) -> Generator[AsyncCodeModeContext, None, None]:
37 """Build an AsyncCodeModeContext with a document snapshot from the kernel.
38
39 `extra_doc_cells` adds cells to the document that are *not* in the
40 kernel graph, simulating cells that exist on disk but were never run.
41 """
42 cells = [
43 NotebookCell(id=cid, code=cell.code, name="", config=cell.config)
44 for cid, cell in k.graph.cells.items()
45 ]
46 if extra_doc_cells:
47 cells.extend(extra_doc_cells)
48 doc = NotebookDocument(cells)
49 with notebook_document_context(doc):
50 # Staleness check coverage lives in test_staleness.py.
51 ctx = AsyncCodeModeContext(k, skip_staleness_check=True)
52 # Use a deterministic seed in tests for snapshot stability.
53 ctx._id_generator = CellIdGenerator(seed=7)
54 ctx._id_generator.seen_ids = set(doc.cell_ids)
55 yield ctx
56
57
58def _tx_ops(k: Kernel) -> list[dict[str, object]]:

Calls 6

NotebookCellClass · 0.90
NotebookDocumentClass · 0.90
CellIdGeneratorClass · 0.90
itemsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…