##// END OF EJS Templates
Use safe appending in RichIPythonWidget.
epatters -
Show More
@@ -217,12 +217,12 b' class IPythonWidget(FrontendWidget):'
217 # representation.
217 # representation.
218 if data.has_key('text/html'):
218 if data.has_key('text/html'):
219 html = data['text/html']
219 html = data['text/html']
220 self._append_html(html, before_prompt=True)
220 self._append_html(html, True)
221 elif data.has_key('text/plain'):
221 elif data.has_key('text/plain'):
222 text = data['text/plain']
222 text = data['text/plain']
223 self._append_plain_text(text, before_prompt=True)
223 self._append_plain_text(text, True)
224 # This newline seems to be needed for text and html output.
224 # This newline seems to be needed for text and html output.
225 self._append_plain_text(u'\n', before_prompt=True)
225 self._append_plain_text(u'\n', True)
226
226
227 def _started_channels(self):
227 def _started_channels(self):
228 """ Reimplemented to make a history request.
228 """ Reimplemented to make a history request.
@@ -74,20 +74,17 b' class RichIPythonWidget(IPythonWidget):'
74 prompt_number = content['execution_count']
74 prompt_number = content['execution_count']
75 data = content['data']
75 data = content['data']
76 if data.has_key('image/svg+xml'):
76 if data.has_key('image/svg+xml'):
77 self._append_plain_text(self.output_sep)
77 self._append_plain_text(self.output_sep, True)
78 self._append_html(self._make_out_prompt(prompt_number))
78 self._append_html(self._make_out_prompt(prompt_number), True)
79 # TODO: try/except this call.
79 self._append_svg(data['image/svg+xml'], True)
80 self._append_svg(data['image/svg+xml'])
80 self._append_html(self.output_sep2, True)
81 self._append_html(self.output_sep2)
82 elif data.has_key('image/png'):
81 elif data.has_key('image/png'):
83 self._append_plain_text(self.output_sep)
82 self._append_plain_text(self.output_sep, True)
84 self._append_html(self._make_out_prompt(prompt_number))
83 self._append_html(self._make_out_prompt(prompt_number), True)
85 # This helps the output to look nice.
84 # This helps the output to look nice.
86 self._append_plain_text('\n')
85 self._append_plain_text('\n', True)
87 # TODO: try/except these calls
86 self._append_png(decodestring(data['image/png']), True)
88 png = decodestring(data['image/png'])
87 self._append_html(self.output_sep2, True)
89 self._append_png(png)
90 self._append_html(self.output_sep2)
91 else:
88 else:
92 # Default back to the plain text representation.
89 # Default back to the plain text representation.
93 return super(RichIPythonWidget, self)._handle_pyout(msg)
90 return super(RichIPythonWidget, self)._handle_pyout(msg)
@@ -103,14 +100,12 b' class RichIPythonWidget(IPythonWidget):'
103 # FIXME: Is this the right ordering of things to try?
100 # FIXME: Is this the right ordering of things to try?
104 if data.has_key('image/svg+xml'):
101 if data.has_key('image/svg+xml'):
105 svg = data['image/svg+xml']
102 svg = data['image/svg+xml']
106 # TODO: try/except this call.
103 self._append_svg(svg, True)
107 self._append_svg(svg)
108 elif data.has_key('image/png'):
104 elif data.has_key('image/png'):
109 # TODO: try/except these calls
110 # PNG data is base64 encoded as it passes over the network
105 # PNG data is base64 encoded as it passes over the network
111 # in a JSON structure so we decode it.
106 # in a JSON structure so we decode it.
112 png = decodestring(data['image/png'])
107 png = decodestring(data['image/png'])
113 self._append_png(png)
108 self._append_png(png, True)
114 else:
109 else:
115 # Default back to the plain text representation.
110 # Default back to the plain text representation.
116 return super(RichIPythonWidget, self)._handle_display_data(msg)
111 return super(RichIPythonWidget, self)._handle_display_data(msg)
@@ -119,35 +114,15 b' class RichIPythonWidget(IPythonWidget):'
119 # 'RichIPythonWidget' protected interface
114 # 'RichIPythonWidget' protected interface
120 #---------------------------------------------------------------------------
115 #---------------------------------------------------------------------------
121
116
122 def _append_svg(self, svg):
117 def _append_png(self, png, before_prompt=False):
123 """ Append raw svg data to the widget.
118 """ Append raw PNG data to the widget.
124 """
119 """
125 try:
120 self._append_custom(self._insert_png, png, before_prompt)
126 image = svg_to_image(svg)
127 except ValueError:
128 self._append_plain_text('Received invalid plot data.')
129 else:
130 format = self._add_image(image)
131 self._name_to_svg_map[format.name()] = svg
132 cursor = self._get_end_cursor()
133 cursor.insertBlock()
134 cursor.insertImage(format)
135 cursor.insertBlock()
136
121
137 def _append_png(self, png):
122 def _append_svg(self, svg, before_prompt=False):
138 """ Append raw svg data to the widget.
123 """ Append raw SVG data to the widget.
139 """
124 """
140 try:
125 self._append_custom(self._insert_svg, svg, before_prompt)
141 image = QtGui.QImage()
142 image.loadFromData(png, 'PNG')
143 except ValueError:
144 self._append_plain_text('Received invalid plot data.')
145 else:
146 format = self._add_image(image)
147 cursor = self._get_end_cursor()
148 cursor.insertBlock()
149 cursor.insertImage(format)
150 cursor.insertBlock()
151
126
152 def _add_image(self, image):
127 def _add_image(self, image):
153 """ Adds the specified QImage to the document and returns a
128 """ Adds the specified QImage to the document and returns a
@@ -236,6 +211,34 b' class RichIPythonWidget(IPythonWidget):'
236 else:
211 else:
237 return '<b>Unrecognized image format</b>'
212 return '<b>Unrecognized image format</b>'
238
213
214 def _insert_png(self, cursor, png):
215 """ Insert raw PNG data into the widget.
216 """
217 try:
218 image = QtGui.QImage()
219 image.loadFromData(png, 'PNG')
220 except ValueError:
221 self._insert_plain_text(cursor, 'Received invalid PNG data.')
222 else:
223 format = self._add_image(image)
224 cursor.insertBlock()
225 cursor.insertImage(format)
226 cursor.insertBlock()
227
228 def _insert_svg(self, cursor, svg):
229 """ Insert raw SVG data into the widet.
230 """
231 try:
232 image = svg_to_image(svg)
233 except ValueError:
234 self._insert_plain_text(cursor, 'Received invalid SVG data.')
235 else:
236 format = self._add_image(image)
237 self._name_to_svg_map[format.name()] = svg
238 cursor.insertBlock()
239 cursor.insertImage(format)
240 cursor.insertBlock()
241
239 def _save_image(self, name, format='PNG'):
242 def _save_image(self, name, format='PNG'):
240 """ Shows a save dialog for the ImageResource with 'name'.
243 """ Shows a save dialog for the ImageResource with 'name'.
241 """
244 """
General Comments 0
You need to be logged in to leave comments. Login now