Skip to content

Commit ef1ed30

Browse files
committed
remove expensive len() call
1 parent 46994d7 commit ef1ed30

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

bigframes/display/anywidget.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@
4545

4646

4747
class TableWidget(WIDGET_BASE):
48-
"""
49-
An interactive, paginated table widget for BigFrames DataFrames.
48+
"""An interactive, paginated table widget for BigFrames DataFrames.
49+
50+
This widget provides a user-friendly way to display and navigate through
51+
large BigQuery DataFrames within a Jupyter environment.
5052
"""
5153

5254
def __init__(self, dataframe: bigframes.dataframe.DataFrame):
@@ -75,15 +77,18 @@ def __init__(self, dataframe: bigframes.dataframe.DataFrame):
7577
# Initialize data fetching attributes.
7678
self._batches = dataframe.to_pandas_batches(page_size=initial_page_size)
7779

80+
# Access total_rows through type casting (internal use only)
81+
from bigframes.core.blocks import PandasBatches
82+
83+
if isinstance(self._batches, PandasBatches):
84+
self.row_count = self._batches.total_rows or 0
85+
else:
86+
# Fallback for compatibility
87+
self.row_count = 0
88+
7889
# set traitlets properties that trigger observers
7990
self.page_size = initial_page_size
8091

81-
# len(dataframe) is expensive, since it will trigger a
82-
# SELECT COUNT(*) query. It is a must have however.
83-
# TODO(b/428238610): Start iterating over the result of `to_pandas_batches()`
84-
# before we get here so that the count might already be cached.
85-
self.row_count = len(dataframe)
86-
8792
# get the initial page
8893
self._set_table_html()
8994

0 commit comments

Comments
 (0)