##// END OF EJS Templates
flush stdout/stderr on displayhook...
MinRK -
Show More
@@ -1,68 +1,73 b''
1 import __builtin__
1 import __builtin__
2 import sys
2 from base64 import encodestring
3 from base64 import encodestring
3
4
4 from IPython.core.displayhook import DisplayHook
5 from IPython.core.displayhook import DisplayHook
5 from IPython.utils.traitlets import Instance, Dict
6 from IPython.utils.traitlets import Instance, Dict
6 from session import extract_header, Session
7 from session import extract_header, Session
7
8
8 class ZMQDisplayHook(object):
9 class ZMQDisplayHook(object):
9 """A simple displayhook that publishes the object's repr over a ZeroMQ
10 """A simple displayhook that publishes the object's repr over a ZeroMQ
10 socket."""
11 socket."""
11 topic=None
12 topic=None
12
13
13 def __init__(self, session, pub_socket):
14 def __init__(self, session, pub_socket):
14 self.session = session
15 self.session = session
15 self.pub_socket = pub_socket
16 self.pub_socket = pub_socket
16 self.parent_header = {}
17 self.parent_header = {}
17
18
18 def __call__(self, obj):
19 def __call__(self, obj):
19 if obj is None:
20 if obj is None:
20 return
21 return
21
22
22 __builtin__._ = obj
23 __builtin__._ = obj
24 sys.stdout.flush()
25 sys.stderr.flush()
23 msg = self.session.send(self.pub_socket, u'pyout', {u'data':repr(obj)},
26 msg = self.session.send(self.pub_socket, u'pyout', {u'data':repr(obj)},
24 parent=self.parent_header, ident=self.topic)
27 parent=self.parent_header, ident=self.topic)
25
28
26 def set_parent(self, parent):
29 def set_parent(self, parent):
27 self.parent_header = extract_header(parent)
30 self.parent_header = extract_header(parent)
28
31
29
32
30 def _encode_binary(format_dict):
33 def _encode_binary(format_dict):
31 pngdata = format_dict.get('image/png')
34 pngdata = format_dict.get('image/png')
32 if pngdata is not None:
35 if pngdata is not None:
33 format_dict['image/png'] = encodestring(pngdata).decode('ascii')
36 format_dict['image/png'] = encodestring(pngdata).decode('ascii')
34 jpegdata = format_dict.get('image/jpeg')
37 jpegdata = format_dict.get('image/jpeg')
35 if jpegdata is not None:
38 if jpegdata is not None:
36 format_dict['image/jpeg'] = encodestring(jpegdata).decode('ascii')
39 format_dict['image/jpeg'] = encodestring(jpegdata).decode('ascii')
37
40
38
41
39 class ZMQShellDisplayHook(DisplayHook):
42 class ZMQShellDisplayHook(DisplayHook):
40 """A displayhook subclass that publishes data using ZeroMQ. This is intended
43 """A displayhook subclass that publishes data using ZeroMQ. This is intended
41 to work with an InteractiveShell instance. It sends a dict of different
44 to work with an InteractiveShell instance. It sends a dict of different
42 representations of the object."""
45 representations of the object."""
43
46
44 session = Instance(Session)
47 session = Instance(Session)
45 pub_socket = Instance('zmq.Socket')
48 pub_socket = Instance('zmq.Socket')
46 parent_header = Dict({})
49 parent_header = Dict({})
47
50
48 def set_parent(self, parent):
51 def set_parent(self, parent):
49 """Set the parent for outbound messages."""
52 """Set the parent for outbound messages."""
50 self.parent_header = extract_header(parent)
53 self.parent_header = extract_header(parent)
51
54
52 def start_displayhook(self):
55 def start_displayhook(self):
53 self.msg = self.session.msg(u'pyout', {}, parent=self.parent_header)
56 self.msg = self.session.msg(u'pyout', {}, parent=self.parent_header)
54
57
55 def write_output_prompt(self):
58 def write_output_prompt(self):
56 """Write the output prompt."""
59 """Write the output prompt."""
57 if self.do_full_cache:
60 if self.do_full_cache:
58 self.msg['content']['execution_count'] = self.prompt_count
61 self.msg['content']['execution_count'] = self.prompt_count
59
62
60 def write_format_data(self, format_dict):
63 def write_format_data(self, format_dict):
61 _encode_binary(format_dict)
64 _encode_binary(format_dict)
62 self.msg['content']['data'] = format_dict
65 self.msg['content']['data'] = format_dict
63
66
64 def finish_displayhook(self):
67 def finish_displayhook(self):
65 """Finish up all displayhook activities."""
68 """Finish up all displayhook activities."""
69 sys.stdout.flush()
70 sys.stderr.flush()
66 self.session.send(self.pub_socket, self.msg)
71 self.session.send(self.pub_socket, self.msg)
67 self.msg = None
72 self.msg = None
68
73
General Comments 0
You need to be logged in to leave comments. Login now