17
17
from importlib import resources
18
18
import functools
19
19
import math
20
- from typing import Any , Dict , Iterator , List , Optional , Type
20
+ import typing
21
+ from typing import Any , cast , Dict , Iterator , List , Optional , Type
21
22
import uuid
22
23
23
24
import pandas as pd
24
25
25
26
import bigframes
27
+ import bigframes .core .blocks
26
28
import bigframes .display .html
27
29
28
30
# anywidget and traitlets are optional dependencies. We don't want the import of this
@@ -69,21 +71,24 @@ def __init__(self, dataframe: bigframes.dataframe.DataFrame):
69
71
self ._table_id = str (uuid .uuid4 ())
70
72
self ._all_data_loaded = False
71
73
self ._batch_iter : Optional [Iterator [pd .DataFrame ]] = None
74
+ self ._batches : Optional [bigframes .core .blocks .PandasBatches ] = None
72
75
self ._cached_batches : List [pd .DataFrame ] = []
73
76
74
77
# Respect display options for initial page size
75
78
initial_page_size = bigframes .options .display .max_rows
76
79
77
80
try :
78
81
# Fetches initial data batches and row count for display.
79
- # `to_pandas_batches` provides an iterable of pandas DataFrames
80
- # and eagerly retrieves the total row count
81
- self ._batches = dataframe .to_pandas_batches (
82
+ batches = dataframe .to_pandas_batches (
82
83
page_size = initial_page_size ,
83
84
)
85
+ self ._batches = cast (bigframes .core .blocks .PandasBatches , batches )
84
86
85
- # Access the total_rows property directly
86
- self .row_count = self ._batches .total_rows or 0
87
+ # Use total_rows if available, otherwise default to 0.
88
+ if self ._batches :
89
+ self .row_count = self ._batches .total_rows or 0
90
+ else :
91
+ self .row_count = 0
87
92
self .page_size = initial_page_size
88
93
89
94
# Generates the initial HTML table content
@@ -92,7 +97,7 @@ def __init__(self, dataframe: bigframes.dataframe.DataFrame):
92
97
except Exception :
93
98
self .row_count = 0
94
99
self .page_size = initial_page_size
95
- self ._batches = iter ([])
100
+ self ._batches = None
96
101
self .table_html = ""
97
102
98
103
@functools .cached_property
@@ -176,7 +181,10 @@ def _get_next_batch(self) -> bool:
176
181
def _batch_iterator (self ) -> Iterator [pd .DataFrame ]:
177
182
"""Lazily initializes and returns the batch iterator."""
178
183
if self ._batch_iter is None :
179
- self ._batch_iter = iter (self ._batches )
184
+ if self ._batches is None :
185
+ self ._batch_iter = iter ([])
186
+ else :
187
+ self ._batch_iter = iter (self ._batches )
180
188
return self ._batch_iter
181
189
182
190
@property
@@ -188,7 +196,8 @@ def _cached_data(self) -> pd.DataFrame:
188
196
189
197
def _reset_batches_for_new_page_size (self ):
190
198
"""Reset the batch iterator when page size changes."""
191
- self ._batches = self ._dataframe .to_pandas_batches (page_size = self .page_size )
199
+ batches = self ._dataframe .to_pandas_batches (page_size = self .page_size )
200
+ self ._batches = typing .cast (bigframes .core .blocks .PandasBatches , batches )
192
201
self ._cached_batches = []
193
202
self ._batch_iter = None
194
203
self ._all_data_loaded = False
0 commit comments