Skip to content

Commit 0a1ba4f

Browse files
committed
perf: Default to interactive display for SQL in anywidget mode
Previously, SQL queries in anywidget mode would fall back to deferred execution, showing a dry run instead of an interactive table. This change modifies the display logic to directly use the anywidget interactive display for SQL queries, providing a more consistent and responsive user experience. A test case has been added to verify this behavior.
1 parent a1bb974 commit 0a1ba4f

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

bigframes/dataframe.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -783,11 +783,26 @@ def __repr__(self) -> str:
783783

784784
opts = bigframes.options.display
785785
max_results = opts.max_rows
786-
# anywdiget mode uses the same display logic as the "deferred" mode
787-
# for faster execution
788-
if opts.repr_mode in ("deferred", "anywidget"):
786+
787+
# Only deferred mode shows dry run
788+
if opts.repr_mode in ("deferred"):
789789
return formatter.repr_query_job(self._compute_dry_run())
790790

791+
# Anywidget mode uses interative display
792+
if opts.repr_mode == "anywidget":
793+
# Try to display with anywidget, fall back to deferred if not in IPython
794+
try:
795+
from IPython.display import display as ipython_display
796+
797+
from bigframes import display
798+
799+
widget = display.TableWidget(self.copy())
800+
ipython_display(widget)
801+
return "" # Return empty string since we used display()
802+
except (AttributeError, ValueError, ImportError):
803+
# Not in IPython environment, fall back to deferred mode
804+
return formatter.repr_query_job(self._compute_dry_run())
805+
791806
# TODO(swast): pass max_columns and get the true column count back. Maybe
792807
# get 1 more column than we have requested so that pandas can add the
793808
# ... for us?

tests/system/small/test_anywidget.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,21 @@ def test_widget_creation_should_load_css_for_rendering(table_widget):
436436
assert ".bigframes-widget .footer" in css_content
437437

438438

439+
def test_sql_anywidget_mode(session: bf.Session):
440+
"""
441+
Test that a SQL query runs in anywidget mode.
442+
"""
443+
sql = "SELECT * FROM `bigquery-public-data.usa_names.usa_1910_current` LIMIT 5"
444+
445+
with bf.option_context("display.repr_mode", "anywidget"):
446+
df = session.read_gbq(sql)
447+
# In a real environment, this would display a widget.
448+
# For testing, we just want to make sure we're in the anywidget code path.
449+
# The `_repr_html_` method in anywidget mode will return an empty string
450+
# and display the widget via IPython's display mechanism.
451+
assert df._repr_html_() == ""
452+
453+
439454
# TODO(shuowei): Add tests for custom index and multiindex
440455
# This may not be necessary for the SQL Cell use case but should be
441456
# considered for completeness.

0 commit comments

Comments
 (0)