Skip to content

Commit f8976c3

Browse files
committed
get row count directly
1 parent cf876b5 commit f8976c3

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

bigframes/display/anywidget.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from importlib import resources
1818
import functools
1919
import math
20-
from typing import Any, Dict, Iterator, List, Optional, Type
20+
from typing import Any, cast, Dict, Iterator, List, Optional, Type
2121
import uuid
2222

2323
import pandas as pd
@@ -52,6 +52,17 @@ class TableWidget(WIDGET_BASE):
5252
large BigQuery DataFrames within a Jupyter environment.
5353
"""
5454

55+
page_size = traitlets.Int(0).tag(sync=True)
56+
# Use dynamic default
57+
page_size = traitlets.Int().tag(sync=True)
58+
row_count = traitlets.Int(0).tag(sync=True)
59+
table_html = traitlets.Unicode().tag(sync=True)
60+
61+
@traitlets.default("page_size")
62+
def _page_size_default(self):
63+
"""Set the default page size from display options."""
64+
return bigframes.options.display.max_rows
65+
5566
def __init__(self, dataframe: bigframes.dataframe.DataFrame):
5667
"""Initialize the TableWidget.
5768
@@ -76,21 +87,15 @@ def __init__(self, dataframe: bigframes.dataframe.DataFrame):
7687
# Respect display options for initial page size
7788
initial_page_size = bigframes.options.display.max_rows
7889

79-
execute_result = dataframe._block.session._executor.execute(
80-
dataframe._block.expr,
81-
ordered=True,
82-
use_explicit_destination=True,
90+
self._batches: bigframes.core.blocks.PandasBatches = cast(
91+
bigframes.core.blocks.PandasBatches,
92+
dataframe.to_pandas_batches(page_size=initial_page_size),
8393
)
8494

8595
# The query issued by `to_pandas_batches()` already contains metadata
8696
# about how many results there were. Use that to avoid doing an extra
8797
# COUNT(*) query that `len(...)` would do.
88-
self.row_count = execute_result.total_rows or 0
89-
90-
# Create pandas batches from the ExecuteResult
91-
self._batches = execute_result.to_pandas_batches(page_size=initial_page_size)
92-
93-
self.page_size = initial_page_size
98+
self.row_count = self._batches.total_rows or 0
9499

95100
self._set_table_html()
96101
self._initializing = False
@@ -106,7 +111,7 @@ def _css(self):
106111
return resources.read_text(bigframes.display, "table_widget.css")
107112

108113
page = traitlets.Int(0).tag(sync=True)
109-
page_size = traitlets.Int(25).tag(sync=True)
114+
page_size = traitlets.Int().tag(sync=True)
110115
row_count = traitlets.Int(0).tag(sync=True)
111116
table_html = traitlets.Unicode().tag(sync=True)
112117

@@ -187,16 +192,10 @@ def _cached_data(self) -> pd.DataFrame:
187192

188193
def _reset_batches_for_new_page_size(self):
189194
"""Reset the batch iterator when page size changes."""
190-
# Execute with explicit destination for consistency with __init__
191-
execute_result = self._dataframe._block.session._executor.execute(
192-
self._dataframe._block.expr,
193-
ordered=True,
194-
use_explicit_destination=True,
195+
self._batches = cast(
196+
bigframes.core.blocks.PandasBatches,
197+
self._dataframe.to_pandas_batches(page_size=self.page_size),
195198
)
196-
197-
# Create pandas batches from the ExecuteResult
198-
self._batches = execute_result.to_pandas_batches(page_size=self.page_size)
199-
200199
self._cached_batches = []
201200
self._batch_iter = None
202201
self._all_data_loaded = False

notebooks/dataframes/anywidget_mode.ipynb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 1,
5+
"execution_count": null,
66
"id": "d10bfca4",
77
"metadata": {},
88
"outputs": [],
@@ -32,7 +32,7 @@
3232
},
3333
{
3434
"cell_type": "code",
35-
"execution_count": 1,
35+
"execution_count": null,
3636
"id": "ca22f059",
3737
"metadata": {},
3838
"outputs": [],
@@ -50,7 +50,7 @@
5050
},
5151
{
5252
"cell_type": "code",
53-
"execution_count": 2,
53+
"execution_count": null,
5454
"id": "1bc5aaf3",
5555
"metadata": {},
5656
"outputs": [],
@@ -69,7 +69,7 @@
6969
},
7070
{
7171
"cell_type": "code",
72-
"execution_count": 3,
72+
"execution_count": null,
7373
"id": "f289d250",
7474
"metadata": {},
7575
"outputs": [
@@ -96,7 +96,7 @@
9696
},
9797
{
9898
"cell_type": "code",
99-
"execution_count": 4,
99+
"execution_count": null,
100100
"id": "42bb02ab",
101101
"metadata": {},
102102
"outputs": [
@@ -123,7 +123,7 @@
123123
},
124124
{
125125
"cell_type": "code",
126-
"execution_count": 5,
126+
"execution_count": null,
127127
"id": "ce250157",
128128
"metadata": {},
129129
"outputs": [
@@ -179,7 +179,7 @@
179179
},
180180
{
181181
"cell_type": "code",
182-
"execution_count": 6,
182+
"execution_count": null,
183183
"id": "6920d49b",
184184
"metadata": {},
185185
"outputs": [
@@ -241,7 +241,7 @@
241241
},
242242
{
243243
"cell_type": "code",
244-
"execution_count": 7,
244+
"execution_count": null,
245245
"id": "12b68f15",
246246
"metadata": {},
247247
"outputs": [
@@ -278,7 +278,7 @@
278278
},
279279
{
280280
"cell_type": "code",
281-
"execution_count": 8,
281+
"execution_count": null,
282282
"id": "a9d5d13a",
283283
"metadata": {},
284284
"outputs": [

0 commit comments

Comments
 (0)