Skip to content

Commit 2ad1e28

Browse files
committed
Simply truncate long image values and add tests
1 parent 0517cf7 commit 2ad1e28

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

ipywidgets/widgets/tests/test_widget_image.py

+14
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ def test_format_inference_overridable():
130130
assert img.format == 'gif'
131131

132132

133+
def test_value_repr_length():
134+
with get_logo_png() as LOGO_PNG:
135+
with open(LOGO_PNG, 'rb') as f:
136+
img = Image.from_file(f)
137+
assert len(img.__repr__()) < 120
138+
assert img.__repr__().endswith("...')")
139+
140+
141+
def test_value_repr_url():
142+
img = Image.from_url(b'https://jupyter.org/assets/main-logo.svg')
143+
144+
assert 'https://jupyter.org/assets/main-logo.svg' in img.__repr__()
145+
146+
133147
# Helper functions
134148
def get_hash_hex(byte_str):
135149
m = hashlib.new('sha256')

ipywidgets/widgets/widget_image.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,16 @@ def _guess_format(cls, filename):
133133
def __repr__(self):
134134
# Truncate the value in the repr, since it will
135135
# typically be very, very large.
136-
137-
def hash_string(s):
138-
return hashlib.md5(s.encode("utf8")).hexdigest()
139-
140136
class_name = self.__class__.__name__
141137

142138
# Return value first like a ValueWidget
143139
signature = []
144-
sig_value = str(self.value)
145-
if len(str(self.value)) > 100:
146-
sig_value = '<base64, hash={}...>'.format(hash_string(sig_value)[:16])
147-
signature.append('%s=%r' % ('value', sig_value))
140+
sig_value = repr(self.value)
141+
prefix, rest = sig_value.split("'", 1)
142+
content = rest[:-1]
143+
if len(content) > 100:
144+
sig_value = "{}'{}...'".format(prefix, content[0:100])
145+
signature.append('%s=%s' % ('value', sig_value))
148146

149147
for key in super(Image, self)._repr_keys():
150148
if key == 'value':

0 commit comments

Comments
 (0)