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.

cleanup: refactor enabling/disabling logic into dataflow graph (#121)

Akshay Agrawal76e305a86159
diff --git a/marimo/_runtime/dataflow.py b/marimo/_runtime/dataflow.py
index 3ced2043b5..5aa2288699 100644
--- a/marimo/_runtime/dataflow.py
+++ b/marimo/_runtime/dataflow.py
@@ -142,6 +142,55 @@ class DirectedGraph:
self.cycles.add(tuple([(other_id, cell_id)] + path))
self.children[other_id].add(cell_id)
+ def disable_cell(self, cell_id: CellId_t) -> set[CellId_t]:
+ """
+ Disables a cell in the graph.
+
+ Does not mutate the graph (but does mutate cell statuses).
+
+ Returns the ids of descendants that are disabled transitively.
+ """
+ if cell_id not in self.cells:
+ raise ValueError(f"Cell {cell_id} not found")
+
+ disabled_transitively: set[CellId_t] = set()
+ for cid in transitive_closure(self, set([cell_id])) - set([cell_id]):
+ cell = self.cells[cid]
+ if not cell.stale:
+ cell.set_status(status="disabled-transitively")
+ disabled_transitively.add(cid)
+ return disabled_transitively
+
+ def enable_cell(
+ self, cell_id: CellId_t
+ ) -> tuple[set[CellId_t], set[CellId_t]]:
+ """
+ Enables a cell in the graph.
+
+ Does not mutate the graph (but does mutate cell statuses).
+
+ Returns:
+ - set of cells that were stale and should be re-run
+ - set of cells that were disabled transitively and now idle
+ """
+ if cell_id not in self.cells:
+ raise ValueError(f"Cell {cell_id} not found")
+
+ cells_to_run: set[CellId_t] = set()
+ cells_idle: set[CellId_t] = set()
+ for cid in transitive_closure(self, set([cell_id])):
+ if not self.is_disabled(cid):
+ child = self.cells[cid]
+ if child.stale:
+ # cell was previously disabled, is no longer
+ # disabled, and is stale: needs to run.
+ cells_to_run.add(cid)
+ elif child.disabled_transitively:
+ # cell is no longer disabled: status -> idle
+ cells_idle.add(cid)
+ child.set_status("idle")
+ return cells_to_run, cells_idle
+
def delete_cell(self, cell_id: CellId_t) -> set[CellId_t]:
"""Removes a cell from the graph.
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index c2dae199dd..6eb85057b6 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -739,35 +739,16 @@ class Kernel:
cells_to_run: set[CellId_t] = set()
for cell_id, config in request.configs.items():
cell = self.graph.cells.get(cell_id)
- if cell is not None:
- cell.configure(config)
- if not cell.config.disabled:
- # When a cell is enabled, previously stale cells will
- # need to run
- for cid in dataflow.transitive_closure(
- self.graph, set([cell_id])
- ):
- if not self.graph.is_disabled(cid):
- child = self.graph.cells[cid]
- if child.stale:
- # cell was previously disabled, is no longer
- # disabled, and is stale: needs to run.
- cells_to_run.add(cid)
- elif child.disabled_transitively:
- write_idle(cid)
- # cell is no longer disabled: status -> idle
- child.set_status("idle")
- elif cell.config.disabled:
- # When a cell is disabled, we need to tell the frontend
- # that its children are transitively disabled
- for cid in dataflow.transitive_closure(
- self.graph, set([cell_id])
- ):
- if not self.graph.cells[cid].stale and cid != cell_id:
- self.graph.cells[cid].set_status(
- status="disabled-transitively"
- )
- write_disabled_transitively(cid)
+ if cell is None:
+ continue
+ cell.configure(config)
+ if not cell.config.disabled:
+ cells_to_run, cells_idle = self.graph.enable_cell(cell_id)
+ for cid in cells_idle:
+ write_idle(cid)
+ elif cell.config.disabled:
+ for cid in self.graph.disable_cell(cell_id):
+ write_disabled_transitively(cid)
# store the config, regardless of whether we've seen the cell yet
self.cell_metadata[cell_id] = CellMetadata(