@@ -76,9 +76,6 @@ def __init__(self, dataframe: bigframes.dataframe.DataFrame):
7676 # Initialize data fetching attributes.
7777 self ._batches = dataframe ._to_pandas_batches (page_size = initial_page_size )
7878
79- # set traitlets properties that trigger observers
80- self .page_size = initial_page_size
81-
8279 # len(dataframe) is expensive, since it will trigger a
8380 # SELECT COUNT(*) query. It is a must have however.
8481 # TODO(b/428238610): Start iterating over the result of `to_pandas_batches()`
@@ -87,6 +84,10 @@ def __init__(self, dataframe: bigframes.dataframe.DataFrame):
8784 # there are multiple pages and show "page 1 of many" in this case.
8885 self .row_count = self ._batches .total_rows or 0
8986
87+ # set traitlets properties that trigger observers
88+ self .page_size = initial_page_size
89+ self ._reset_batches_for_new_page_size ()
90+
9091 # get the initial page
9192 self ._set_table_html ()
9293
@@ -190,19 +191,21 @@ def _reset_batches_for_new_page_size(self):
190191
191192 def _set_table_html (self ):
192193 """Sets the current html data based on the current page and page size."""
193- start = self .page * self .page_size
194- end = start + self .page_size
195-
196- # fetch more data if the requested page is outside our cache
197- cached_data = self ._cached_data
198- while len (cached_data ) < end and not self ._all_data_loaded :
199- if self ._get_next_batch ():
200- cached_data = self ._cached_data
201- else :
202- break
203-
204- # Get the data for the current page
205- page_data = cached_data .iloc [start :end ]
194+ # For empty dataframe, render empty table with headers.
195+ if self .row_count == 0 :
196+ page_data = self ._cached_data
197+ else :
198+ start = self .page * self .page_size
199+ end = start + self .page_size
200+
201+ # fetch more data if the requested page is outside our cache
202+ cached_data = self ._cached_data
203+ while len (cached_data ) < end and not self ._all_data_loaded :
204+ if self ._get_next_batch ():
205+ cached_data = self ._cached_data
206+ else :
207+ break
208+ page_data = cached_data .iloc [start :end ]
206209
207210 # Generate HTML table
208211 self .table_html = bigframes .display .html .render_html (
@@ -221,8 +224,5 @@ def _page_size_changed(self, _change: Dict[str, Any]):
221224 # Reset the page to 0 when page size changes to avoid invalid page states
222225 self .page = 0
223226
224- # Reset batches to use new page size for future data fetching
225- self ._reset_batches_for_new_page_size ()
226-
227227 # Update the table display
228228 self ._set_table_html ()
0 commit comments