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