Skip to content

Commit 92fa260

Browse files
committed
code refactor
1 parent 6d1a28d commit 92fa260

File tree

4 files changed

+147
-146
lines changed

4 files changed

+147
-146
lines changed

bigframes/dataframe.py

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,7 @@ def __repr__(self) -> str:
809809
repr_string = pandas_df.to_string(**to_string_kwargs)
810810

811811
# Modify the end of the string to reflect count.
812+
# Remove pandas' default row/column summary to add our own.
812813
lines = repr_string.split("\n")
813814
pattern = re.compile("\\[[0-9]+ rows x [0-9]+ columns\\]")
814815
if pattern.match(lines[-1]):
@@ -906,52 +907,52 @@ def obj_ref_rt_to_html(obj_ref_rt) -> str:
906907
html_string += f"[{row_count} rows x {column_count} columns in total]"
907908
return html_string
908909

910+
def _get_anywidget_bundle(self, include=None, exclude=None):
911+
"""
912+
Helper method to create and return the anywidget mimebundle.
913+
This function encapsulates the logic for anywidget display.
914+
"""
915+
from bigframes import display
916+
917+
# Process blob columns if needed
918+
self._cached()
919+
df = self.copy()
920+
if bigframes.options.display.blob_display:
921+
blob_cols = [
922+
series_name
923+
for series_name, series in df.items()
924+
if series.dtype == bigframes.dtypes.OBJ_REF_DTYPE
925+
]
926+
for col in blob_cols:
927+
df[col] = df[col].blob._get_runtime(mode="R", with_metadata=True)
928+
929+
# Create and display the widget
930+
widget = display.TableWidget(df)
931+
widget_repr_result = widget._repr_mimebundle_(include=include, exclude=exclude)
932+
933+
# Handle both tuple (data, metadata) and dict returns
934+
if isinstance(widget_repr_result, tuple):
935+
widget_repr = dict(widget_repr_result[0]) # Extract data dict from tuple
936+
else:
937+
widget_repr = dict(widget_repr_result)
938+
939+
# Use deferred repr for text/plain of anywidget display.
940+
# This avoids kicking off a query when the user is just
941+
# printing the last expression in a cell.
942+
widget_repr["text/plain"] = repr(df)
943+
widget_repr["text/html"] = self._repr_html_fallback_()
944+
return widget_repr
945+
909946
def _repr_mimebundle_(self, include=None, exclude=None):
910947
"""
911948
Custom display method for IPython/Jupyter environments.
912949
This is called by IPython's display system when the object is displayed.
913950
"""
914951
opts = bigframes.options.display
915-
916952
# Only handle widget display in anywidget mode
917953
if opts.repr_mode == "anywidget":
918954
try:
919-
from bigframes import display
920-
921-
# Process blob columns if needed
922-
self._cached()
923-
df = self.copy()
924-
if bigframes.options.display.blob_display:
925-
blob_cols = [
926-
series_name
927-
for series_name, series in df.items()
928-
if series.dtype == bigframes.dtypes.OBJ_REF_DTYPE
929-
]
930-
for col in blob_cols:
931-
df[col] = df[col].blob._get_runtime(
932-
mode="R", with_metadata=True
933-
)
934-
935-
# Create and display the widget
936-
widget = display.TableWidget(df)
937-
widget_repr_result = widget._repr_mimebundle_(
938-
include=include, exclude=exclude
939-
)
940-
941-
# Handle both tuple (data, metadata) and dict returns
942-
if isinstance(widget_repr_result, tuple):
943-
widget_repr = dict(
944-
widget_repr_result[0]
945-
) # Extract data dict from tuple
946-
else:
947-
widget_repr = dict(widget_repr_result)
948-
949-
# Use deferred repr for text/plain of anywidget display.
950-
# This avoids kicking off a query when the user is just
951-
# printing the last expression in a cell.
952-
widget_repr["text/plain"] = repr(df)
953-
widget_repr["text/html"] = self._repr_html_fallback_()
954-
return widget_repr
955+
return self._get_anywidget_bundle(include=include, exclude=exclude)
955956

956957
except (AttributeError, ValueError, ImportError):
957958
# Fallback: let IPython use _repr_html_fallback_() instead

0 commit comments

Comments
 (0)