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

Skip to content

Commit 3b0d8c0

Browse files
committed
add ConsoleWidget.include_other_output
matches zmq console. Doesn't include `[remote] ` prefix, because true async output makes it less important.
1 parent fbda549 commit 3b0d8c0

5 files changed

Lines changed: 45 additions & 19 deletions

File tree

IPython/qt/base_frontend_mixin.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,23 @@ def _dispatch(self, msg):
136136
handler = getattr(self, '_handle_' + msg_type, None)
137137
if handler:
138138
handler(msg)
139-
140-
def _is_from_this_session(self, msg):
141-
""" Returns whether a reply from the kernel originated from a request
142-
from this frontend.
143-
"""
144-
session = self._kernel_client.session.session
145-
parent = msg['parent_header']
146-
if not parent:
147-
# if the message has no parent, assume it is meant for all frontends
139+
140+
def from_here(self, msg):
141+
"""Return whether a message is from this session"""
142+
session_id = self._kernel_client.session.session
143+
return msg['parent_header'].get("session", session_id) == session_id
144+
145+
def include_output(self, msg):
146+
"""Return whether we should include a given output message"""
147+
if self._hidden:
148+
return False
149+
from_here = self.from_here(msg)
150+
if msg['msg_type'] == 'execute_input':
151+
# only echo inputs not from here
152+
return self.include_other_output and not from_here
153+
154+
if self.include_other_output:
148155
return True
149156
else:
150-
return parent.get('session') == session
157+
return from_here
158+

IPython/qt/console/console_widget.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,15 @@ def sizeHint(self):
519519
#---------------------------------------------------------------------------
520520
# 'ConsoleWidget' public interface
521521
#---------------------------------------------------------------------------
522-
522+
523+
include_other_output = Bool(False, config=True,
524+
help="""Whether to include output from clients
525+
other than this one sharing the same kernel.
526+
527+
Outputs are not displayed until enter is pressed.
528+
"""
529+
)
530+
523531
def can_copy(self):
524532
""" Returns whether text can be copied to the clipboard.
525533
"""

IPython/qt/console/frontend_widget.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ def _insert_continuation_prompt(self, cursor):
350350
#---------------------------------------------------------------------------
351351
def _handle_clear_output(self, msg):
352352
"""Handle clear output messages."""
353-
if not self._hidden and self._is_from_this_session(msg):
353+
if include_output(msg):
354354
wait = msg['content'].get('wait', True)
355355
if wait:
356356
self._pending_clearoutput = True
@@ -523,7 +523,7 @@ def _handle_execute_result(self, msg):
523523
""" Handle display hook output.
524524
"""
525525
self.log.debug("execute_result: %s", msg.get('content', ''))
526-
if not self._hidden and self._is_from_this_session(msg):
526+
if self.include_output(msg):
527527
self.flush_clearoutput()
528528
text = msg['content']['data']
529529
self._append_plain_text(text + '\n', before_prompt=True)
@@ -532,7 +532,7 @@ def _handle_stream(self, msg):
532532
""" Handle stdout, stderr, and stdin.
533533
"""
534534
self.log.debug("stream: %s", msg.get('content', ''))
535-
if not self._hidden and self._is_from_this_session(msg):
535+
if self.include_output(msg):
536536
self.flush_clearoutput()
537537
self.append_stream(msg['content']['text'])
538538

@@ -541,7 +541,7 @@ def _handle_shutdown_reply(self, msg):
541541
"""
542542
self.log.info("shutdown: %s", msg.get('content', ''))
543543
restart = msg.get('content', {}).get('restart', False)
544-
if not self._hidden and not self._is_from_this_session(msg):
544+
if not self._hidden and not self.from_here(msg):
545545
# got shutdown reply, request came from session other than ours
546546
if restart:
547547
# someone restarted the kernel, handle it

IPython/qt/console/ipython_widget.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,21 @@ def _handle_history_reply(self, msg):
220220
last_cell = cell
221221
self._set_history(items)
222222

223+
def _handle_execute_input(self, msg):
224+
"""Handle an execute_input message"""
225+
self.log.debug("execute_input: %s", msg.get('content', ''))
226+
if self.include_output(msg):
227+
content = msg['content']
228+
prompt_number = content.get('execution_count', 0)
229+
self._append_html(self._make_in_prompt(prompt_number), True)
230+
self._append_plain_text(content['code'], True)
231+
232+
223233
def _handle_execute_result(self, msg):
224234
""" Reimplemented for IPython-style "display hook".
225235
"""
226236
self.log.debug("execute_result: %s", msg.get('content', ''))
227-
if not self._hidden and self._is_from_this_session(msg):
237+
if self.include_output(msg):
228238
self.flush_clearoutput()
229239
content = msg['content']
230240
prompt_number = content.get('execution_count', 0)
@@ -246,7 +256,7 @@ def _handle_display_data(self, msg):
246256
# For now, we don't display data from other frontends, but we
247257
# eventually will as this allows all frontends to monitor the display
248258
# data. But we need to figure out how to handle this in the GUI.
249-
if not self._hidden and self._is_from_this_session(msg):
259+
if self.include_output(msg):
250260
self.flush_clearoutput()
251261
data = msg['content']['data']
252262
metadata = msg['content']['metadata']

IPython/qt/console/rich_ipython_widget.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def _pre_image_append(self, msg, prompt_number):
107107
def _handle_execute_result(self, msg):
108108
""" Overridden to handle rich data types, like SVG.
109109
"""
110-
if not self._hidden and self._is_from_this_session(msg):
110+
if self.include_output(msg):
111111
self.flush_clearoutput()
112112
content = msg['content']
113113
prompt_number = content.get('execution_count', 0)
@@ -146,7 +146,7 @@ def _handle_execute_result(self, msg):
146146
def _handle_display_data(self, msg):
147147
""" Overridden to handle rich data types, like SVG.
148148
"""
149-
if not self._hidden and self._is_from_this_session(msg):
149+
if self.include_output(msg):
150150
self.flush_clearoutput()
151151
data = msg['content']['data']
152152
metadata = msg['content']['metadata']

0 commit comments

Comments
 (0)