Skip to content

Commit 8e31c6c

Browse files
committed
code style change
1 parent 17a1956 commit 8e31c6c

File tree

3 files changed

+158
-52
lines changed

3 files changed

+158
-52
lines changed

bigframes/core/compile/ibis_compiler/scalar_op_registry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from bigframes.core.compile.constants import UNIT_TO_US_CONVERSION_FACTORS
3131
import bigframes.core.compile.ibis_compiler.default_ordering
3232
from bigframes.core.compile.ibis_compiler.scalar_op_compiler import (
33-
scalar_op_compiler, # TODO(tswast): avoid import of variables
33+
scalar_op_compiler, # TODO(b/428238610): avoid import of variables
3434
)
3535
import bigframes.core.compile.ibis_types
3636
import bigframes.operations as ops

bigframes/dataframe.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ def __repr__(self) -> str:
784784
opts = bigframes.options.display
785785
max_results = opts.max_rows
786786

787-
# anywdiget mode uses the same display logic as the "deferred" mode
787+
# anywidget mode uses the same display logic as the "deferred" mode
788788
# for faster execution
789789
if opts.repr_mode in ("deferred", "anywidget"):
790790
return formatter.repr_query_job(self._compute_dry_run())
@@ -857,6 +857,29 @@ def _repr_html_(self) -> str:
857857
import traitlets # noqa: F401
858858

859859
from bigframes import display
860+
861+
# The anywidget frontend doesn't support the db_dtypes JSON type, so
862+
# convert to strings for display.
863+
json_cols = [
864+
series_name
865+
for series_name, series in df.items()
866+
if bigframes.dtypes.contains_db_dtypes_json_dtype(series.dtype)
867+
]
868+
if json_cols:
869+
warnings.warn(
870+
"Converting JSON columns to strings for display. "
871+
"This is temporary and will be removed when the frontend supports JSON types."
872+
)
873+
for col in json_cols:
874+
df[col] = df[col]._apply_unary_op(ops.json_ops.ToJSONString())
875+
876+
# Always create a new widget instance for each display call
877+
# This ensures that each cell gets its own widget and prevents
878+
# unintended sharing between cells
879+
widget = display.TableWidget(df.copy())
880+
881+
ipython_display(widget)
882+
return "" # Return empty string since we used display()
860883
except (AttributeError, ValueError, ImportError):
861884
# Fallback if anywidget is not available
862885
warnings.warn(
@@ -866,29 +889,6 @@ def _repr_html_(self) -> str:
866889
)
867890
return formatter.repr_query_job(self._compute_dry_run())
868891

869-
# The anywidget frontend doesn't support the db_dtypes JSON type, so
870-
# convert to strings for display.
871-
json_cols = [
872-
series_name
873-
for series_name, series in df.items()
874-
if bigframes.dtypes.contains_db_dtypes_json_dtype(series.dtype)
875-
]
876-
if json_cols:
877-
warnings.warn(
878-
"Converting JSON columns to strings for display. "
879-
"This is temporary and will be removed when the frontend supports JSON types."
880-
)
881-
for col in json_cols:
882-
df[col] = df[col]._apply_unary_op(ops.json_ops.ToJSONString())
883-
884-
# Always create a new widget instance for each display call
885-
# This ensures that each cell gets its own widget and prevents
886-
# unintended sharing between cells
887-
widget = display.TableWidget(df.copy())
888-
889-
ipython_display(widget)
890-
return "" # Return empty string since we used display()
891-
892892
# Continue with regular HTML rendering for non-anywidget modes
893893
# TODO(swast): pass max_columns and get the true column count back. Maybe
894894
# get 1 more column than we have requested so that pandas can add the

notebooks/dataframes/anywidget_mode.ipynb

