##// END OF EJS Templates
Support echoing output from other clients...
MinRK -
Show More
@@ -26,7 +26,7 b' from IPython.core import release'
26 from IPython.utils.warn import warn, error
26 from IPython.utils.warn import warn, error
27 from IPython.utils import io
27 from IPython.utils import io
28 from IPython.utils.py3compat import string_types, input
28 from IPython.utils.py3compat import string_types, input
29 from IPython.utils.traitlets import List, Enum, Any, Instance, Unicode, Float
29 from IPython.utils.traitlets import List, Enum, Any, Instance, Unicode, Float, Bool
30 from IPython.utils.tempdir import NamedFileInTemporaryDirectory
30 from IPython.utils.tempdir import NamedFileInTemporaryDirectory
31
31
32 from IPython.terminal.interactiveshell import TerminalInteractiveShell
32 from IPython.terminal.interactiveshell import TerminalInteractiveShell
@@ -212,8 +212,37 b' class ZMQTerminalInteractiveShell(TerminalInteractiveShell):'
212 print(frame, file=io.stderr)
212 print(frame, file=io.stderr)
213
213
214 self.execution_count = int(content["execution_count"] + 1)
214 self.execution_count = int(content["execution_count"] + 1)
215
215
216
216 include_other_output = Bool(False, config=True,
217 help="""Whether to include output from clients
218 other than this one sharing the same kernel.
219
220 Outputs are not displayed until enter is pressed.
221 """
222 )
223 other_output_prefix = Unicode("[remote] ", config=True,
224 help="""Prefix to add to outputs coming from clients other than this one.
225
226 Only relevant if include_other_output is True.
227 """
228 )
229
230 def from_here(self, msg):
231 """Return whether a message is from this session"""
232 return msg['parent_header'].get("session", self.session_id) == self.session_id
233
234 def include_output(self, msg):
235 """Return whether we should include a given output message"""
236 from_here = self.from_here(msg)
237 if msg['msg_type'] == 'execute_input':
238 # only echo inputs not from here
239 return self.include_other_output and not from_here
240
241 if self.include_other_output:
242 return True
243 else:
244 return from_here
245
217 def handle_iopub(self, msg_id=''):
246 def handle_iopub(self, msg_id=''):
218 """Process messages on the IOPub channel
247 """Process messages on the IOPub channel
219
248
@@ -227,7 +256,7 b' class ZMQTerminalInteractiveShell(TerminalInteractiveShell):'
227 msg_type = sub_msg['header']['msg_type']
256 msg_type = sub_msg['header']['msg_type']
228 parent = sub_msg["parent_header"]
257 parent = sub_msg["parent_header"]
229
258
230 if parent.get("session", self.session_id) == self.session_id:
259 if self.include_output(sub_msg):
231 if msg_type == 'status':
260 if msg_type == 'status':
232 self._execution_state = sub_msg["content"]["execution_state"]
261 self._execution_state = sub_msg["content"]["execution_state"]
233 elif msg_type == 'stream':
262 elif msg_type == 'stream':
@@ -249,8 +278,11 b' class ZMQTerminalInteractiveShell(TerminalInteractiveShell):'
249 print("\r", file=io.stdout, end="")
278 print("\r", file=io.stdout, end="")
250 self._pending_clearoutput = False
279 self._pending_clearoutput = False
251 self.execution_count = int(sub_msg["content"]["execution_count"])
280 self.execution_count = int(sub_msg["content"]["execution_count"])
281 if not self.from_here(sub_msg):
282 sys.stdout.write(self.other_output_prefix)
252 format_dict = sub_msg["content"]["data"]
283 format_dict = sub_msg["content"]["data"]
253 self.handle_rich_data(format_dict)
284 self.handle_rich_data(format_dict)
285
254 # taken from DisplayHook.__call__:
286 # taken from DisplayHook.__call__:
255 hook = self.displayhook
287 hook = self.displayhook
256 hook.start_displayhook()
288 hook.start_displayhook()
@@ -263,10 +295,20 b' class ZMQTerminalInteractiveShell(TerminalInteractiveShell):'
263 data = sub_msg["content"]["data"]
295 data = sub_msg["content"]["data"]
264 handled = self.handle_rich_data(data)
296 handled = self.handle_rich_data(data)
265 if not handled:
297 if not handled:
298 if not self.from_here(sub_msg):
299 sys.stdout.write(self.other_output_prefix)
266 # if it was an image, we handled it by now
300 # if it was an image, we handled it by now
267 if 'text/plain' in data:
301 if 'text/plain' in data:
268 print(data['text/plain'])
302 print(data['text/plain'])
269
303
304 elif msg_type == 'execute_input':
305 content = sub_msg['content']
306 self.execution_count = content['execution_count']
307 if not self.from_here(sub_msg):
308 sys.stdout.write(self.other_output_prefix)
309 sys.stdout.write(self.prompt_manager.render('in'))
310 sys.stdout.write(content['code'])
311
270 elif msg_type == 'clear_output':
312 elif msg_type == 'clear_output':
271 if sub_msg["content"]["wait"]:
313 if sub_msg["content"]["wait"]:
272 self._pending_clearoutput = True
314 self._pending_clearoutput = True
General Comments 0
You need to be logged in to leave comments. Login now