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