##// END OF EJS Templates
Support passing image format to external program...
Takafumi Arakaki -
Show More
@@ -75,8 +75,9 b' class ZMQTerminalInteractiveShell(TerminalInteractiveShell):'
75 Command to invoke an image viewer program when you are using
75 Command to invoke an image viewer program when you are using
76 'tempfile' image handler. This option is a list of string
76 'tempfile' image handler. This option is a list of string
77 where the first element is the command itself and reminders
77 where the first element is the command itself and reminders
78 are the options for the command. You can use {file} in the
78 are the options for the command. You can use {file} and
79 string to represent the location of the generated image file.
79 {format} in the string to represent the location of the
80 generated image file and image format.
80 """
81 """
81 )
82 )
82
83
@@ -85,7 +86,8 b' class ZMQTerminalInteractiveShell(TerminalInteractiveShell):'
85 Callable object called via 'callable' image handler with one
86 Callable object called via 'callable' image handler with one
86 argument, `data`, which is `msg["content"]["data"]` where
87 argument, `data`, which is `msg["content"]["data"]` where
87 `msg` is the message from iopub channel. For exmaple, you can
88 `msg` is the message from iopub channel. For exmaple, you can
88 find base64 encoded PNG data as `data['image/png']`.
89 find base64 encoded PNG data as `data['image/png']`. You can
90 use {format} in the string to represent the image format.
89 """
91 """
90 )
92 )
91
93
@@ -227,6 +229,12 b' class ZMQTerminalInteractiveShell(TerminalInteractiveShell):'
227 elif msg_type == 'display_data':
229 elif msg_type == 'display_data':
228 self.handle_rich_data(sub_msg["content"]["data"])
230 self.handle_rich_data(sub_msg["content"]["data"])
229
231
232 _imagemime = {
233 'image/png': 'png',
234 'image/jpeg': 'jpeg',
235 'image/svg+xml': 'svg',
236 }
237
230 def handle_rich_data(self, data):
238 def handle_rich_data(self, data):
231 for mime in ['image/png', 'image/jpeg', 'image/svg+xml']:
239 for mime in ['image/png', 'image/jpeg', 'image/svg+xml']:
232 if mime in data:
240 if mime in data:
@@ -249,17 +257,22 b' class ZMQTerminalInteractiveShell(TerminalInteractiveShell):'
249
257
250 def handle_image_stream(self, data, mime):
258 def handle_image_stream(self, data, mime):
251 raw = base64.decodestring(data[mime])
259 raw = base64.decodestring(data[mime])
260 imageformat = self._imagemime[mime]
261 fmt = dict(format=imageformat)
262 args = [s.format(**fmt) for s in self.stream_image_handler]
252 proc = subprocess.Popen(
263 proc = subprocess.Popen(
253 self.stream_image_handler, stdin=subprocess.PIPE,
264 args, stdin=subprocess.PIPE,
254 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
265 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
255 proc.communicate(raw)
266 proc.communicate(raw)
256
267
257 def handle_image_tempfile(self, data, mime):
268 def handle_image_tempfile(self, data, mime):
258 raw = base64.decodestring(data[mime])
269 raw = base64.decodestring(data[mime])
259 with tempfile.NamedTemporaryFile() as f:
270 imageformat = self._imagemime[mime]
271 ext = '.{0}'.format(imageformat)
272 with tempfile.NamedTemporaryFile(suffix=ext) as f:
260 f.write(raw)
273 f.write(raw)
261 f.flush()
274 f.flush()
262 fmt = dict(file=f.name)
275 fmt = dict(file=f.name, format=imageformat)
263 args = [s.format(**fmt) for s in self.tempfile_image_handler]
276 args = [s.format(**fmt) for s in self.tempfile_image_handler]
264 subprocess.Popen(
277 subprocess.Popen(
265 args, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
278 args, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
General Comments 0
You need to be logged in to leave comments. Login now