Skip to content

Commit 61a9903

Browse files
committed
remove _repr_html_()
1 parent 2c206e8 commit 61a9903

File tree

4 files changed

+469
-95
lines changed

4 files changed

+469
-95
lines changed

bigframes/dataframe.py

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

824-
def _repr_html_(self) -> str:
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:
825874
"""
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.
875+
Generates a static HTML table as a fallback representation.
829876
"""
830877
opts = bigframes.options.display
831878
max_results = opts.max_rows
@@ -906,55 +953,6 @@ def obj_ref_rt_to_html(obj_ref_rt) -> str:
906953
html_string += f"[{row_count} rows x {column_count} columns in total]"
907954
return html_string
908955

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-
958956
def __delitem__(self, key: str):
959957
df = self.drop(columns=[key])
960958
self._set_block(df._get_block())

0 commit comments

Comments
 (0)