##// END OF EJS Templates
add IPython.zmq.datapub
MinRK -
Show More
@@ -0,0 +1,71 b''
1 """Publishing
2 """
3
4 #-----------------------------------------------------------------------------
5 # Copyright (C) 2012 The IPython Development Team
6 #
7 # Distributed under the terms of the BSD License. The full license is in
8 # the file COPYING, distributed as part of this software.
9 #-----------------------------------------------------------------------------
10
11 #-----------------------------------------------------------------------------
12 # Imports
13 #-----------------------------------------------------------------------------
14
15 from IPython.config import Configurable
16
17 from IPython.utils.jsonutil import json_clean
18 from IPython.utils.traitlets import Instance, Dict, CBytes
19
20 from IPython.zmq.serialize import serialize_object
21 from IPython.zmq.session import Session, extract_header
22
23 #-----------------------------------------------------------------------------
24 # Code
25 #-----------------------------------------------------------------------------
26
27
28 class ZMQDataPublisher(Configurable):
29
30 topic = topic = CBytes(b'datapub')
31 session = Instance(Session)
32 pub_socket = Instance('zmq.Socket')
33 parent_header = Dict({})
34
35 def set_parent(self, parent):
36 """Set the parent for outbound messages."""
37 self.parent_header = extract_header(parent)
38
39 def publish_data(self, data):
40 """publish a data_message on the IOPub channel
41
42 Parameters
43 ----------
44
45 data : dict
46 The data to be published. Think of it as a namespace.
47 """
48 session = self.session
49 buffers = serialize_object(data,
50 buffer_threshold=session.buffer_threshold,
51 item_threshold=session.item_threshold,
52 )
53 content = json_clean(dict(keys=data.keys()))
54 session.send(self.pub_socket, 'data_message', content=content,
55 parent=self.parent_header,
56 buffers=buffers,
57 ident=self.topic,
58 )
59
60
61 def publish_data(data):
62 """publish a data_message on the IOPub channel
63
64 Parameters
65 ----------
66
67 data : dict
68 The data to be published. Think of it as a namespace.
69 """
70 from IPython.zmq.zmqshell import ZMQInteractiveShell
71 ZMQInteractiveShell.instance().data_pub.publish_data(data)
General Comments 0
You need to be logged in to leave comments. Login now