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