Show More
@@ -868,7 +868,7 class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): | |||
|
868 | 868 | # 'ConsoleWidget' protected interface |
|
869 | 869 | #-------------------------------------------------------------------------- |
|
870 | 870 | |
|
871 | def _append_custom(self, insert, input, before_prompt=False): | |
|
871 | def _append_custom(self, insert, input, before_prompt=False, *args, **kwargs): | |
|
872 | 872 | """ A low-level method for appending content to the end of the buffer. |
|
873 | 873 | |
|
874 | 874 | If 'before_prompt' is enabled, the content will be inserted before the |
@@ -883,7 +883,7 class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): | |||
|
883 | 883 | start_pos = cursor.position() |
|
884 | 884 | |
|
885 | 885 | # Perform the insertion. |
|
886 | result = insert(cursor, input) | |
|
886 | result = insert(cursor, input, *args, **kwargs) | |
|
887 | 887 | |
|
888 | 888 | # Adjust the prompt position if we have inserted before it. This is safe |
|
889 | 889 | # because buffer truncation is disabled when not executing. |
@@ -117,17 +117,20 class RichIPythonWidget(IPythonWidget): | |||
|
117 | 117 | content = msg['content'] |
|
118 | 118 | prompt_number = content.get('execution_count', 0) |
|
119 | 119 | data = content['data'] |
|
120 | metadata = msg['content']['metadata'] | |
|
120 | 121 | if 'image/svg+xml' in data: |
|
121 | 122 | self._pre_image_append(msg, prompt_number) |
|
122 | 123 | self._append_svg(data['image/svg+xml'], True) |
|
123 | 124 | self._append_html(self.output_sep2, True) |
|
124 | 125 | elif 'image/png' in data: |
|
125 | 126 | self._pre_image_append(msg, prompt_number) |
|
126 |
|
|
|
127 | png = decodestring(data['image/png'].encode('ascii')) | |
|
128 | self._append_png(png, True, metadata=metadata.get('image/png', None)) | |
|
127 | 129 | self._append_html(self.output_sep2, True) |
|
128 | 130 | elif 'image/jpeg' in data and self._jpg_supported: |
|
129 | 131 | self._pre_image_append(msg, prompt_number) |
|
130 |
|
|
|
132 | jpg = decodestring(data['image/jpeg'].encode('ascii')) | |
|
133 | self._append_jpg(jpg, True, metadata=metadata.get('image/jpeg', None)) | |
|
131 | 134 | self._append_html(self.output_sep2, True) |
|
132 | 135 | else: |
|
133 | 136 | # Default back to the plain text representation. |
@@ -151,11 +154,11 class RichIPythonWidget(IPythonWidget): | |||
|
151 | 154 | # PNG data is base64 encoded as it passes over the network |
|
152 | 155 | # in a JSON structure so we decode it. |
|
153 | 156 | png = decodestring(data['image/png'].encode('ascii')) |
|
154 | self._append_png(png, True) | |
|
157 | self._append_png(png, True, metadata=metadata.get('image/png', None)) | |
|
155 | 158 | elif 'image/jpeg' in data and self._jpg_supported: |
|
156 | 159 | self.log.debug("display: %s", msg.get('content', '')) |
|
157 | 160 | jpg = decodestring(data['image/jpeg'].encode('ascii')) |
|
158 | self._append_jpg(jpg, True) | |
|
161 | self._append_jpg(jpg, True, metadata=metadata.get('image/jpeg', None)) | |
|
159 | 162 | else: |
|
160 | 163 | # Default back to the plain text representation. |
|
161 | 164 | return super(RichIPythonWidget, self)._handle_display_data(msg) |
@@ -164,14 +167,14 class RichIPythonWidget(IPythonWidget): | |||
|
164 | 167 | # 'RichIPythonWidget' protected interface |
|
165 | 168 | #--------------------------------------------------------------------------- |
|
166 | 169 | |
|
167 | def _append_jpg(self, jpg, before_prompt=False): | |
|
170 | def _append_jpg(self, jpg, before_prompt=False, metadata=None): | |
|
168 | 171 | """ Append raw JPG data to the widget.""" |
|
169 | self._append_custom(self._insert_jpg, jpg, before_prompt) | |
|
172 | self._append_custom(self._insert_jpg, jpg, before_prompt, metadata=metadata) | |
|
170 | 173 | |
|
171 | def _append_png(self, png, before_prompt=False): | |
|
174 | def _append_png(self, png, before_prompt=False, metadata=None): | |
|
172 | 175 | """ Append raw PNG data to the widget. |
|
173 | 176 | """ |
|
174 | self._append_custom(self._insert_png, png, before_prompt) | |
|
177 | self._append_custom(self._insert_png, png, before_prompt, metadata=metadata) | |
|
175 | 178 | |
|
176 | 179 | def _append_svg(self, svg, before_prompt=False): |
|
177 | 180 | """ Append raw SVG data to the widget. |
@@ -276,20 +279,31 class RichIPythonWidget(IPythonWidget): | |||
|
276 | 279 | else: |
|
277 | 280 | return '<b>Unrecognized image format</b>' |
|
278 | 281 | |
|
279 | def _insert_jpg(self, cursor, jpg): | |
|
282 | def _insert_jpg(self, cursor, jpg, metadata=None): | |
|
280 | 283 | """ Insert raw PNG data into the widget.""" |
|
281 | self._insert_img(cursor, jpg, 'jpg') | |
|
284 | self._insert_img(cursor, jpg, 'jpg', metadata=metadata) | |
|
282 | 285 | |
|
283 | def _insert_png(self, cursor, png): | |
|
286 | def _insert_png(self, cursor, png, metadata=None): | |
|
284 | 287 | """ Insert raw PNG data into the widget. |
|
285 | 288 | """ |
|
286 | self._insert_img(cursor, png, 'png') | |
|
289 | self._insert_img(cursor, png, 'png', metadata=metadata) | |
|
287 | 290 | |
|
288 | def _insert_img(self, cursor, img, fmt): | |
|
291 | def _insert_img(self, cursor, img, fmt, metadata=None): | |
|
289 | 292 | """ insert a raw image, jpg or png """ |
|
293 | if metadata: | |
|
294 | width = metadata.get('width', None) | |
|
295 | height = metadata.get('height', None) | |
|
296 | else: | |
|
297 | width = height = None | |
|
290 | 298 | try: |
|
291 | 299 | image = QtGui.QImage() |
|
292 | 300 | image.loadFromData(img, fmt.upper()) |
|
301 | if width and height: | |
|
302 | image = image.scaled(width, height) | |
|
303 | elif width and not height: | |
|
304 | image = image.scaledToWidth(width) | |
|
305 | elif height and not width: | |
|
306 | image = image.scaledToHeight(height) | |
|
293 | 307 | except ValueError: |
|
294 | 308 | self._insert_plain_text(cursor, 'Received invalid %s data.'%fmt) |
|
295 | 309 | else: |
General Comments 0
You need to be logged in to leave comments.
Login now