Skip to content

Commit 0caaa52

Browse files
committed
remove double query when display widget
1 parent b442fce commit 0caaa52

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

bigframes/display/anywidget.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ def __init__(self, dataframe: bigframes.dataframe.DataFrame):
6363
"Please `pip install anywidget traitlets` or `pip install 'bigframes[anywidget]'` to use TableWidget."
6464
)
6565

66-
super().__init__()
6766
self._dataframe = dataframe
67+
self._initializing = True
68+
super().__init__()
6869

6970
# Initialize attributes that might be needed by observers first
7071
self._table_id = str(uuid.uuid4())
@@ -89,12 +90,10 @@ def __init__(self, dataframe: bigframes.dataframe.DataFrame):
8990
# Create pandas batches from the ExecuteResult
9091
self._batches = execute_result.to_pandas_batches(page_size=initial_page_size)
9192

92-
# Set page_size after _batches is available, but avoid triggering observers
93-
# by setting the underlying traitlet value directly
94-
self._trait_values["page_size"] = initial_page_size
95-
self._trait_notifiers["page_size"] = {} # Initialize notifiers if needed
93+
self.page_size = initial_page_size
9694

9795
self._set_table_html()
96+
self._initializing = False
9897

9998
@functools.cached_property
10099
def _esm(self):
@@ -227,11 +226,15 @@ def _set_table_html(self):
227226
@traitlets.observe("page")
228227
def _page_changed(self, _change: Dict[str, Any]):
229228
"""Handler for when the page number is changed from the frontend."""
229+
if self._initializing:
230+
return
230231
self._set_table_html()
231232

232233
@traitlets.observe("page_size")
233234
def _page_size_changed(self, _change: Dict[str, Any]):
234235
"""Handler for when the page size is changed from the frontend."""
236+
if self._initializing:
237+
return
235238
# Reset the page to 0 when page size changes to avoid invalid page states
236239
self.page = 0
237240

notebooks/dataframes/anywidget_mode.ipynb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
{
131131
"data": {
132132
"text/html": [
133-
"Query job 087c4276-8c26-467f-852b-c0d31848f666 is DONE. 171.4 MB processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:087c4276-8c26-467f-852b-c0d31848f666&page=queryresults\">Open Job</a>"
133+
"Query job 1171b7b3-3f65-4165-a69d-69dad5a100d1 is DONE. 171.4 MB processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:1171b7b3-3f65-4165-a69d-69dad5a100d1&page=queryresults\">Open Job</a>"
134134
],
135135
"text/plain": [
136136
"<IPython.core.display.HTML object>"
@@ -142,7 +142,7 @@
142142
{
143143
"data": {
144144
"application/vnd.jupyter.widget-view+json": {
145-
"model_id": "c2a4111b39c3462a8d0f4f2e4a01635b",
145+
"model_id": "6b70bf0e30a04a3cab11e03b2ed80856",
146146
"version_major": 2,
147147
"version_minor": 1
148148
},
@@ -186,7 +186,7 @@
186186
{
187187
"data": {
188188
"text/html": [
189-
"Query job 86d748cf-699c-407c-8eba-2d6421375aad is DONE. 171.4 MB processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:86d748cf-699c-407c-8eba-2d6421375aad&page=queryresults\">Open Job</a>"
189+
"Query job 3100859b-c57c-42fe-a5fb-abb4f2f25db2 is DONE. 171.4 MB processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:3100859b-c57c-42fe-a5fb-abb4f2f25db2&page=queryresults\">Open Job</a>"
190190
],
191191
"text/plain": [
192192
"<IPython.core.display.HTML object>"
@@ -205,7 +205,7 @@
205205
{
206206
"data": {
207207
"application/vnd.jupyter.widget-view+json": {
208-
"model_id": "401985bd2b3f40f3a2f7e48eeabb272d",
208+
"model_id": "4714b0794f55435a8d3e136517158a5c",
209209
"version_major": 2,
210210
"version_minor": 1
211211
},
@@ -292,7 +292,7 @@
292292
{
293293
"data": {
294294
"text/html": [
295-
"Query job 2cb31c3a-ccbc-40fc-b548-ce8503fd2cc3 is DONE. 171.4 MB processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:2cb31c3a-ccbc-40fc-b548-ce8503fd2cc3&page=queryresults\">Open Job</a>"
295+
"Query job b4143f15-4bac-44a5-bb29-c5056f95b30b is DONE. 171.4 MB processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:b4143f15-4bac-44a5-bb29-c5056f95b30b&page=queryresults\">Open Job</a>"
296296
],
297297
"text/plain": [
298298
"<IPython.core.display.HTML object>"
@@ -311,7 +311,7 @@
311311
{
312312
"data": {
313313
"application/vnd.jupyter.widget-view+json": {
314-
"model_id": "9d2e3ced089a4cadbec9eb06d3724237",
314+
"model_id": "c70b5611db6b4e6a806a16d0a8287cd3",
315315
"version_major": 2,
316316
"version_minor": 1
317317
},
@@ -335,7 +335,7 @@
335335
],
336336
"metadata": {
337337
"kernelspec": {
338-
"display_name": "Python 3 (ipykernel)",
338+
"display_name": "venv",
339339
"language": "python",
340340
"name": "python3"
341341
},
@@ -349,7 +349,7 @@
349349
"name": "python",
350350
"nbconvert_exporter": "python",
351351
"pygments_lexer": "ipython3",
352-
"version": "3.10.16"
352+
"version": "3.10.15"
353353
}
354354
},
355355
"nbformat": 4,

0 commit comments

Comments
 (0)