Skip to content

Commit 9a1ee30

Browse files
committed
Revert "remove _repr_html_()"
This reverts commit 61a9903.
1 parent 61a9903 commit 9a1ee30

File tree

4 files changed

+95
-469
lines changed

4 files changed

+95
-469
lines changed

bigframes/dataframe.py

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -821,58 +821,11 @@ def __repr__(self) -> str:
821821
lines.append(f"[{row_count} rows x {column_count} columns]")
822822
return "\n".join(lines)
823823

824-
def _repr_mimebundle_(self, include=None, exclude=None):
825-
"""
826-
Custom display method for IPython/Jupyter environments.
827-
This is called by IPython's display system when the object is displayed.
828-
"""
829-
opts = bigframes.options.display
830-
831-
# Only handle widget display in anywidget mode
832-
if opts.repr_mode == "anywidget":
833-
try:
834-
from bigframes import display
835-
836-
# Process blob columns if needed
837-
self._cached()
838-
df = self.copy()
839-
if bigframes.options.display.blob_display:
840-
blob_cols = [
841-
series_name
842-
for series_name, series in df.items()
843-
if series.dtype == bigframes.dtypes.OBJ_REF_DTYPE
844-
]
845-
for col in blob_cols:
846-
df[col] = df[col].blob._get_runtime(
847-
mode="R", with_metadata=True
848-
)
849-
850-
# Create and display the widget
851-
widget = display.TableWidget(df)
852-
widget_repr = widget._repr_mimebundle_(include=include, exclude=exclude)
853-
854-
# Use deferred repr for text/plain of anywidget display.
855-
# This avoids kicking off a query when the user is just
856-
# printing the last expression in a cell.
857-
widget_repr["text/plain"] = repr(self)
858-
widget_repr["text/html"] = self._repr_html_fallback()
859-
return widget_repr
860-
861-
except (AttributeError, ValueError, ImportError):
862-
# Fallback: let IPython use _repr_html_() instead
863-
warnings.warn(
864-
"Anywidget mode is not available. "
865-
"Please `pip install anywidget traitlets` or `pip install 'bigframes[anywidget]'` to use interactive tables. "
866-
f"Falling back to static HTML. Error: {traceback.format_exc()}"
867-
)
868-
# Don't return anything - let IPython fall back to _repr_html_()
869-
pass
870-
871-
return {"text/html": self._repr_html_fallback(), "text/plain": repr(self)}
872-
873-
def _repr_html_fallback(self) -> str:
824+
def _repr_html_(self) -> str:
874825
"""
875-
Generates a static HTML table as a fallback representation.
826+
Returns an html string primarily for use by notebooks for displaying
827+
a representation of the DataFrame. Displays 20 rows by default since
828+
many notebooks are not configured for large tables.
876829
"""
877830
opts = bigframes.options.display
878831
max_results = opts.max_rows
@@ -953,6 +906,55 @@ def obj_ref_rt_to_html(obj_ref_rt) -> str:
953906
html_string += f"[{row_count} rows x {column_count} columns in total]"
954907
return html_string
955908

909+
def _repr_mimebundle_(self, include=None, exclude=None):
910+
"""
911+
Custom display method for IPython/Jupyter environments.
912+
This is called by IPython's display system when the object is displayed.
913+
"""
914+
opts = bigframes.options.display
915+
916+
# Only handle widget display in anywidget mode
917+
if opts.repr_mode == "anywidget":
918+
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 = widget._repr_mimebundle_(include=include, exclude=exclude)
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(self)
943+
widget_repr["text/html"] = self._repr_html_()
944+
return widget_repr
945+
946+
except (AttributeError, ValueError, ImportError):
947+
# Fallback: let IPython use _repr_html_() instead
948+
warnings.warn(
949+
"Anywidget mode is not available. "
950+
"Please `pip install anywidget traitlets` or `pip install 'bigframes[anywidget]'` to use interactive tables. "
951+
f"Falling back to static HTML. Error: {traceback.format_exc()}"
952+
)
953+
# Don't return anything - let IPython fall back to _repr_html_()
954+
pass
955+
956+
return {"text/html": self._repr_html_(), "text/plain": repr(self)}
957+
956958
def __delitem__(self, key: str):
957959
df = self.drop(columns=[key])
958960
self._set_block(df._get_block())

0 commit comments

Comments
 (0)