Skip to content

Commit 41ec0f0

Browse files
committed
notebook and testcase updates
1 parent aded381 commit 41ec0f0

File tree

4 files changed

+193
-99
lines changed

4 files changed

+193
-99
lines changed

bigframes/display/anywidget.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def _set_table_html(self) -> None:
227227
break
228228

229229
# Get the data for the current page
230-
page_data = cached_data[start:end]
230+
page_data = cached_data.iloc[start:end]
231231

232232
# Generate HTML table
233233
self.table_html = bigframes.display.html.render_html(

bigframes/display/html.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@ def render_html(
4646
# Add index headers
4747
for name in dataframe.index.names:
4848
table_html.append(
49-
f' <th style="text-align: left;"><div style="resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;">{html.escape(str(name))}</div></th>'
49+
f' <th style="text-align: left;">'
50+
f'<div style="resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;">'
51+
f"{html.escape(str(name))}</div></th>"
5052
)
5153

5254
for col in dataframe.columns:
5355
table_html.append(
54-
f' <th style="text-align: left;"><div style="resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;">{html.escape(str(col))}</div></th>'
56+
f' <th style="text-align: left;">'
57+
f'<div style="resize: horizontal; overflow: auto; box-sizing: border-box; width: 100%; height: 100%; padding: 0.5em;">'
58+
f"{html.escape(str(col))}</div></th>"
5559
)
5660
table_html.append(" </tr>")
5761
table_html.append(" </thead>")

notebooks/dataframes/anywidget_mode.ipynb

Lines changed: 107 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,7 @@
3535
"execution_count": 2,
3636
"id": "ca22f059",
3737
"metadata": {},
38-
"outputs": [
39-
{
40-
"name": "stderr",
41-
"output_type": "stream",
42-
"text": [
43-
"/usr/local/google/home/shuowei/src/python-bigquery-dataframes/venv/lib/python3.10/site-packages/google/api_core/_python_version_support.py:266: FutureWarning: You are using a Python version (3.10.15) which Google will stop supporting in new releases of google.api_core once it reaches its end of life (2026-10-04). Please upgrade to the latest Python version, or at least Python 3.11, to continue receiving updates for google.api_core past that date.\n",
44-
" warnings.warn(message, FutureWarning)\n"
45-
]
46-
}
47-
],
38+
"outputs": [],
4839
"source": [
4940
"import bigframes.pandas as bpd"
5041
]
@@ -151,7 +142,7 @@
151142
{
152143
"data": {
153144
"application/vnd.jupyter.widget-view+json": {
154-
"model_id": "47795eaa10f149aeb99574232c0936eb",
145+
"model_id": "e2e84120b65f4a6d8bff7c7bfbf813e8",
155146
"version_major": 2,
156147
"version_minor": 1
157148
},
@@ -214,7 +205,7 @@
214205
{
215206
"data": {
216207
"application/vnd.jupyter.widget-view+json": {
217-
"model_id": "8354ce0f82d3495a9b630dfc362f73ee",
208+
"model_id": "fc58c5471b8f444b85e35dd5b93a17ca",
218209
"version_major": 2,
219210
"version_minor": 1
220211
},
@@ -293,27 +284,8 @@
293284
{
294285
"data": {
295286
"text/html": [
296-
"\n",
297-
" Query started with request ID bigframes-dev:US.c45952fb-01b4-409c-9da4-f7c5bfc0d47d.<details><summary>SQL</summary><pre>SELECT\n",
298-
"`state` AS `state`,\n",
299-
"`gender` AS `gender`,\n",
300-
"`year` AS `year`,\n",
301-
"`name` AS `name`,\n",
302-
"`number` AS `number`\n",
303-
"FROM\n",
304-
"(SELECT\n",
305-
" *\n",
306-
"FROM (\n",
307-
" SELECT\n",
308-
" `state`,\n",
309-
" `gender`,\n",
310-
" `year`,\n",
311-
" `name`,\n",
312-
" `number`\n",
313-
" FROM `bigquery-public-data.usa_names.usa_1910_2013` FOR SYSTEM_TIME AS OF TIMESTAMP(&#x27;2025-10-30T21:48:48.979701+00:00&#x27;)\n",
314-
") AS `t0`)\n",
315-
"ORDER BY `name` ASC NULLS LAST ,`year` ASC NULLS LAST ,`state` ASC NULLS LAST\n",
316-
"LIMIT 5</pre></details>\n",
287+
"✅ Completed. \n",
288+
" Query processed 171.4 MB in 2 seconds of slot time.\n",
317289
" "
318290
],
319291
"text/plain": [
@@ -333,7 +305,7 @@
333305
{
334306
"data": {
335307
"application/vnd.jupyter.widget-view+json": {
336-
"model_id": "59461286a17d4a42b6be6d9d9c7bf7e3",
308+
"model_id": "16ef1baae8934457a4d9e032262cac54",
337309
"version_major": 2,
338310
"version_minor": 1
339311
},
@@ -373,7 +345,7 @@
373345
"data": {
374346
"text/html": [
375347
"✅ Completed. \n",
376-
" Query processed 85.9 kB in 14 seconds of slot time.\n",
348+
" Query processed 85.9 kB in 16 seconds of slot time.\n",
377349
" "
378350
],
379351
"text/plain": [
@@ -387,7 +359,7 @@
387359
"name": "stderr",
388360
"output_type": "stream",
389361
"text": [
390-
"/usr/local/google/home/shuowei/src/python-bigquery-dataframes/bigframes/dtypes.py:969: JSONDtypeWarning: JSON columns will be represented as pandas.ArrowDtype(pyarrow.json_())\n",
362+
"/usr/local/google/home/shuowei/src/python-bigquery-dataframes/bigframes/dtypes.py:987: JSONDtypeWarning: JSON columns will be represented as pandas.ArrowDtype(pyarrow.json_())\n",
391363
"instead of using `db_dtypes` in the future when available in pandas\n",
392364
"(https://github.com/pandas-dev/pandas/issues/60958) and pyarrow.\n",
393365
" warnings.warn(msg, bigframes.exceptions.JSONDtypeWarning)\n"
@@ -408,7 +380,7 @@
408380
{
409381
"data": {
410382
"application/vnd.jupyter.widget-view+json": {
411-
"model_id": "d1794b42579542a8980bd158e521bd3e",
383+
"model_id": "42d656c30fc34855a8fa5e398de412eb",
412384
"version_major": 2,
413385
"version_minor": 1
414386
},
@@ -443,6 +415,104 @@
443415
" LIMIT 5;\n",
444416
"\"\"\")"
445417
]
418+
},
419+
{
420+
"cell_type": "markdown",
421+
"id": "multi-index-display-markdown",
422+
"metadata": {},
423+
"source": [
424+
"## Display Multi-Index DataFrame in anywidget mode\n",
425+
"This section demonstrates how BigFrames can display a DataFrame with multiple levels of indexing (a \"multi-index\") when using the `anywidget` display mode."
426+
]
427+
},
428+
{
429+
"cell_type": "code",
430+
"execution_count": 11,
431+
"id": "ad7482aa",
432+
"metadata": {},
433+
"outputs": [
434+
{
435+
"name": "stderr",
436+
"output_type": "stream",
437+
"text": [
438+
"/usr/local/google/home/shuowei/src/python-bigquery-dataframes/bigframes/session/_io/bigquery/read_gbq_table.py:483: DefaultIndexWarning: Table 'bigquery-public-data.pypi.file_downloads' is clustered and/or\n",
439+
"partitioned, but BigQuery DataFrames was not able to find a suitable\n",
440+
"index. To avoid this warning, set at least one of: `index_col` or\n",
441+
"`filters`.\n",
442+
" warnings.warn(msg, category=bfe.DefaultIndexWarning)\n"
443+
]
444+
},
445+
{
446+
"data": {
447+
"text/html": [
448+
"✅ Completed. \n",
449+
" Query processed 469.9 GB in 55 minutes of slot time. [<a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:27cb1a99-ccf8-4237-b5a6-65cc1a63f6ee&page=queryresults\">Job bigframes-dev:US.27cb1a99-ccf8-4237-b5a6-65cc1a63f6ee details</a>]\n",
450+
" "
451+
],
452+
"text/plain": [
453+
"<IPython.core.display.HTML object>"
454+
]
455+
},
456+
"metadata": {},
457+
"output_type": "display_data"
458+
},
459+
{
460+
"data": {
461+
"text/html": [
462+
"✅ Completed. \n",
463+
" Query processed 110.6 MB in 6 seconds of slot time. [<a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:job_Fu0VQ9m67G-hhsbXQne-UAqqJddU&page=queryresults\">Job bigframes-dev:US.job_Fu0VQ9m67G-hhsbXQne-UAqqJddU details</a>]\n",
464+
" "
465+
],
466+
"text/plain": [
467+
"<IPython.core.display.HTML object>"
468+
]
469+
},
470+
"metadata": {},
471+
"output_type": "display_data"
472+
},
473+
{
474+
"data": {
475+
"application/vnd.jupyter.widget-view+json": {
476+
"model_id": "e11b0c862f8d45b5aa68487678d1d2d9",
477+
"version_major": 2,
478+
"version_minor": 1
479+
},
480+
"text/plain": [
481+
"TableWidget(page_size=10, row_count=3534819, table_html='<table border=\"1\" class=\"dataframe table table-stripe…"
482+
]
483+
},
484+
"metadata": {},
485+
"output_type": "display_data"
486+
},
487+
{
488+
"data": {
489+
"text/html": [],
490+
"text/plain": [
491+
"Computation deferred. Computation will process 506.0 GB"
492+
]
493+
},
494+
"execution_count": 11,
495+
"metadata": {},
496+
"output_type": "execute_result"
497+
}
498+
],
499+
"source": [
500+
"import datetime\n",
501+
"\n",
502+
" # Read the PyPI downloads dataset\n",
503+
"pypi_df = bpd.read_gbq(\"bigquery-public-data.pypi.file_downloads\")\n",
504+
"\n",
505+
"# Filter for the last 7 days to reduce the data size for this example\n",
506+
"seven_days_ago = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days=7)\n",
507+
"pypi_df_recent = pypi_df[pypi_df[\"timestamp\"] > seven_days_ago]\n",
508+
" \n",
509+
"# Create a multi-index by grouping by date and project\n",
510+
"pypi_df_recent['date'] = pypi_df_recent['timestamp'].dt.date\n",
511+
"multi_index_df = pypi_df_recent.groupby([\"date\", \"project\"]).size().to_frame(\"downloads\")\n",
512+
" \n",
513+
"# Display the DataFrame with the multi-index\n",
514+
"multi_index_df"
515+
]
446516
}
447517
],
448518
"metadata": {

0 commit comments

Comments
 (0)