Skip to content

Commit c8e7b8f

Browse files
authored
chore: fix experimental blob _repr_html_ only previews image content type (#1306)
1 parent e4b0c8d commit c8e7b8f

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

bigframes/dataframe.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import datetime
2020
import inspect
2121
import itertools
22+
import json
2223
import re
2324
import sys
2425
import textwrap
@@ -747,10 +748,9 @@ def _repr_html_(self) -> str:
747748
if df[col].dtype == bigframes.dtypes.OBJ_REF_DTYPE
748749
]
749750
for col in blob_cols:
751+
df[col] = df[col]._apply_unary_op(ops.obj_fetch_metadata_op)
752+
# TODO(garrettwu): Not necessary to get access urls for all the rows. Update when having a to get URLs from local data.
750753
df[col] = df[col]._apply_unary_op(ops.ObjGetAccessUrl(mode="R"))
751-
df[col] = df[col]._apply_unary_op(
752-
ops.JSONValue(json_path="$.access_urls.read_url")
753-
)
754754

755755
# TODO(swast): pass max_columns and get the true column count back. Maybe
756756
# get 1 more column than we have requested so that pandas can add the
@@ -767,10 +767,21 @@ def _repr_html_(self) -> str:
767767
# Allows to preview images in the DataFrame. The implementation changes the string repr as well, that it doesn't truncate strings or escape html charaters such as "<" and ">". We may need to implement a full-fledged repr module to better support types not in pandas.
768768
if bigframes.options.experiments.blob:
769769

770-
def url_to_image_html(url: str) -> str:
771-
return f'<img src="{url}">'
770+
def obj_ref_rt_to_html(obj_ref_rt) -> str:
771+
obj_ref_rt_json = json.loads(obj_ref_rt)
772+
content_type = typing.cast(
773+
str,
774+
obj_ref_rt_json["objectref"]["details"]["gcs_metadata"][
775+
"content_type"
776+
],
777+
)
778+
if content_type.startswith("image"):
779+
url = obj_ref_rt_json["access_urls"]["read_url"]
780+
return f'<img src="{url}">'
781+
782+
return f'uri: {obj_ref_rt_json["objectref"]["uri"]}, authorizer: {obj_ref_rt_json["objectref"]["authorizer"]}'
772783

773-
formatters = {blob_col: url_to_image_html for blob_col in blob_cols}
784+
formatters = {blob_col: obj_ref_rt_to_html for blob_col in blob_cols}
774785

775786
# set max_colwidth so not to truncate the image url
776787
with pandas.option_context("display.max_colwidth", None):

bigframes/operations/blob.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def display_single_url(read_url: str, content_type: str):
182182
ipy_display.display(ipy_display.Video(url=read_url))
183183
else: # display as raw data
184184
response = requests.get(read_url)
185-
ipy_display.display(response.content, raw=True)
185+
ipy_display.display(response.content)
186186

187187
for _, row in df.iterrows():
188188
display_single_url(row["read_url"], row["content_type"])

0 commit comments

Comments
 (0)