##// END OF EJS Templates
First working draft of new payload system.
Brian Granger -
Show More
@@ -1901,6 +1901,9 b' class InteractiveShell(Configurable, Magic):'
1901 - 1: an error occurred.
1901 - 1: an error occurred.
1902 """
1902 """
1903
1903
1904 # Clear the payload before executing new code.
1905 self.payload_manager.clear_payload()
1906
1904 # Set our own excepthook in case the user code tries to call it
1907 # Set our own excepthook in case the user code tries to call it
1905 # directly, so that the IPython crash handler doesn't get triggered
1908 # directly, so that the IPython crash handler doesn't get triggered
1906 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1909 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
@@ -32,10 +32,10 b' class PayloadManager(Configurable):'
32 def write_payload(self, data):
32 def write_payload(self, data):
33 if not isinstance(data, dict):
33 if not isinstance(data, dict):
34 raise TypeError('Each payload write must be a dict, got: %r' % data)
34 raise TypeError('Each payload write must be a dict, got: %r' % data)
35 self.payload.append(data)
35 self._payload.append(data)
36
37 def reset_payload(self):
38 self.payload = []
39
36
40 def read_payload(self):
37 def read_payload(self):
41 return self._payload
38 return self._payload
39
40 def clear_payload(self):
41 self._payload = []
@@ -60,20 +60,28 b' class RichIPythonWidget(IPythonWidget):'
60 """ Reimplemented to handle matplotlib plot payloads.
60 """ Reimplemented to handle matplotlib plot payloads.
61 """
61 """
62 payload = msg['content']['payload']
62 payload = msg['content']['payload']
63 plot_payload = payload.get('plot', None)
63 if payload:
64 if plot_payload and plot_payload['format'] == 'svg':
64 for item in payload:
65 svg = plot_payload['data']
65 if item['type'] == 'plot':
66 try:
66 if item['format'] == 'svg':
67 image = svg_to_image(svg)
67 svg = item['data']
68 except ValueError:
68 try:
69 self._append_plain_text('Received invalid plot data.')
69 image = svg_to_image(svg)
70 else:
70 except ValueError:
71 format = self._add_image(image)
71 self._append_plain_text('Received invalid plot data.')
72 format.setProperty(self._svg_text_format_property, svg)
72 else:
73 cursor = self._get_end_cursor()
73 format = self._add_image(image)
74 cursor.insertBlock()
74 format.setProperty(self._svg_text_format_property, svg)
75 cursor.insertImage(format)
75 cursor = self._get_end_cursor()
76 cursor.insertBlock()
76 cursor.insertBlock()
77 cursor.insertImage(format)
78 cursor.insertBlock()
79 else:
80 # Add other plot formats here!
81 pass
82 else:
83 # Add other payload types here!
84 pass
77 else:
85 else:
78 super(RichIPythonWidget, self)._process_execute_ok(msg)
86 super(RichIPythonWidget, self)._process_execute_ok(msg)
79
87
@@ -49,9 +49,6 b' class Kernel(Configurable):'
49 pub_socket = Instance('zmq.Socket')
49 pub_socket = Instance('zmq.Socket')
50 req_socket = Instance('zmq.Socket')
50 req_socket = Instance('zmq.Socket')
51
51
52 # The global kernel instance.
53 _kernel = None
54
55 # Maps user-friendly backend names to matplotlib backend identifiers.
52 # Maps user-friendly backend names to matplotlib backend identifiers.
56 _pylab_map = { 'tk': 'TkAgg',
53 _pylab_map = { 'tk': 'TkAgg',
57 'gtk': 'GTKAgg',
54 'gtk': 'GTKAgg',
@@ -69,9 +66,6 b' class Kernel(Configurable):'
69 self.shell.displayhook.session = self.session
66 self.shell.displayhook.session = self.session
70 self.shell.displayhook.pub_socket = self.pub_socket
67 self.shell.displayhook.pub_socket = self.pub_socket
71
68
72 # Protected variables.
73 self._exec_payload = {}
74
75 # Build dict of handlers for message types
69 # Build dict of handlers for message types
76 msg_types = [ 'execute_request', 'complete_request',
70 msg_types = [ 'execute_request', 'complete_request',
77 'object_info_request', 'prompt_request',
71 'object_info_request', 'prompt_request',
@@ -80,11 +74,6 b' class Kernel(Configurable):'
80 for msg_type in msg_types:
74 for msg_type in msg_types:
81 self.handlers[msg_type] = getattr(self, msg_type)
75 self.handlers[msg_type] = getattr(self, msg_type)
82
76
83 def add_exec_payload(self, key, value):
84 """ Adds a key/value pair to the execute payload.
85 """
86 self._exec_payload[key] = value
87
88 def activate_pylab(self, backend=None, import_all=True):
77 def activate_pylab(self, backend=None, import_all=True):
89 """ Activates pylab in this kernel's namespace.
78 """ Activates pylab in this kernel's namespace.
90
79
@@ -126,21 +115,9 b' class Kernel(Configurable):'
126
115
127 matplotlib.interactive(True)
116 matplotlib.interactive(True)
128
117
129 @classmethod
130 def get_kernel(cls):
131 """ Return the global kernel instance or raise a RuntimeError if it does
132 not exist.
133 """
134 if cls._kernel is None:
135 raise RuntimeError("Kernel not started!")
136 else:
137 return cls._kernel
138
139 def start(self):
118 def start(self):
140 """ Start the kernel main loop.
119 """ Start the kernel main loop.
141 """
120 """
142 # Set the global kernel instance.
143 self.__class__._kernel = self
144
121
145 while True:
122 while True:
146 ident = self.reply_socket.recv()
123 ident = self.reply_socket.recv()
@@ -169,9 +146,6 b' class Kernel(Configurable):'
169 pyin_msg = self.session.msg(u'pyin',{u'code':code}, parent=parent)
146 pyin_msg = self.session.msg(u'pyin',{u'code':code}, parent=parent)
170 self.pub_socket.send_json(pyin_msg)
147 self.pub_socket.send_json(pyin_msg)
171
148
172 # Clear the execute payload from the last request.
173 self._exec_payload = {}
174
175 try:
149 try:
176 # Replace raw_input. Note that is not sufficient to replace
150 # Replace raw_input. Note that is not sufficient to replace
177 # raw_input in the user namespace.
151 # raw_input in the user namespace.
@@ -182,7 +156,6 b' class Kernel(Configurable):'
182 self.shell.displayhook.set_parent(parent)
156 self.shell.displayhook.set_parent(parent)
183
157
184 self.shell.runlines(code)
158 self.shell.runlines(code)
185 # exec comp_code in self.user_ns, self.user_ns
186 except:
159 except:
187 etype, evalue, tb = sys.exc_info()
160 etype, evalue, tb = sys.exc_info()
188 tb = traceback.format_exception(etype, evalue, tb)
161 tb = traceback.format_exception(etype, evalue, tb)
@@ -196,7 +169,11 b' class Kernel(Configurable):'
196 self.pub_socket.send_json(exc_msg)
169 self.pub_socket.send_json(exc_msg)
197 reply_content = exc_content
170 reply_content = exc_content
198 else:
171 else:
199 reply_content = { 'status' : 'ok', 'payload' : self._exec_payload }
172 payload = self.shell.payload_manager.read_payload()
173 # Be agressive about clearing the payload because we don't want
174 # it to sit in memory until the next execute_request comes in.
175 self.shell.payload_manager.clear_payload()
176 reply_content = { 'status' : 'ok', 'payload' : payload }
200
177
201 # Compute the prompt information
178 # Compute the prompt information
202 prompt_number = self.shell.displayhook.prompt_count
179 prompt_number = self.shell.displayhook.prompt_count
@@ -2,7 +2,7 b''
2 """
2 """
3
3
4 # Local imports.
4 # Local imports.
5 from IPython.zmq.ipkernel import Kernel
5 from IPython.core.interactiveshell import InteractiveShell
6
6
7
7
8 def add_plot_payload(format, data, metadata={}):
8 def add_plot_payload(format, data, metadata={}):
@@ -19,5 +19,5 b' def add_plot_payload(format, data, metadata={}):'
19 metadata : dict, optional [default empty]
19 metadata : dict, optional [default empty]
20 Allows for specification of additional information about the plot data.
20 Allows for specification of additional information about the plot data.
21 """
21 """
22 payload = dict(format=format, data=data, metadata=metadata)
22 payload = dict(type='plot', format=format, data=data, metadata=metadata)
23 Kernel.get_kernel().add_exec_payload('plot', payload)
23 InteractiveShell.instance().payload_manager.write_payload(payload)
General Comments 0
You need to be logged in to leave comments. Login now