Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Support echoing output from other clients
in the zmq console

Can result in outputs like:

```
In [1]: 1
[remote]In [1]: 1
[remote]Out[1]: 1
[remote]In [2]: 3
[remote]Out[2]: 3
Out[3]: 1
```

Note that there will be inconsistencies in prompt numbers
because some execution may take place after the in-prompt is drawn.
  • Loading branch information
minrk committed Oct 16, 2014
commit fbda54992de313373388a28200ec7cf7aac3685b
52 changes: 47 additions & 5 deletions IPython/terminal/console/interactiveshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from IPython.utils.warn import warn, error
from IPython.utils import io
from IPython.utils.py3compat import string_types, input
from IPython.utils.traitlets import List, Enum, Any, Instance, Unicode, Float
from IPython.utils.traitlets import List, Enum, Any, Instance, Unicode, Float, Bool
from IPython.utils.tempdir import NamedFileInTemporaryDirectory

from IPython.terminal.interactiveshell import TerminalInteractiveShell
Expand Down Expand Up @@ -212,8 +212,37 @@ def handle_execute_reply(self, msg_id, timeout=None):
print(frame, file=io.stderr)

self.execution_count = int(content["execution_count"] + 1)



include_other_output = Bool(False, config=True,
help="""Whether to include output from clients
other than this one sharing the same kernel.

Outputs are not displayed until enter is pressed.
"""
)
other_output_prefix = Unicode("[remote] ", config=True,
help="""Prefix to add to outputs coming from clients other than this one.

Only relevant if include_other_output is True.
"""
)

def from_here(self, msg):
"""Return whether a message is from this session"""
return msg['parent_header'].get("session", self.session_id) == self.session_id

def include_output(self, msg):
"""Return whether we should include a given output message"""
from_here = self.from_here(msg)
if msg['msg_type'] == 'execute_input':
# only echo inputs not from here
return self.include_other_output and not from_here

if self.include_other_output:
return True
else:
return from_here
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name of function is a little weird if it's other input too, but that fine I guess. Also isn't the last equivalent to return self.include_other_output and from_here ? If so it seem it could be simplified with the previous block. Handwaving i would say if (include_other and (not from_here or 'execute_input')), but maybe it becomes hard to read.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had also looked at this block, and seen that it could be return self.include_other_output or from_here, but I don't think that's any clearer than spelling it out as Min has done.

Therefore, merging.


def handle_iopub(self, msg_id=''):
"""Process messages on the IOPub channel

Expand All @@ -227,7 +256,7 @@ def handle_iopub(self, msg_id=''):
msg_type = sub_msg['header']['msg_type']
parent = sub_msg["parent_header"]

if parent.get("session", self.session_id) == self.session_id:
if self.include_output(sub_msg):
if msg_type == 'status':
self._execution_state = sub_msg["content"]["execution_state"]
elif msg_type == 'stream':
Expand All @@ -249,8 +278,11 @@ def handle_iopub(self, msg_id=''):
print("\r", file=io.stdout, end="")
self._pending_clearoutput = False
self.execution_count = int(sub_msg["content"]["execution_count"])
if not self.from_here(sub_msg):
sys.stdout.write(self.other_output_prefix)
format_dict = sub_msg["content"]["data"]
self.handle_rich_data(format_dict)

# taken from DisplayHook.__call__:
hook = self.displayhook
hook.start_displayhook()
Expand All @@ -263,10 +295,20 @@ def handle_iopub(self, msg_id=''):
data = sub_msg["content"]["data"]
handled = self.handle_rich_data(data)
if not handled:
if not self.from_here(sub_msg):
sys.stdout.write(self.other_output_prefix)
# if it was an image, we handled it by now
if 'text/plain' in data:
print(data['text/plain'])


elif msg_type == 'execute_input':
content = sub_msg['content']
self.execution_count = content['execution_count']
if not self.from_here(sub_msg):
sys.stdout.write(self.other_output_prefix)
sys.stdout.write(self.prompt_manager.render('in'))
sys.stdout.write(content['code'])

elif msg_type == 'clear_output':
if sub_msg["content"]["wait"]:
self._pending_clearoutput = True
Expand Down