Skip to content

Commit 64230d1

Browse files
committed
fix: refactor html display to address review comments
1 parent 38899b7 commit 64230d1

File tree

1 file changed

+40
-44
lines changed

1 file changed

+40
-44
lines changed

bigframes/display/html.py

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,24 @@ def render_html(
104104
return "\n".join(table_html)
105105

106106

107+
def _obj_ref_rt_to_html(obj_ref_rt: str) -> str:
108+
obj_ref_rt_json = json.loads(obj_ref_rt)
109+
obj_ref_details = obj_ref_rt_json["objectref"]["details"]
110+
if "gcs_metadata" in obj_ref_details:
111+
gcs_metadata = obj_ref_details["gcs_metadata"]
112+
content_type = typing.cast(str, gcs_metadata.get("content_type", ""))
113+
if content_type.startswith("image"):
114+
size_str = ""
115+
if options.display.blob_display_width:
116+
size_str = f' width="{options.display.blob_display_width}"'
117+
if options.display.blob_display_height:
118+
size_str = size_str + f' height="{options.display.blob_display_height}"'
119+
url = obj_ref_rt_json["access_urls"]["read_url"]
120+
return f'<img src="{url}"{size_str}>'
121+
122+
return f'uri: {obj_ref_rt_json["objectref"]["uri"]}, authorizer: {obj_ref_rt_json["objectref"]["authorizer"]}'
123+
124+
107125
def create_html_representation(
108126
obj: Union[bigframes.dataframe.DataFrame, bigframes.series.Series],
109127
pandas_df: pd.DataFrame,
@@ -114,52 +132,30 @@ def create_html_representation(
114132
"""Create an HTML representation of the DataFrame or Series."""
115133
from bigframes.series import Series
116134

117-
if isinstance(obj, Series):
118-
# Some pandas objects may not have a _repr_html_ method, or it might
119-
# fail in certain environments. We fall back to a pre-formatted
120-
# string representation to ensure something is always displayed.
121-
pd_series = pandas_df.iloc[:, 0]
122-
try:
123-
html_string = pd_series._repr_html_()
124-
except AttributeError:
125-
html_string = f"<pre>{pd_series.to_string()}</pre>"
126-
127-
html_string += f"[{total_rows} rows]"
128-
return html_string
129-
else:
130-
# It's a DataFrame
131-
opts = options.display
132-
with display_options.pandas_repr(opts):
135+
opts = options.display
136+
with display_options.pandas_repr(opts):
137+
if isinstance(obj, Series):
138+
# Some pandas objects may not have a _repr_html_ method, or it might
139+
# fail in certain environments. We fall back to a pre-formatted
140+
# string representation to ensure something is always displayed.
141+
pd_series = pandas_df.iloc[:, 0]
142+
try:
143+
# TODO(b/464053870): Support rich display for blob Series.
144+
html_string = pd_series._repr_html_()
145+
except AttributeError:
146+
html_string = f"<pre>{pd_series.to_string()}</pre>"
147+
148+
is_truncated = total_rows is not None and total_rows > len(pandas_df)
149+
if is_truncated:
150+
html_string += f"<p>[{total_rows} rows]</p>"
151+
return html_string
152+
else:
153+
# It's a DataFrame
133154
# TODO(shuowei, b/464053870): Escaping HTML would be useful, but
134155
# `escape=False` is needed to show images. We may need to implement
135156
# a full-fledged repr module to better support types not in pandas.
136157
if options.display.blob_display and blob_cols:
137-
138-
def obj_ref_rt_to_html(obj_ref_rt) -> str:
139-
obj_ref_rt_json = json.loads(obj_ref_rt)
140-
obj_ref_details = obj_ref_rt_json["objectref"]["details"]
141-
if "gcs_metadata" in obj_ref_details:
142-
gcs_metadata = obj_ref_details["gcs_metadata"]
143-
content_type = typing.cast(
144-
str, gcs_metadata.get("content_type", "")
145-
)
146-
if content_type.startswith("image"):
147-
size_str = ""
148-
if options.display.blob_display_width:
149-
size_str = (
150-
f' width="{options.display.blob_display_width}"'
151-
)
152-
if options.display.blob_display_height:
153-
size_str = (
154-
size_str
155-
+ f' height="{options.display.blob_display_height}"'
156-
)
157-
url = obj_ref_rt_json["access_urls"]["read_url"]
158-
return f'<img src="{url}"{size_str}>'
159-
160-
return f'uri: {obj_ref_rt_json["objectref"]["uri"]}, authorizer: {obj_ref_rt_json["objectref"]["authorizer"]}'
161-
162-
formatters = {blob_col: obj_ref_rt_to_html for blob_col in blob_cols}
158+
formatters = {blob_col: _obj_ref_rt_to_html for blob_col in blob_cols}
163159

164160
# set max_colwidth so not to truncate the image url
165161
with pandas.option_context("display.max_colwidth", None):
@@ -175,8 +171,8 @@ def obj_ref_rt_to_html(obj_ref_rt) -> str:
175171
# _repr_html_ stub is missing so mypy thinks it's a Series. Ignore mypy.
176172
html_string = pandas_df._repr_html_() # type:ignore
177173

178-
html_string += f"[{total_rows} rows x {total_columns} columns in total]"
179-
return html_string
174+
html_string += f"[{total_rows} rows x {total_columns} columns in total]"
175+
return html_string
180176

181177

182178
def _get_obj_metadata(

0 commit comments

Comments
 (0)