Skip to content

Commit b9c9336

Browse files
committed
feat: propagate cell execution count to widget queries and rename history filter
1 parent 66fe150 commit b9c9336

7 files changed

Lines changed: 51 additions & 7 deletions

File tree

packages/bigframes/bigframes/core/blocks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@ def to_pandas_batches(
696696
page_size: Optional[int] = None,
697697
max_results: Optional[int] = None,
698698
allow_large_results: Optional[bool] = None,
699+
cell_execution_count: Optional[int] = None,
699700
) -> PandasBatches:
700701
"""Download results one message at a time.
701702
@@ -713,6 +714,7 @@ def to_pandas_batches(
713714
execution_spec.ExecutionSpec(
714715
promise_under_10gb=under_10gb,
715716
ordered=True,
717+
cell_execution_count=cell_execution_count,
716718
),
717719
)
718720
result_batches = execution_result.batches()

packages/bigframes/bigframes/core/global_session.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def execution_history(
128128
*,
129129
events: Optional[Iterable[bigframes.core.events.Event]] = None,
130130
job_ids: Optional[Iterable[str]] = None,
131-
filter_by_cell: bool = True,
131+
current_cell_only: bool = True,
132132
) -> "bigframes.session._ExecutionHistory":
133133
import pandas # noqa: F401
134134

@@ -138,7 +138,7 @@ def execution_history(
138138
bigframes.session.Session.execution_history,
139139
events=events,
140140
job_ids=job_ids,
141-
filter_by_cell=filter_by_cell,
141+
current_cell_only=current_cell_only,
142142
)
143143

144144

packages/bigframes/bigframes/dataframe.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,7 @@ def to_pandas_batches(
17391739
max_results: Optional[int] = None,
17401740
*,
17411741
allow_large_results: Optional[bool] = None,
1742+
cell_execution_count: Optional[int] = None,
17421743
) -> blocks.PandasBatches:
17431744
"""Stream DataFrame results to an iterable of pandas DataFrame.
17441745
@@ -1791,6 +1792,7 @@ def to_pandas_batches(
17911792
page_size=page_size,
17921793
max_results=max_results,
17931794
allow_large_results=allow_large_results,
1795+
cell_execution_count=cell_execution_count,
17941796
)
17951797

17961798
def _to_pandas_batches(
@@ -1799,11 +1801,13 @@ def _to_pandas_batches(
17991801
max_results: Optional[int] = None,
18001802
*,
18011803
allow_large_results: Optional[bool] = None,
1804+
cell_execution_count: Optional[int] = None,
18021805
) -> blocks.PandasBatches:
18031806
return self._block.to_pandas_batches(
18041807
page_size=page_size,
18051808
max_results=max_results,
18061809
allow_large_results=allow_large_results,
1810+
cell_execution_count=cell_execution_count,
18071811
)
18081812

18091813
def _compute_dry_run(self) -> google.cloud.bigquery.job.QueryJob:

packages/bigframes/bigframes/display/anywidget.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ def __init__(self, dataframe: bigframes.dataframe.DataFrame):
9292

9393
self._dataframe = dataframe
9494

95+
self._cell_execution_count = None
96+
try:
97+
import IPython
98+
99+
ipy = IPython.get_ipython()
100+
if ipy is not None and hasattr(ipy, "execution_count"):
101+
self._cell_execution_count = ipy.execution_count
102+
except (ImportError, NameError):
103+
pass
104+
95105
super().__init__()
96106

97107
# Initialize attributes that might be needed by observers first
@@ -286,7 +296,10 @@ def _reset_batch_cache(self) -> None:
286296
def _reset_batches_for_new_page_size(self) -> None:
287297
"""Reset the batch iterator when page size changes."""
288298
with bigframes.option_context("display.progress_bar", None):
289-
self._batches = self._dataframe.to_pandas_batches(page_size=self.page_size)
299+
self._batches = self._dataframe.to_pandas_batches(
300+
page_size=self.page_size,
301+
cell_execution_count=self._cell_execution_count,
302+
)
290303

291304
self._reset_batch_cache()
292305

@@ -318,7 +331,8 @@ def _set_table_html(self) -> None:
318331
current_sort_state = _SortState(tuple(sort_columns), tuple(sort_ascending))
319332
if self._last_sort_state != current_sort_state:
320333
self._batches = df_to_display.to_pandas_batches(
321-
page_size=self.page_size
334+
page_size=self.page_size,
335+
cell_execution_count=self._cell_execution_count,
322336
)
323337
self._reset_batch_cache()
324338
self._last_sort_state = current_sort_state

packages/bigframes/bigframes/series.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,7 @@ def to_pandas_batches(
756756
max_results: Optional[int] = None,
757757
*,
758758
allow_large_results: Optional[bool] = None,
759+
cell_execution_count: Optional[int] = None,
759760
) -> Iterable[pandas.Series]:
760761
"""Stream Series results to an iterable of pandas Series.
761762
@@ -808,6 +809,7 @@ def to_pandas_batches(
808809
page_size=page_size,
809810
max_results=max_results,
810811
allow_large_results=allow_large_results,
812+
cell_execution_count=cell_execution_count,
811813
)
812814
return map(lambda df: cast(pandas.Series, df.squeeze(1)), batches)
813815

packages/bigframes/bigframes/session/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ def execution_history(
448448
*,
449449
events: Optional[Iterable[bigframes.core.events.Event]] = None,
450450
job_ids: Optional[Iterable[str]] = None,
451-
filter_by_cell: bool = True,
451+
current_cell_only: bool = True,
452452
) -> _ExecutionHistory:
453453
"""Returns the history of executions initiated by BigFrames in the current session.
454454
@@ -459,7 +459,7 @@ def execution_history(
459459
Filter execution history to only include jobs associated with the given events.
460460
job_ids (Iterable[str], optional):
461461
Filter execution history to only include jobs matching the given job IDs.
462-
filter_by_cell (bool, optional):
462+
current_cell_only (bool, optional):
463463
If True and running in Colab/Jupyter, automatically filter history to only include
464464
jobs executed within the current cell. Defaults to True.
465465
"""
@@ -503,7 +503,7 @@ def execution_history(
503503
)
504504
]
505505

506-
elif filter_by_cell:
506+
elif current_cell_only:
507507
try:
508508
import IPython
509509

packages/bigframes/tests/unit/display/test_anywidget.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,25 @@ def test_page_size_change_resets_sort(mock_df):
179179

180180
# to_pandas_batches called again (reset)
181181
assert mock_df.to_pandas_batches.call_count >= 2
182+
183+
184+
def test_cell_execution_count_propagation(mock_df):
185+
"""Test that the captured cell_execution_count is propagated to to_pandas_batches."""
186+
from bigframes.display.anywidget import TableWidget
187+
188+
# Mock IPython to return a specific execution count
189+
mock_ipy = mock.Mock()
190+
mock_ipy.execution_count = 42
191+
192+
with mock.patch("IPython.get_ipython", return_value=mock_ipy):
193+
with bigframes.option_context("display.render_mode", "anywidget"):
194+
widget = TableWidget(mock_df)
195+
196+
# Verify captured execution count
197+
assert widget._cell_execution_count == 42
198+
199+
# to_pandas_batches should be called with cell_execution_count=42
200+
mock_df.to_pandas_batches.assert_called_with(
201+
page_size=widget.page_size,
202+
cell_execution_count=42,
203+
)

0 commit comments

Comments
 (0)