##// END OF EJS Templates
Add image message handler in ZMQTerminalInteractiveShell...
Takafumi Arakaki -
Show More
@@ -21,13 +21,21 b' import bdb'
21 import signal
21 import signal
22 import sys
22 import sys
23 import time
23 import time
24 from cStringIO import StringIO
25 import base64
24
26
25 from Queue import Empty
27 from Queue import Empty
26
28
29 try:
30 import PIL
31 except ImportError:
32 PIL = None
33
27 from IPython.core.alias import AliasManager, AliasError
34 from IPython.core.alias import AliasManager, AliasError
28 from IPython.core import page
35 from IPython.core import page
29 from IPython.utils.warn import warn, error, fatal
36 from IPython.utils.warn import warn, error, fatal
30 from IPython.utils import io
37 from IPython.utils import io
38 from IPython.utils.traitlets import List
31
39
32 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
40 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
33 from IPython.frontend.terminal.console.completer import ZMQCompleter
41 from IPython.frontend.terminal.console.completer import ZMQCompleter
@@ -36,7 +44,18 b' from IPython.frontend.terminal.console.completer import ZMQCompleter'
36 class ZMQTerminalInteractiveShell(TerminalInteractiveShell):
44 class ZMQTerminalInteractiveShell(TerminalInteractiveShell):
37 """A subclass of TerminalInteractiveShell that uses the 0MQ kernel"""
45 """A subclass of TerminalInteractiveShell that uses the 0MQ kernel"""
38 _executing = False
46 _executing = False
39
47
48 image_handler = List([], allow_none=False, config=True, help=
49 """
50 Handlers for image type output. This is useful, for example,
51 when connecting to the kernel in which pylab inline backend is
52 activated. Handlers in the list is tried one-by-one and first
53 available handler is used. Currently IPython only supports
54 handler `PIL`. IPython shell pops up window to show image
55 when the kernel sends image (e.g., when you plot a graph).
56 """
57 )
58
40 def __init__(self, *args, **kwargs):
59 def __init__(self, *args, **kwargs):
41 self.km = kwargs.pop('kernel_manager')
60 self.km = kwargs.pop('kernel_manager')
42 self.session_id = self.km.session.session
61 self.session_id = self.km.session.session
@@ -163,6 +182,7 b' class ZMQTerminalInteractiveShell(TerminalInteractiveShell):'
163 elif msg_type == 'pyout':
182 elif msg_type == 'pyout':
164 self.execution_count = int(sub_msg["content"]["execution_count"])
183 self.execution_count = int(sub_msg["content"]["execution_count"])
165 format_dict = sub_msg["content"]["data"]
184 format_dict = sub_msg["content"]["data"]
185 self.handle_rich_data(format_dict)
166 # taken from DisplayHook.__call__:
186 # taken from DisplayHook.__call__:
167 hook = self.displayhook
187 hook = self.displayhook
168 hook.start_displayhook()
188 hook.start_displayhook()
@@ -171,6 +191,21 b' class ZMQTerminalInteractiveShell(TerminalInteractiveShell):'
171 hook.log_output(format_dict)
191 hook.log_output(format_dict)
172 hook.finish_displayhook()
192 hook.finish_displayhook()
173
193
194 elif msg_type == 'display_data':
195 self.handle_rich_data(sub_msg["content"]["data"])
196
197 def handle_rich_data(self, data):
198 if 'image/png' in data:
199 self.handle_image(data['image/png'])
200 elif 'image/jpeg' in data:
201 self.handle_image(data['image/jpeg'])
202
203 def handle_image(self, string):
204 if 'PIL' in self.image_handler and PIL:
205 data = base64.decodestring(string)
206 img = PIL.Image.open(StringIO(data))
207 img.show()
208
174 def handle_stdin_request(self, timeout=0.1):
209 def handle_stdin_request(self, timeout=0.1):
175 """ Method to capture raw_input
210 """ Method to capture raw_input
176 """
211 """
General Comments 0
You need to be logged in to leave comments. Login now