Lines changed: 133 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,11 @@
7373
"id": "f289d250",
7474
"metadata": {},
7575
"outputs": [
76-
{
77-
"data": {
78-
"application/vnd.jupyter.widget-view+json": {
79-
"model_id": "071c0a905297406ba6c990cbbb8fc28d",
80-
"version_major": 2,
81-
"version_minor": 0
82-
},
83-
"text/plain": [
84-
"TableWidget(page_size=10, row_count=5552452, table_html='<table border=\"1\" class=\"dataframe table table-stripe…"
85-
]
86-
},
87-
"metadata": {},
88-
"output_type": "display_data"
89-
},
9076
{
9177
"name": "stdout",
9278
"output_type": "stream",
9379
"text": [
94-
"\n"
80+
"Computation deferred. Computation will process 171.4 MB\n"
9581
]
9682
}
9783
],
@@ -143,13 +129,11 @@
143129
"outputs": [
144130
{
145131
"data": {
146-
"application/vnd.jupyter.widget-view+json": {
147-
"model_id": "042a3d55c51b4a3192cf1a942c6797e8",
148-
"version_major": 2,
149-
"version_minor": 0
150-
},
132+
"text/html": [
133+
"✅ Completed. "
134+
],
151135
"text/plain": [
152-
"TableWidget(page_size=10, row_count=5552452, table_html='<table border=\"1\" class=\"dataframe table table-stripe…"
136+
"<IPython.core.display.HTML object>"
153137
]
154138
},
155139
"metadata": {},
@@ -158,7 +142,7 @@
158142
{
159143
"data": {
160144
"application/vnd.jupyter.widget-view+json": {
161-
"model_id": "06e46087ecbe4e6d8bdc2ed7c9284a7d",
145+
"model_id": "25b38c1408434091865f4bf9525dd069",
162146
"version_major": 2,
163147
"version_minor": 0
164148
},
@@ -172,7 +156,9 @@
172156
{
173157
"data": {
174158
"text/html": [],
175-
"text/plain": []
159+
"text/plain": [
160+
"Computation deferred. Computation will process 171.4 MB"
161+
]
176162
},
177163
"execution_count": 6,
178164
"metadata": {},
@@ -197,6 +183,18 @@
197183
"id": "6920d49b",
198184
"metadata": {},
199185
"outputs": [
186+
{
187+
"data": {
188+
"text/html": [
189+
"✅ Completed. "
190+
],
191+
"text/plain": [
192+
"<IPython.core.display.HTML object>"
193+
]
194+
},
195+
"metadata": {},
196+
"output_type": "display_data"
197+
},
200198
{
201199
"name": "stdout",
202200
"output_type": "stream",
@@ -207,7 +205,7 @@
207205
{
208206
"data": {
209207
"application/vnd.jupyter.widget-view+json": {
210-
"model_id": "4da2aaa13f074229b022c7256ba7510b",
208+
"model_id": "cb4f246802a1407cb966321d8724ea27",
211209
"version_major": 2,
212210
"version_minor": 0
213211
},
@@ -282,6 +280,20 @@
282280
"id": "a9d5d13a",
283281
"metadata": {},
284282
"outputs": [
283+
{
284+
"data": {
285+
"text/html": [
286+
"✅ Completed. \n",
287+
" Query processed 171.4 MB in a moment of slot time.\n",
288+
" "
289+
],
290+
"text/plain": [
291+
"<IPython.core.display.HTML object>"
292+
]
293+
},
294+
"metadata": {},
295+
"output_type": "display_data"
296+
},
285297
{
286298
"name": "stdout",
287299
"output_type": "stream",
@@ -292,7 +304,7 @@
292304
{
293305
"data": {
294306
"application/vnd.jupyter.widget-view+json": {
295-
"model_id": "d2739b180f7d40b1b52ebab121ab7a8a",
307+
"model_id": "5b48c05254224c4dbce56f2793d6b661",
296308
"version_major": 2,
297309
"version_minor": 0
298310
},
@@ -311,11 +323,105 @@
311323
"print(f\"Small dataset pages: {math.ceil(small_widget.row_count / small_widget.page_size)}\")\n",
312324
"small_widget"
313325
]
326+
},
327+
{
328+
"cell_type": "markdown",
329+
"id": "added-cell-2",
330+
"metadata": {},
331+
"source": [
332+
"### Displaying Generative AI results containing JSON\n",
333+
"The `AI.GENERATE` function in BigQuery returns results in a JSON column. While BigQuery's JSON type is not natively supported by the underlying Arrow `to_pandas_batches()` method used in anywidget mode ([Apache Arrow issue #45262](https://github.com/apache/arrow/issues/45262)), BigQuery Dataframes automatically converts JSON columns to strings for display. This allows you to view the results of generative AI functions seamlessly."
334+
]
335+
},
336+
{
337+
"cell_type": "code",
338+
"execution_count": 10,
339+
"id": "added-cell-1",
340+
"metadata": {},
341+
"outputs": [
342+
{
343+
"data": {
344+
"text/html": [
345+
"✅ Completed. \n",
346+
" Query processed 85.9 kB in 14 seconds of slot time.\n",
347+
" "
348+
],
349+
"text/plain": [
350+
"<IPython.core.display.HTML object>"
351+
]
352+
},
353+
"metadata": {},
354+
"output_type": "display_data"
355+
},
356+
{
357+
"name": "stderr",
358+
"output_type": "stream",
359+
"text": [
360+
"/usr/local/google/home/shuowei/src/github.com/googleapis/python-bigquery-dataframes/bigframes/dtypes.py:959: JSONDtypeWarning: JSON columns will be represented as pandas.ArrowDtype(pyarrow.json_())\n",
361+
"instead of using `db_dtypes` in the future when available in pandas\n",
362+
"(https://github.com/pandas-dev/pandas/issues/60958) and pyarrow.\n",
363+
" warnings.warn(msg, bigframes.exceptions.JSONDtypeWarning)\n",
364+
"/usr/local/google/home/shuowei/src/github.com/googleapis/python-bigquery-dataframes/bigframes/dataframe.py:869: UserWarning: Converting JSON columns to strings for display. This is temporary and will be removed when the frontend supports JSON types.\n",
365+
" warnings.warn(\n"
366+
]
367+
},
368+
{
369+
"data": {
370+
"text/html": [
371+
"✅ Completed. \n",
372+
" Query processed 0 Bytes in a moment of slot time.\n",
373+
" "
374+
],
375+
"text/plain": [
376+
"<IPython.core.display.HTML object>"
377+
]
378+
},
379+
"metadata": {},
380+
"output_type": "display_data"
381+
},
382+
{
383+
"data": {
384+
"application/vnd.jupyter.widget-view+json": {
385+
"model_id": "ab607cc7263f4a159ecfe63682c5e651",
386+
"version_major": 2,
387+
"version_minor": 0
388+
},
389+
"text/plain": [
390+
"TableWidget(page_size=10, row_count=5, table_html='<table border=\"1\" class=\"dataframe table table-striped tabl…"
391+
]
392+
},
393+
"metadata": {},
394+
"output_type": "display_data"
395+
},
396+
{
397+
"data": {
398+
"text/html": [],
399+
"text/plain": [
400+
"Computation deferred. Computation will process 0 Bytes"
401+
]
402+
},
403+
"execution_count": 10,
404+
"metadata": {},
405+
"output_type": "execute_result"
406+
}
407+
],
408+
"source": [
409+
"bpd._read_gbq_colab(\"\"\"\\n\n",
410+
"SELECT\\n\n",
411+
" AI.GENERATE(\\n\n",
412+
" prompt=>(\\\"Extract the values.\\\", OBJ.GET_ACCESS_URL(OBJ.FETCH_METADATA(OBJ.MAKE_REF(gcs_path, \\\"us.conn\\\")), \\\"r\\\")),\\n\n",
413+
" connection_id=>\\\"bigframes-dev.us.bigframes-default-connection\\\",\\n\n",
414+
" output_schema=>\\\"publication_date string, class_international string, application_number string, filing_date string\\\") AS result,\\n\n",
415+
" *\\n\n",
416+
"FROM `bigquery-public-data.labeled_patents.extracted_data`\\n\n",
417+
"LIMIT 5;\\n\n",
418+
"\"\"\")"
419+
]
314420
}
315421
],
316422
"metadata": {
317423
"kernelspec": {
318-
"display_name": "venv",
424+
"display_name": "3.10.18",
319425
"language": "python",
320426
"name": "python3"
321427
},
@@ -329,7 +435,7 @@
329435
"name": "python",
330436
"nbconvert_exporter": "python",
331437
"pygments_lexer": "ipython3",
332-
"version": "3.10.16"
438+
"version": "3.10.18"
333439
}
334440
},
335441
"nbformat": 4,

0 commit comments

Comments
 (0)