##// END OF EJS Templates
Merge pull request #7560 from minrk/no-displayhook-no-data...
Thomas Kluyver -
r20116:b82833eb merge
parent child Browse files
Show More
@@ -1,73 +1,74 b''
1 """Replacements for sys.displayhook that publish over ZMQ."""
1 """Replacements for sys.displayhook that publish over ZMQ."""
2
2
3 # Copyright (c) IPython Development Team.
3 # Copyright (c) IPython Development Team.
4 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
5
5
6 import sys
6 import sys
7
7
8 from IPython.core.displayhook import DisplayHook
8 from IPython.core.displayhook import DisplayHook
9 from IPython.kernel.inprocess.socket import SocketABC
9 from IPython.kernel.inprocess.socket import SocketABC
10 from IPython.utils.jsonutil import encode_images
10 from IPython.utils.jsonutil import encode_images
11 from IPython.utils.py3compat import builtin_mod
11 from IPython.utils.py3compat import builtin_mod
12 from IPython.utils.traitlets import Instance, Dict
12 from IPython.utils.traitlets import Instance, Dict
13 from .session import extract_header, Session
13 from .session import extract_header, Session
14
14
15 class ZMQDisplayHook(object):
15 class ZMQDisplayHook(object):
16 """A simple displayhook that publishes the object's repr over a ZeroMQ
16 """A simple displayhook that publishes the object's repr over a ZeroMQ
17 socket."""
17 socket."""
18 topic=b'execute_result'
18 topic=b'execute_result'
19
19
20 def __init__(self, session, pub_socket):
20 def __init__(self, session, pub_socket):
21 self.session = session
21 self.session = session
22 self.pub_socket = pub_socket
22 self.pub_socket = pub_socket
23 self.parent_header = {}
23 self.parent_header = {}
24
24
25 def __call__(self, obj):
25 def __call__(self, obj):
26 if obj is None:
26 if obj is None:
27 return
27 return
28
28
29 builtin_mod._ = obj
29 builtin_mod._ = obj
30 sys.stdout.flush()
30 sys.stdout.flush()
31 sys.stderr.flush()
31 sys.stderr.flush()
32 msg = self.session.send(self.pub_socket, u'execute_result', {u'data':repr(obj)},
32 msg = self.session.send(self.pub_socket, u'execute_result', {u'data':repr(obj)},
33 parent=self.parent_header, ident=self.topic)
33 parent=self.parent_header, ident=self.topic)
34
34
35 def set_parent(self, parent):
35 def set_parent(self, parent):
36 self.parent_header = extract_header(parent)
36 self.parent_header = extract_header(parent)
37
37
38
38
39 class ZMQShellDisplayHook(DisplayHook):
39 class ZMQShellDisplayHook(DisplayHook):
40 """A displayhook subclass that publishes data using ZeroMQ. This is intended
40 """A displayhook subclass that publishes data using ZeroMQ. This is intended
41 to work with an InteractiveShell instance. It sends a dict of different
41 to work with an InteractiveShell instance. It sends a dict of different
42 representations of the object."""
42 representations of the object."""
43 topic=None
43 topic=None
44
44
45 session = Instance(Session)
45 session = Instance(Session)
46 pub_socket = Instance(SocketABC)
46 pub_socket = Instance(SocketABC)
47 parent_header = Dict({})
47 parent_header = Dict({})
48
48
49 def set_parent(self, parent):
49 def set_parent(self, parent):
50 """Set the parent for outbound messages."""
50 """Set the parent for outbound messages."""
51 self.parent_header = extract_header(parent)
51 self.parent_header = extract_header(parent)
52
52
53 def start_displayhook(self):
53 def start_displayhook(self):
54 self.msg = self.session.msg(u'execute_result', {
54 self.msg = self.session.msg(u'execute_result', {
55 'data': {},
55 'data': {},
56 'metadata': {},
56 'metadata': {},
57 }, parent=self.parent_header)
57 }, parent=self.parent_header)
58
58
59 def write_output_prompt(self):
59 def write_output_prompt(self):
60 """Write the output prompt."""
60 """Write the output prompt."""
61 self.msg['content']['execution_count'] = self.prompt_count
61 self.msg['content']['execution_count'] = self.prompt_count
62
62
63 def write_format_data(self, format_dict, md_dict=None):
63 def write_format_data(self, format_dict, md_dict=None):
64 self.msg['content']['data'] = encode_images(format_dict)
64 self.msg['content']['data'] = encode_images(format_dict)
65 self.msg['content']['metadata'] = md_dict
65 self.msg['content']['metadata'] = md_dict
66
66
67 def finish_displayhook(self):
67 def finish_displayhook(self):
68 """Finish up all displayhook activities."""
68 """Finish up all displayhook activities."""
69 sys.stdout.flush()
69 sys.stdout.flush()
70 sys.stderr.flush()
70 sys.stderr.flush()
71 self.session.send(self.pub_socket, self.msg, ident=self.topic)
71 if self.msg['content']['data']:
72 self.session.send(self.pub_socket, self.msg, ident=self.topic)
72 self.msg = None
73 self.msg = None
73
74
General Comments 0
You need to be logged in to leave comments. Login now