File tree 2 files changed +37
-0
lines changed
2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -130,6 +130,20 @@ def test_format_inference_overridable():
130
130
assert img .format == 'gif'
131
131
132
132
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
+
133
147
# Helper functions
134
148
def get_hash_hex (byte_str ):
135
149
m = hashlib .new ('sha256' )
Original file line number Diff line number Diff line change 6
6
Represents an image in the frontend using a widget.
7
7
"""
8
8
import mimetypes
9
+ import hashlib
9
10
10
11
from .widget_core import CoreWidget
11
12
from .domwidget import DOMWidget
@@ -128,3 +129,25 @@ def _guess_format(cls, filename):
128
129
return mtype [len ('image/' ):]
129
130
except Exception :
130
131
return None
132
+
133
+ def __repr__ (self ):
134
+ # Truncate the value in the repr, since it will
135
+ # typically be very, very large.
136
+ class_name = self .__class__ .__name__
137
+
138
+ # Return value first like a ValueWidget
139
+ signature = []
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 ))
146
+
147
+ for key in super (Image , self )._repr_keys ():
148
+ if key == 'value' :
149
+ continue
150
+ value = str (getattr (self , key ))
151
+ signature .append ('%s=%r' % (key , value ))
152
+ signature = ', ' .join (signature )
153
+ return '%s(%s)' % (class_name , signature )
You can’t perform that action at this time.
0 commit comments