Skip to content

Commit 3cd960e

Browse files
committed
code refactor
1 parent 6b407d4 commit 3cd960e

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
@@ -815,6 +815,7 @@ def __repr__(self) -> str:
815815
repr_string = pandas_df.to_string(**to_string_kwargs)
816816

817817
# Modify the end of the string to reflect count.
818+
# Remove pandas' default row/column summary to add our own.
818819
lines = repr_string.split("\n")
819820
pattern = re.compile("\\[[0-9]+ rows x [0-9]+ columns\\]")
820821
if pattern.match(lines[-1]):
@@ -912,52 +913,52 @@ def obj_ref_rt_to_html(obj_ref_rt) -> str:
912913
html_string += f"[{row_count} rows x {column_count} columns in total]"
913914
return html_string
914915

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

962963
except (AttributeError, ValueError, ImportError):
963964
# Fallback: let IPython use _repr_html_fallback_() instead

0 commit comments

Comments
 (0)