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