Skip to content

Commit

Permalink
Merge pull request #2111 from mwcraig/no-value-in-image-repr
Browse files Browse the repository at this point in the history
Remove value from the Image repr
  • Loading branch information
jasongrout authored Jul 16, 2018
2 parents 40b0741 + 2ad1e28 commit 884e171
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ipywidgets/widgets/tests/test_widget_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ def test_format_inference_overridable():
assert img.format == 'gif'


def test_value_repr_length():
with get_logo_png() as LOGO_PNG:
with open(LOGO_PNG, 'rb') as f:
img = Image.from_file(f)
assert len(img.__repr__()) < 120
assert img.__repr__().endswith("...')")


def test_value_repr_url():
img = Image.from_url(b'https://jupyter.org/assets/main-logo.svg')

assert 'https://jupyter.org/assets/main-logo.svg' in img.__repr__()


# Helper functions
def get_hash_hex(byte_str):
m = hashlib.new('sha256')
Expand Down
23 changes: 23 additions & 0 deletions ipywidgets/widgets/widget_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Represents an image in the frontend using a widget.
"""
import mimetypes
import hashlib

from .widget_core import CoreWidget
from .domwidget import DOMWidget
Expand Down Expand Up @@ -128,3 +129,25 @@ def _guess_format(cls, filename):
return mtype[len('image/'):]
except Exception:
return None

def __repr__(self):
# Truncate the value in the repr, since it will
# typically be very, very large.
class_name = self.__class__.__name__

# Return value first like a ValueWidget
signature = []
sig_value = repr(self.value)
prefix, rest = sig_value.split("'", 1)
content = rest[:-1]
if len(content) > 100:
sig_value = "{}'{}...'".format(prefix, content[0:100])
signature.append('%s=%s' % ('value', sig_value))

for key in super(Image, self)._repr_keys():
if key == 'value':
continue
value = str(getattr(self, key))
signature.append('%s=%r' % (key, value))
signature = ', '.join(signature)
return '%s(%s)' % (class_name, signature)

0 comments on commit 884e171

Please sign in to comment.