Skip to content

Commit 30a6237

Browse files
authored
chore: add blob display size options (#1657)
1 parent c46ad06 commit 30a6237

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed

‎bigframes/_config/experiment_options.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from typing import Optional
1516
import warnings
1617

1718
import bigframes.exceptions as bfe
@@ -26,6 +27,9 @@ def __init__(self):
2627
self._semantic_operators: bool = False
2728
self._ai_operators: bool = False
2829
self._blob: bool = False
30+
self._blob_display: bool = True
31+
self._blob_display_width: Optional[int] = None
32+
self._blob_display_height: Optional[int] = None
2933

3034
@property
3135
def semantic_operators(self) -> bool:
@@ -67,3 +71,30 @@ def blob(self, value: bool):
6771
)
6872
warnings.warn(msg, category=bfe.PreviewWarning)
6973
self._blob = value
74+
75+
@property
76+
def blob_display(self) -> bool:
77+
"""Whether to display the blob content in notebook DataFrame preview. Default True."""
78+
return self._blob_display
79+
80+
@blob_display.setter
81+
def blob_display(self, value: bool):
82+
self._blob_display = value
83+
84+
@property
85+
def blob_display_width(self) -> Optional[int]:
86+
"""Width in pixels that the blob constrained to."""
87+
return self._blob_display_width
88+
89+
@blob_display_width.setter
90+
def blob_display_width(self, value: Optional[int]):
91+
self._blob_display_width = value
92+
93+
@property
94+
def blob_display_height(self) -> Optional[int]:
95+
"""Height in pixels that the blob constrained to."""
96+
return self._blob_display_height
97+
98+
@blob_display_height.setter
99+
def blob_display_height(self, value: Optional[int]):
100+
self._blob_display_height = value

‎bigframes/dataframe.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,10 @@ def _repr_html_(self) -> str:
765765
return formatter.repr_query_job(self._compute_dry_run())
766766

767767
df = self.copy()
768-
if bigframes.options.experiments.blob:
768+
if (
769+
bigframes.options.experiments.blob
770+
and bigframes.options.experiments.blob_display
771+
):
769772
blob_cols = [
770773
col
771774
for col in df.columns
@@ -788,7 +791,10 @@ def _repr_html_(self) -> str:
788791

789792
with display_options.pandas_repr(opts):
790793
# 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.
791-
if bigframes.options.experiments.blob:
794+
if (
795+
bigframes.options.experiments.blob
796+
and bigframes.options.experiments.blob_display
797+
):
792798

793799
def obj_ref_rt_to_html(obj_ref_rt) -> str:
794800
obj_ref_rt_json = json.loads(obj_ref_rt)
@@ -799,8 +805,16 @@ def obj_ref_rt_to_html(obj_ref_rt) -> str:
799805
str, gcs_metadata.get("content_type", "")
800806
)
801807
if content_type.startswith("image"):
808+
size_str = ""
809+
if bigframes.options.experiments.blob_display_width:
810+
size_str = f' width="{bigframes.options.experiments.blob_display_width}"'
811+
if bigframes.options.experiments.blob_display_height:
812+
size_str = (
813+
size_str
814+
+ f' height="{bigframes.options.experiments.blob_display_height}"'
815+
)
802816
url = obj_ref_rt_json["access_urls"]["read_url"]
803-
return f'<img src="{url}">'
817+
return f'<img src="{url}"{size_str}>'
804818

805819
return f'uri: {obj_ref_rt_json["objectref"]["uri"]}, authorizer: {obj_ref_rt_json["objectref"]["authorizer"]}'
806820

‎bigframes/operations/blob.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,12 @@ def display(
241241
Args:
242242
n (int, default 3): number of sample blob objects to display.
243243
content_type (str, default ""): content type of the blob. If unset, use the blob metadata of the storage. Possible values are "image", "audio" and "video".
244-
width (int or None, default None): width in pixels that the image/video are constrained to. If unset, use the image/video's original size or ratio. No-op for other content types.
245-
height (int or None, default None): height in pixels that the image/video are constrained to. If unset, use the image/video's original size or ratio. No-op for other content types.
244+
width (int or None, default None): width in pixels that the image/video are constrained to. If unset, use the global setting in bigframes.options.experiments.blob_display_width, otherwise image/video's original size or ratio is used. No-op for other content types.
245+
height (int or None, default None): height in pixels that the image/video are constrained to. If unset, use the global setting in bigframes.options.experiments.blob_display_height, otherwise image/video's original size or ratio is used. No-op for other content types.
246246
"""
247+
width = width or bigframes.options.experiments.blob_display_width
248+
height = height or bigframes.options.experiments.blob_display_height
249+
247250
# col name doesn't matter here. Rename to avoid column name conflicts
248251
df = bigframes.series.Series(self._block).rename("blob_col").to_frame()
249252

‎samples/snippets/multimodal_test.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ def test_multimodal_dataframe(gcs_dst_bucket: str) -> None:
2121

2222
# Flag to enable the feature
2323
bigframes.options.experiments.blob = True
24+
# Flags to control preview image/video preview size
25+
bigframes.options.experiments.blob_display_width = 300
2426

2527
import bigframes.pandas as bpd
2628

@@ -47,10 +49,12 @@ def test_multimodal_dataframe(gcs_dst_bucket: str) -> None:
4749
df_image["size"] = df_image["image"].blob.size()
4850
df_image["updated"] = df_image["image"].blob.updated()
4951
df_image
52+
# [END bigquery_dataframes_multimodal_dataframe_merge]
5053

54+
# [START bigquery_dataframes_multimodal_dataframe_filter]
5155
# Filter images and display, you can also display audio and video types. Use width/height parameters to constrain window sizes.
52-
df_image[df_image["author"] == "alice"]["image"].blob.display(width=400)
53-
# [END bigquery_dataframes_multimodal_dataframe_merge]
56+
df_image[df_image["author"] == "alice"]["image"].blob.display()
57+
# [END bigquery_dataframes_multimodal_dataframe_filter]
5458

5559
# [START bigquery_dataframes_multimodal_dataframe_image_transform]
5660
df_image["blurred"] = df_image["image"].blob.image_blur(

0 commit comments

Comments
 (0)