Skip to content

Commit 3045744

Browse files
committed
remove expensive len() call
1 parent e3f8ca7 commit 3045744

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
@@ -44,8 +44,10 @@
4444

4545

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

5153
def __init__(self, dataframe: bigframes.dataframe.DataFrame):
@@ -74,15 +76,18 @@ def __init__(self, dataframe: bigframes.dataframe.DataFrame):
7476
# Initialize data fetching attributes.
7577
self._batches = dataframe.to_pandas_batches(page_size=initial_page_size)
7678

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

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

0 commit comments

Comments
 (0)