##// END OF EJS Templates
Merge pull request #5008 from ivanov/messaging-doc...
Thomas Kluyver -
r15017:b3bfc778 merge
parent child Browse files
Show More
@@ -1,1171 +1,1170 b''
1 .. _messaging:
1 .. _messaging:
2
2
3 ======================
3 ======================
4 Messaging in IPython
4 Messaging in IPython
5 ======================
5 ======================
6
6
7
7
8 Versioning
8 Versioning
9 ==========
9 ==========
10
10
11 The IPython message specification is versioned independently of IPython.
11 The IPython message specification is versioned independently of IPython.
12 The current version of the specification is 4.1.
12 The current version of the specification is 4.1.
13
13
14
14
15 Introduction
15 Introduction
16 ============
16 ============
17
17
18 This document explains the basic communications design and messaging
18 This document explains the basic communications design and messaging
19 specification for how the various IPython objects interact over a network
19 specification for how the various IPython objects interact over a network
20 transport. The current implementation uses the ZeroMQ_ library for messaging
20 transport. The current implementation uses the ZeroMQ_ library for messaging
21 within and between hosts.
21 within and between hosts.
22
22
23 .. Note::
23 .. Note::
24
24
25 This document should be considered the authoritative description of the
25 This document should be considered the authoritative description of the
26 IPython messaging protocol, and all developers are strongly encouraged to
26 IPython messaging protocol, and all developers are strongly encouraged to
27 keep it updated as the implementation evolves, so that we have a single
27 keep it updated as the implementation evolves, so that we have a single
28 common reference for all protocol details.
28 common reference for all protocol details.
29
29
30 The basic design is explained in the following diagram:
30 The basic design is explained in the following diagram:
31
31
32 .. image:: figs/frontend-kernel.png
32 .. image:: figs/frontend-kernel.png
33 :width: 450px
33 :width: 450px
34 :alt: IPython kernel/frontend messaging architecture.
34 :alt: IPython kernel/frontend messaging architecture.
35 :align: center
35 :align: center
36 :target: ../_images/frontend-kernel.png
36 :target: ../_images/frontend-kernel.png
37
37
38 A single kernel can be simultaneously connected to one or more frontends. The
38 A single kernel can be simultaneously connected to one or more frontends. The
39 kernel has three sockets that serve the following functions:
39 kernel has three sockets that serve the following functions:
40
40
41 1. stdin: this ROUTER socket is connected to all frontends, and it allows
41 1. stdin: this ROUTER socket is connected to all frontends, and it allows
42 the kernel to request input from the active frontend when :func:`raw_input` is called.
42 the kernel to request input from the active frontend when :func:`raw_input` is called.
43 The frontend that executed the code has a DEALER socket that acts as a 'virtual keyboard'
43 The frontend that executed the code has a DEALER socket that acts as a 'virtual keyboard'
44 for the kernel while this communication is happening (illustrated in the
44 for the kernel while this communication is happening (illustrated in the
45 figure by the black outline around the central keyboard). In practice,
45 figure by the black outline around the central keyboard). In practice,
46 frontends may display such kernel requests using a special input widget or
46 frontends may display such kernel requests using a special input widget or
47 otherwise indicating that the user is to type input for the kernel instead
47 otherwise indicating that the user is to type input for the kernel instead
48 of normal commands in the frontend.
48 of normal commands in the frontend.
49
49
50 2. Shell: this single ROUTER socket allows multiple incoming connections from
50 2. Shell: this single ROUTER socket allows multiple incoming connections from
51 frontends, and this is the socket where requests for code execution, object
51 frontends, and this is the socket where requests for code execution, object
52 information, prompts, etc. are made to the kernel by any frontend. The
52 information, prompts, etc. are made to the kernel by any frontend. The
53 communication on this socket is a sequence of request/reply actions from
53 communication on this socket is a sequence of request/reply actions from
54 each frontend and the kernel.
54 each frontend and the kernel.
55
55
56 3. IOPub: this socket is the 'broadcast channel' where the kernel publishes all
56 3. IOPub: this socket is the 'broadcast channel' where the kernel publishes all
57 side effects (stdout, stderr, etc.) as well as the requests coming from any
57 side effects (stdout, stderr, etc.) as well as the requests coming from any
58 client over the shell socket and its own requests on the stdin socket. There
58 client over the shell socket and its own requests on the stdin socket. There
59 are a number of actions in Python which generate side effects: :func:`print`
59 are a number of actions in Python which generate side effects: :func:`print`
60 writes to ``sys.stdout``, errors generate tracebacks, etc. Additionally, in
60 writes to ``sys.stdout``, errors generate tracebacks, etc. Additionally, in
61 a multi-client scenario, we want all frontends to be able to know what each
61 a multi-client scenario, we want all frontends to be able to know what each
62 other has sent to the kernel (this can be useful in collaborative scenarios,
62 other has sent to the kernel (this can be useful in collaborative scenarios,
63 for example). This socket allows both side effects and the information
63 for example). This socket allows both side effects and the information
64 about communications taking place with one client over the shell channel
64 about communications taking place with one client over the shell channel
65 to be made available to all clients in a uniform manner.
65 to be made available to all clients in a uniform manner.
66
66
67 All messages are tagged with enough information (details below) for clients
67 All messages are tagged with enough information (details below) for clients
68 to know which messages come from their own interaction with the kernel and
68 to know which messages come from their own interaction with the kernel and
69 which ones are from other clients, so they can display each type
69 which ones are from other clients, so they can display each type
70 appropriately.
70 appropriately.
71
71
72 The actual format of the messages allowed on each of these channels is
72 The actual format of the messages allowed on each of these channels is
73 specified below. Messages are dicts of dicts with string keys and values that
73 specified below. Messages are dicts of dicts with string keys and values that
74 are reasonably representable in JSON. Our current implementation uses JSON
74 are reasonably representable in JSON. Our current implementation uses JSON
75 explicitly as its message format, but this shouldn't be considered a permanent
75 explicitly as its message format, but this shouldn't be considered a permanent
76 feature. As we've discovered that JSON has non-trivial performance issues due
76 feature. As we've discovered that JSON has non-trivial performance issues due
77 to excessive copying, we may in the future move to a pure pickle-based raw
77 to excessive copying, we may in the future move to a pure pickle-based raw
78 message format. However, it should be possible to easily convert from the raw
78 message format. However, it should be possible to easily convert from the raw
79 objects to JSON, since we may have non-python clients (e.g. a web frontend).
79 objects to JSON, since we may have non-python clients (e.g. a web frontend).
80 As long as it's easy to make a JSON version of the objects that is a faithful
80 As long as it's easy to make a JSON version of the objects that is a faithful
81 representation of all the data, we can communicate with such clients.
81 representation of all the data, we can communicate with such clients.
82
82
83 .. Note::
83 .. Note::
84
84
85 Not all of these have yet been fully fleshed out, but the key ones are, see
85 Not all of these have yet been fully fleshed out, but the key ones are, see
86 kernel and frontend files for actual implementation details.
86 kernel and frontend files for actual implementation details.
87
87
88 General Message Format
88 General Message Format
89 ======================
89 ======================
90
90
91 A message is defined by the following four-dictionary structure::
91 A message is defined by the following four-dictionary structure::
92
92
93 {
93 {
94 # The message header contains a pair of unique identifiers for the
94 # The message header contains a pair of unique identifiers for the
95 # originating session and the actual message id, in addition to the
95 # originating session and the actual message id, in addition to the
96 # username for the process that generated the message. This is useful in
96 # username for the process that generated the message. This is useful in
97 # collaborative settings where multiple users may be interacting with the
97 # collaborative settings where multiple users may be interacting with the
98 # same kernel simultaneously, so that frontends can label the various
98 # same kernel simultaneously, so that frontends can label the various
99 # messages in a meaningful way.
99 # messages in a meaningful way.
100 'header' : {
100 'header' : {
101 'msg_id' : uuid,
101 'msg_id' : uuid,
102 'username' : str,
102 'username' : str,
103 'session' : uuid,
103 'session' : uuid,
104 # All recognized message type strings are listed below.
104 # All recognized message type strings are listed below.
105 'msg_type' : str,
105 'msg_type' : str,
106 },
106 },
107
107
108 # In a chain of messages, the header from the parent is copied so that
108 # In a chain of messages, the header from the parent is copied so that
109 # clients can track where messages come from.
109 # clients can track where messages come from.
110 'parent_header' : dict,
110 'parent_header' : dict,
111
111
112 # Any metadata associated with the message.
112 # Any metadata associated with the message.
113 'metadata' : dict,
113 'metadata' : dict,
114
114
115 # The actual content of the message must be a dict, whose structure
115 # The actual content of the message must be a dict, whose structure
116 # depends on the message type.
116 # depends on the message type.
117 'content' : dict,
117 'content' : dict,
118 }
118 }
119
119
120 The Wire Protocol
120 The Wire Protocol
121 =================
121 =================
122
122
123
123
124 This message format exists at a high level,
124 This message format exists at a high level,
125 but does not describe the actual *implementation* at the wire level in zeromq.
125 but does not describe the actual *implementation* at the wire level in zeromq.
126 The canonical implementation of the message spec is our :class:`~IPython.kernel.zmq.session.Session` class.
126 The canonical implementation of the message spec is our :class:`~IPython.kernel.zmq.session.Session` class.
127
127
128 .. note::
128 .. note::
129
129
130 This section should only be relevant to non-Python consumers of the protocol.
130 This section should only be relevant to non-Python consumers of the protocol.
131 Python consumers should simply import and use IPython's own implementation of the wire protocol
131 Python consumers should simply import and use IPython's own implementation of the wire protocol
132 in the :class:`IPython.kernel.zmq.session.Session` object.
132 in the :class:`IPython.kernel.zmq.session.Session` object.
133
133
134 Every message is serialized to a sequence of at least six blobs of bytes:
134 Every message is serialized to a sequence of at least six blobs of bytes:
135
135
136 .. sourcecode:: python
136 .. sourcecode:: python
137
137
138 [
138 [
139 b'u-u-i-d', # zmq identity(ies)
139 b'u-u-i-d', # zmq identity(ies)
140 b'<IDS|MSG>', # delimiter
140 b'<IDS|MSG>', # delimiter
141 b'baddad42', # HMAC signature
141 b'baddad42', # HMAC signature
142 b'{header}', # serialized header dict
142 b'{header}', # serialized header dict
143 b'{parent_header}', # serialized parent header dict
143 b'{parent_header}', # serialized parent header dict
144 b'{metadata}', # serialized metadata dict
144 b'{metadata}', # serialized metadata dict
145 b'{content}, # serialized content dict
145 b'{content}, # serialized content dict
146 b'blob', # extra raw data buffer(s)
146 b'blob', # extra raw data buffer(s)
147 ...
147 ...
148 ]
148 ]
149
149
150 The front of the message is the ZeroMQ routing prefix,
150 The front of the message is the ZeroMQ routing prefix,
151 which can be zero or more socket identities.
151 which can be zero or more socket identities.
152 This is every piece of the message prior to the delimiter key ``<IDS|MSG>``.
152 This is every piece of the message prior to the delimiter key ``<IDS|MSG>``.
153 In the case of IOPub, there should be just one prefix component,
153 In the case of IOPub, there should be just one prefix component,
154 which is the topic for IOPub subscribers, e.g. ``pyout``, ``display_data``.
154 which is the topic for IOPub subscribers, e.g. ``pyout``, ``display_data``.
155
155
156 .. note::
156 .. note::
157
157
158 In most cases, the IOPub topics are irrelevant and completely ignored,
158 In most cases, the IOPub topics are irrelevant and completely ignored,
159 because frontends just subscribe to all topics.
159 because frontends just subscribe to all topics.
160 The convention used in the IPython kernel is to use the msg_type as the topic,
160 The convention used in the IPython kernel is to use the msg_type as the topic,
161 and possibly extra information about the message, e.g. ``pyout`` or ``stream.stdout``
161 and possibly extra information about the message, e.g. ``pyout`` or ``stream.stdout``
162
162
163 After the delimiter is the `HMAC`_ signature of the message, used for authentication.
163 After the delimiter is the `HMAC`_ signature of the message, used for authentication.
164 If authentication is disabled, this should be an empty string.
164 If authentication is disabled, this should be an empty string.
165 By default, the hashing function used for computing these signatures is sha256.
165 By default, the hashing function used for computing these signatures is sha256.
166
166
167 .. _HMAC: http://en.wikipedia.org/wiki/HMAC
167 .. _HMAC: http://en.wikipedia.org/wiki/HMAC
168
168
169 .. note::
169 .. note::
170
170
171 To disable authentication and signature checking,
171 To disable authentication and signature checking,
172 set the `key` field of a connection file to an empty string.
172 set the `key` field of a connection file to an empty string.
173
173
174 The signature is the HMAC hex digest of the concatenation of:
174 The signature is the HMAC hex digest of the concatenation of:
175
175
176 - A shared key (typically the ``key`` field of a connection file)
176 - A shared key (typically the ``key`` field of a connection file)
177 - The serialized header dict
177 - The serialized header dict
178 - The serialized parent header dict
178 - The serialized parent header dict
179 - The serialized metadata dict
179 - The serialized metadata dict
180 - The serialized content dict
180 - The serialized content dict
181
181
182 In Python, this is implemented via:
182 In Python, this is implemented via:
183
183
184 .. sourcecode:: python
184 .. sourcecode:: python
185
185
186 # once:
186 # once:
187 digester = HMAC(key, digestmod=hashlib.sha256)
187 digester = HMAC(key, digestmod=hashlib.sha256)
188
188
189 # for each message
189 # for each message
190 d = digester.copy()
190 d = digester.copy()
191 for serialized_dict in (header, parent, metadata, content):
191 for serialized_dict in (header, parent, metadata, content):
192 d.update(serialized_dict)
192 d.update(serialized_dict)
193 signature = d.hexdigest()
193 signature = d.hexdigest()
194
194
195 After the signature is the actual message, always in four frames of bytes.
195 After the signature is the actual message, always in four frames of bytes.
196 The four dictionaries that compose a message are serialized separately,
196 The four dictionaries that compose a message are serialized separately,
197 in the order of header, parent header, metadata, and content.
197 in the order of header, parent header, metadata, and content.
198 These can be serialized by any function that turns a dict into bytes.
198 These can be serialized by any function that turns a dict into bytes.
199 The default and most common serialization is JSON, but msgpack and pickle
199 The default and most common serialization is JSON, but msgpack and pickle
200 are common alternatives.
200 are common alternatives.
201
201
202 After the serialized dicts are zero to many raw data buffers,
202 After the serialized dicts are zero to many raw data buffers,
203 which can be used by message types that support binary data (mainly apply and data_pub).
203 which can be used by message types that support binary data (mainly apply and data_pub).
204
204
205
205
206 Python functional API
206 Python functional API
207 =====================
207 =====================
208
208
209 As messages are dicts, they map naturally to a ``func(**kw)`` call form. We
209 As messages are dicts, they map naturally to a ``func(**kw)`` call form. We
210 should develop, at a few key points, functional forms of all the requests that
210 should develop, at a few key points, functional forms of all the requests that
211 take arguments in this manner and automatically construct the necessary dict
211 take arguments in this manner and automatically construct the necessary dict
212 for sending.
212 for sending.
213
213
214 In addition, the Python implementation of the message specification extends
214 In addition, the Python implementation of the message specification extends
215 messages upon deserialization to the following form for convenience::
215 messages upon deserialization to the following form for convenience::
216
216
217 {
217 {
218 'header' : dict,
218 'header' : dict,
219 # The msg's unique identifier and type are always stored in the header,
219 # The msg's unique identifier and type are always stored in the header,
220 # but the Python implementation copies them to the top level.
220 # but the Python implementation copies them to the top level.
221 'msg_id' : uuid,
221 'msg_id' : uuid,
222 'msg_type' : str,
222 'msg_type' : str,
223 'parent_header' : dict,
223 'parent_header' : dict,
224 'content' : dict,
224 'content' : dict,
225 'metadata' : dict,
225 'metadata' : dict,
226 }
226 }
227
227
228 All messages sent to or received by any IPython process should have this
228 All messages sent to or received by any IPython process should have this
229 extended structure.
229 extended structure.
230
230
231
231
232 Messages on the shell ROUTER/DEALER sockets
232 Messages on the shell ROUTER/DEALER sockets
233 ===========================================
233 ===========================================
234
234
235 .. _execute:
235 .. _execute:
236
236
237 Execute
237 Execute
238 -------
238 -------
239
239
240 This message type is used by frontends to ask the kernel to execute code on
240 This message type is used by frontends to ask the kernel to execute code on
241 behalf of the user, in a namespace reserved to the user's variables (and thus
241 behalf of the user, in a namespace reserved to the user's variables (and thus
242 separate from the kernel's own internal code and variables).
242 separate from the kernel's own internal code and variables).
243
243
244 Message type: ``execute_request``::
244 Message type: ``execute_request``::
245
245
246 content = {
246 content = {
247 # Source code to be executed by the kernel, one or more lines.
247 # Source code to be executed by the kernel, one or more lines.
248 'code' : str,
248 'code' : str,
249
249
250 # A boolean flag which, if True, signals the kernel to execute
250 # A boolean flag which, if True, signals the kernel to execute
251 # this code as quietly as possible. This means that the kernel
251 # this code as quietly as possible. This means that the kernel
252 # will compile the code with 'exec' instead of 'single' (so
252 # will compile the code with 'exec' instead of 'single' (so
253 # sys.displayhook will not fire), forces store_history to be False,
253 # sys.displayhook will not fire), forces store_history to be False,
254 # and will *not*:
254 # and will *not*:
255 # - broadcast exceptions on the PUB socket
255 # - broadcast exceptions on the PUB socket
256 # - do any logging
256 # - do any logging
257 #
257 #
258 # The default is False.
258 # The default is False.
259 'silent' : bool,
259 'silent' : bool,
260
260
261 # A boolean flag which, if True, signals the kernel to populate history
261 # A boolean flag which, if True, signals the kernel to populate history
262 # The default is True if silent is False. If silent is True, store_history
262 # The default is True if silent is False. If silent is True, store_history
263 # is forced to be False.
263 # is forced to be False.
264 'store_history' : bool,
264 'store_history' : bool,
265
265
266 # A list of variable names from the user's namespace to be retrieved.
266 # A list of variable names from the user's namespace to be retrieved.
267 # What returns is a rich representation of each variable (dict keyed by name).
267 # What returns is a rich representation of each variable (dict keyed by name).
268 # See the display_data content for the structure of the representation data.
268 # See the display_data content for the structure of the representation data.
269 'user_variables' : list,
269 'user_variables' : list,
270
270
271 # Similarly, a dict mapping names to expressions to be evaluated in the
271 # Similarly, a dict mapping names to expressions to be evaluated in the
272 # user's dict.
272 # user's dict.
273 'user_expressions' : dict,
273 'user_expressions' : dict,
274
274
275 # Some frontends (e.g. the Notebook) do not support stdin requests. If
275 # Some frontends (e.g. the Notebook) do not support stdin requests. If
276 # raw_input is called from code executed from such a frontend, a
276 # raw_input is called from code executed from such a frontend, a
277 # StdinNotImplementedError will be raised.
277 # StdinNotImplementedError will be raised.
278 'allow_stdin' : True,
278 'allow_stdin' : True,
279
279
280 }
280 }
281
281
282 The ``code`` field contains a single string (possibly multiline). The kernel
282 The ``code`` field contains a single string (possibly multiline). The kernel
283 is responsible for splitting this into one or more independent execution blocks
283 is responsible for splitting this into one or more independent execution blocks
284 and deciding whether to compile these in 'single' or 'exec' mode (see below for
284 and deciding whether to compile these in 'single' or 'exec' mode (see below for
285 detailed execution semantics).
285 detailed execution semantics).
286
286
287 The ``user_`` fields deserve a detailed explanation. In the past, IPython had
287 The ``user_`` fields deserve a detailed explanation. In the past, IPython had
288 the notion of a prompt string that allowed arbitrary code to be evaluated, and
288 the notion of a prompt string that allowed arbitrary code to be evaluated, and
289 this was put to good use by many in creating prompts that displayed system
289 this was put to good use by many in creating prompts that displayed system
290 status, path information, and even more esoteric uses like remote instrument
290 status, path information, and even more esoteric uses like remote instrument
291 status acquired over the network. But now that IPython has a clean separation
291 status acquired over the network. But now that IPython has a clean separation
292 between the kernel and the clients, the kernel has no prompt knowledge; prompts
292 between the kernel and the clients, the kernel has no prompt knowledge; prompts
293 are a frontend-side feature, and it should be even possible for different
293 are a frontend-side feature, and it should be even possible for different
294 frontends to display different prompts while interacting with the same kernel.
294 frontends to display different prompts while interacting with the same kernel.
295
295
296 The kernel now provides the ability to retrieve data from the user's namespace
296 The kernel now provides the ability to retrieve data from the user's namespace
297 after the execution of the main ``code``, thanks to two fields in the
297 after the execution of the main ``code``, thanks to two fields in the
298 ``execute_request`` message:
298 ``execute_request`` message:
299
299
300 - ``user_variables``: If only variables from the user's namespace are needed, a
300 - ``user_variables``: If only variables from the user's namespace are needed, a
301 list of variable names can be passed and a dict with these names as keys and
301 list of variable names can be passed and a dict with these names as keys and
302 their :func:`repr()` as values will be returned.
302 their :func:`repr()` as values will be returned.
303
303
304 - ``user_expressions``: For more complex expressions that require function
304 - ``user_expressions``: For more complex expressions that require function
305 evaluations, a dict can be provided with string keys and arbitrary python
305 evaluations, a dict can be provided with string keys and arbitrary python
306 expressions as values. The return message will contain also a dict with the
306 expressions as values. The return message will contain also a dict with the
307 same keys and the :func:`repr()` of the evaluated expressions as value.
307 same keys and the :func:`repr()` of the evaluated expressions as value.
308
308
309 With this information, frontends can display any status information they wish
309 With this information, frontends can display any status information they wish
310 in the form that best suits each frontend (a status line, a popup, inline for a
310 in the form that best suits each frontend (a status line, a popup, inline for a
311 terminal, etc).
311 terminal, etc).
312
312
313 .. Note::
313 .. Note::
314
314
315 In order to obtain the current execution counter for the purposes of
315 In order to obtain the current execution counter for the purposes of
316 displaying input prompts, frontends simply make an execution request with an
316 displaying input prompts, frontends simply make an execution request with an
317 empty code string and ``silent=True``.
317 empty code string and ``silent=True``.
318
318
319 Execution semantics
319 Execution semantics
320 ~~~~~~~~~~~~~~~~~~~
320 ~~~~~~~~~~~~~~~~~~~
321
321
322 When the silent flag is false, the execution of use code consists of the
322 When the silent flag is false, the execution of use code consists of the
323 following phases (in silent mode, only the ``code`` field is executed):
323 following phases (in silent mode, only the ``code`` field is executed):
324
324
325 1. Run the ``pre_runcode_hook``.
325 1. Run the ``pre_runcode_hook``.
326
326
327 2. Execute the ``code`` field, see below for details.
327 2. Execute the ``code`` field, see below for details.
328
328
329 3. If #2 succeeds, compute ``user_variables`` and ``user_expressions`` are
329 3. If #2 succeeds, compute ``user_variables`` and ``user_expressions`` are
330 computed. This ensures that any error in the latter don't harm the main
330 computed. This ensures that any error in the latter don't harm the main
331 code execution.
331 code execution.
332
332
333 4. Call any method registered with :meth:`register_post_execute`.
333 4. Call any method registered with :meth:`register_post_execute`.
334
334
335 .. warning::
335 .. warning::
336
336
337 The API for running code before/after the main code block is likely to
337 The API for running code before/after the main code block is likely to
338 change soon. Both the ``pre_runcode_hook`` and the
338 change soon. Both the ``pre_runcode_hook`` and the
339 :meth:`register_post_execute` are susceptible to modification, as we find a
339 :meth:`register_post_execute` are susceptible to modification, as we find a
340 consistent model for both.
340 consistent model for both.
341
341
342 To understand how the ``code`` field is executed, one must know that Python
342 To understand how the ``code`` field is executed, one must know that Python
343 code can be compiled in one of three modes (controlled by the ``mode`` argument
343 code can be compiled in one of three modes (controlled by the ``mode`` argument
344 to the :func:`compile` builtin):
344 to the :func:`compile` builtin):
345
345
346 *single*
346 *single*
347 Valid for a single interactive statement (though the source can contain
347 Valid for a single interactive statement (though the source can contain
348 multiple lines, such as a for loop). When compiled in this mode, the
348 multiple lines, such as a for loop). When compiled in this mode, the
349 generated bytecode contains special instructions that trigger the calling of
349 generated bytecode contains special instructions that trigger the calling of
350 :func:`sys.displayhook` for any expression in the block that returns a value.
350 :func:`sys.displayhook` for any expression in the block that returns a value.
351 This means that a single statement can actually produce multiple calls to
351 This means that a single statement can actually produce multiple calls to
352 :func:`sys.displayhook`, if for example it contains a loop where each
352 :func:`sys.displayhook`, if for example it contains a loop where each
353 iteration computes an unassigned expression would generate 10 calls::
353 iteration computes an unassigned expression would generate 10 calls::
354
354
355 for i in range(10):
355 for i in range(10):
356 i**2
356 i**2
357
357
358 *exec*
358 *exec*
359 An arbitrary amount of source code, this is how modules are compiled.
359 An arbitrary amount of source code, this is how modules are compiled.
360 :func:`sys.displayhook` is *never* implicitly called.
360 :func:`sys.displayhook` is *never* implicitly called.
361
361
362 *eval*
362 *eval*
363 A single expression that returns a value. :func:`sys.displayhook` is *never*
363 A single expression that returns a value. :func:`sys.displayhook` is *never*
364 implicitly called.
364 implicitly called.
365
365
366
366
367 The ``code`` field is split into individual blocks each of which is valid for
367 The ``code`` field is split into individual blocks each of which is valid for
368 execution in 'single' mode, and then:
368 execution in 'single' mode, and then:
369
369
370 - If there is only a single block: it is executed in 'single' mode.
370 - If there is only a single block: it is executed in 'single' mode.
371
371
372 - If there is more than one block:
372 - If there is more than one block:
373
373
374 * if the last one is a single line long, run all but the last in 'exec' mode
374 * if the last one is a single line long, run all but the last in 'exec' mode
375 and the very last one in 'single' mode. This makes it easy to type simple
375 and the very last one in 'single' mode. This makes it easy to type simple
376 expressions at the end to see computed values.
376 expressions at the end to see computed values.
377
377
378 * if the last one is no more than two lines long, run all but the last in
378 * if the last one is no more than two lines long, run all but the last in
379 'exec' mode and the very last one in 'single' mode. This makes it easy to
379 'exec' mode and the very last one in 'single' mode. This makes it easy to
380 type simple expressions at the end to see computed values. - otherwise
380 type simple expressions at the end to see computed values. - otherwise
381 (last one is also multiline), run all in 'exec' mode
381 (last one is also multiline), run all in 'exec' mode
382
382
383 * otherwise (last one is also multiline), run all in 'exec' mode as a single
383 * otherwise (last one is also multiline), run all in 'exec' mode as a single
384 unit.
384 unit.
385
385
386 Any error in retrieving the ``user_variables`` or evaluating the
386 Any error in retrieving the ``user_variables`` or evaluating the
387 ``user_expressions`` will result in a simple error message in the return fields
387 ``user_expressions`` will result in a simple error message in the return fields
388 of the form::
388 of the form::
389
389
390 [ERROR] ExceptionType: Exception message
390 [ERROR] ExceptionType: Exception message
391
391
392 The user can simply send the same variable name or expression for evaluation to
392 The user can simply send the same variable name or expression for evaluation to
393 see a regular traceback.
393 see a regular traceback.
394
394
395 Errors in any registered post_execute functions are also reported similarly,
395 Errors in any registered post_execute functions are also reported similarly,
396 and the failing function is removed from the post_execution set so that it does
396 and the failing function is removed from the post_execution set so that it does
397 not continue triggering failures.
397 not continue triggering failures.
398
398
399 Upon completion of the execution request, the kernel *always* sends a reply,
399 Upon completion of the execution request, the kernel *always* sends a reply,
400 with a status code indicating what happened and additional data depending on
400 with a status code indicating what happened and additional data depending on
401 the outcome. See :ref:`below <execution_results>` for the possible return
401 the outcome. See :ref:`below <execution_results>` for the possible return
402 codes and associated data.
402 codes and associated data.
403
403
404
404
405 .. _execution_counter:
406
405 Execution counter (old prompt number)
407 Execution counter (old prompt number)
406 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
408 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
407
409
408 The kernel has a single, monotonically increasing counter of all execution
410 The kernel has a single, monotonically increasing counter of all execution
409 requests that are made with ``store_history=True``. This counter is used to populate
411 requests that are made with ``store_history=True``. This counter is used to populate
410 the ``In[n]``, ``Out[n]`` and ``_n`` variables, so clients will likely want to
412 the ``In[n]``, ``Out[n]`` and ``_n`` variables, so clients will likely want to
411 display it in some form to the user, which will typically (but not necessarily)
413 display it in some form to the user, which will typically (but not necessarily)
412 be done in the prompts. The value of this counter will be returned as the
414 be done in the prompts. The value of this counter will be returned as the
413 ``execution_count`` field of all ``execute_reply`` messages.
415 ``execution_count`` field of all ``execute_reply`` and ``pyin`` messages.
414
416
415 .. _execution_results:
417 .. _execution_results:
416
418
417 Execution results
419 Execution results
418 ~~~~~~~~~~~~~~~~~
420 ~~~~~~~~~~~~~~~~~
419
421
420 Message type: ``execute_reply``::
422 Message type: ``execute_reply``::
421
423
422 content = {
424 content = {
423 # One of: 'ok' OR 'error' OR 'abort'
425 # One of: 'ok' OR 'error' OR 'abort'
424 'status' : str,
426 'status' : str,
425
427
426 # The global kernel counter that increases by one with each request that
428 # The global kernel counter that increases by one with each request that
427 # stores history. This will typically be used by clients to display
429 # stores history. This will typically be used by clients to display
428 # prompt numbers to the user. If the request did not store history, this will
430 # prompt numbers to the user. If the request did not store history, this will
429 # be the current value of the counter in the kernel.
431 # be the current value of the counter in the kernel.
430 'execution_count' : int,
432 'execution_count' : int,
431 }
433 }
432
434
433 When status is 'ok', the following extra fields are present::
435 When status is 'ok', the following extra fields are present::
434
436
435 {
437 {
436 # 'payload' will be a list of payload dicts.
438 # 'payload' will be a list of payload dicts.
437 # Each execution payload is a dict with string keys that may have been
439 # Each execution payload is a dict with string keys that may have been
438 # produced by the code being executed. It is retrieved by the kernel at
440 # produced by the code being executed. It is retrieved by the kernel at
439 # the end of the execution and sent back to the front end, which can take
441 # the end of the execution and sent back to the front end, which can take
440 # action on it as needed.
442 # action on it as needed.
441 # The only requirement of each payload dict is that it have a 'source' key,
443 # The only requirement of each payload dict is that it have a 'source' key,
442 # which is a string classifying the payload (e.g. 'pager').
444 # which is a string classifying the payload (e.g. 'pager').
443 'payload' : list(dict),
445 'payload' : list(dict),
444
446
445 # Results for the user_variables and user_expressions.
447 # Results for the user_variables and user_expressions.
446 'user_variables' : dict,
448 'user_variables' : dict,
447 'user_expressions' : dict,
449 'user_expressions' : dict,
448 }
450 }
449
451
450 .. admonition:: Execution payloads
452 .. admonition:: Execution payloads
451
453
452 The notion of an 'execution payload' is different from a return value of a
454 The notion of an 'execution payload' is different from a return value of a
453 given set of code, which normally is just displayed on the pyout stream
455 given set of code, which normally is just displayed on the pyout stream
454 through the PUB socket. The idea of a payload is to allow special types of
456 through the PUB socket. The idea of a payload is to allow special types of
455 code, typically magics, to populate a data container in the IPython kernel
457 code, typically magics, to populate a data container in the IPython kernel
456 that will be shipped back to the caller via this channel. The kernel
458 that will be shipped back to the caller via this channel. The kernel
457 has an API for this in the PayloadManager::
459 has an API for this in the PayloadManager::
458
460
459 ip.payload_manager.write_payload(payload_dict)
461 ip.payload_manager.write_payload(payload_dict)
460
462
461 which appends a dictionary to the list of payloads.
463 which appends a dictionary to the list of payloads.
462
464
463 The payload API is not yet stabilized,
465 The payload API is not yet stabilized,
464 and should probably not be supported by non-Python kernels at this time.
466 and should probably not be supported by non-Python kernels at this time.
465 In such cases, the payload list should always be empty.
467 In such cases, the payload list should always be empty.
466
468
467
469
468 When status is 'error', the following extra fields are present::
470 When status is 'error', the following extra fields are present::
469
471
470 {
472 {
471 'ename' : str, # Exception name, as a string
473 'ename' : str, # Exception name, as a string
472 'evalue' : str, # Exception value, as a string
474 'evalue' : str, # Exception value, as a string
473
475
474 # The traceback will contain a list of frames, represented each as a
476 # The traceback will contain a list of frames, represented each as a
475 # string. For now we'll stick to the existing design of ultraTB, which
477 # string. For now we'll stick to the existing design of ultraTB, which
476 # controls exception level of detail statefully. But eventually we'll
478 # controls exception level of detail statefully. But eventually we'll
477 # want to grow into a model where more information is collected and
479 # want to grow into a model where more information is collected and
478 # packed into the traceback object, with clients deciding how little or
480 # packed into the traceback object, with clients deciding how little or
479 # how much of it to unpack. But for now, let's start with a simple list
481 # how much of it to unpack. But for now, let's start with a simple list
480 # of strings, since that requires only minimal changes to ultratb as
482 # of strings, since that requires only minimal changes to ultratb as
481 # written.
483 # written.
482 'traceback' : list,
484 'traceback' : list,
483 }
485 }
484
486
485
487
486 When status is 'abort', there are for now no additional data fields. This
488 When status is 'abort', there are for now no additional data fields. This
487 happens when the kernel was interrupted by a signal.
489 happens when the kernel was interrupted by a signal.
488
490
489
491
490 Object information
492 Object information
491 ------------------
493 ------------------
492
494
493 One of IPython's most used capabilities is the introspection of Python objects
495 One of IPython's most used capabilities is the introspection of Python objects
494 in the user's namespace, typically invoked via the ``?`` and ``??`` characters
496 in the user's namespace, typically invoked via the ``?`` and ``??`` characters
495 (which in reality are shorthands for the ``%pinfo`` magic). This is used often
497 (which in reality are shorthands for the ``%pinfo`` magic). This is used often
496 enough that it warrants an explicit message type, especially because frontends
498 enough that it warrants an explicit message type, especially because frontends
497 may want to get object information in response to user keystrokes (like Tab or
499 may want to get object information in response to user keystrokes (like Tab or
498 F1) besides from the user explicitly typing code like ``x??``.
500 F1) besides from the user explicitly typing code like ``x??``.
499
501
500 Message type: ``object_info_request``::
502 Message type: ``object_info_request``::
501
503
502 content = {
504 content = {
503 # The (possibly dotted) name of the object to be searched in all
505 # The (possibly dotted) name of the object to be searched in all
504 # relevant namespaces
506 # relevant namespaces
505 'oname' : str,
507 'oname' : str,
506
508
507 # The level of detail desired. The default (0) is equivalent to typing
509 # The level of detail desired. The default (0) is equivalent to typing
508 # 'x?' at the prompt, 1 is equivalent to 'x??'.
510 # 'x?' at the prompt, 1 is equivalent to 'x??'.
509 'detail_level' : int,
511 'detail_level' : int,
510 }
512 }
511
513
512 The returned information will be a dictionary with keys very similar to the
514 The returned information will be a dictionary with keys very similar to the
513 field names that IPython prints at the terminal.
515 field names that IPython prints at the terminal.
514
516
515 Message type: ``object_info_reply``::
517 Message type: ``object_info_reply``::
516
518
517 content = {
519 content = {
518 # The name the object was requested under
520 # The name the object was requested under
519 'name' : str,
521 'name' : str,
520
522
521 # Boolean flag indicating whether the named object was found or not. If
523 # Boolean flag indicating whether the named object was found or not. If
522 # it's false, all other fields will be empty.
524 # it's false, all other fields will be empty.
523 'found' : bool,
525 'found' : bool,
524
526
525 # Flags for magics and system aliases
527 # Flags for magics and system aliases
526 'ismagic' : bool,
528 'ismagic' : bool,
527 'isalias' : bool,
529 'isalias' : bool,
528
530
529 # The name of the namespace where the object was found ('builtin',
531 # The name of the namespace where the object was found ('builtin',
530 # 'magics', 'alias', 'interactive', etc.)
532 # 'magics', 'alias', 'interactive', etc.)
531 'namespace' : str,
533 'namespace' : str,
532
534
533 # The type name will be type.__name__ for normal Python objects, but it
535 # The type name will be type.__name__ for normal Python objects, but it
534 # can also be a string like 'Magic function' or 'System alias'
536 # can also be a string like 'Magic function' or 'System alias'
535 'type_name' : str,
537 'type_name' : str,
536
538
537 # The string form of the object, possibly truncated for length if
539 # The string form of the object, possibly truncated for length if
538 # detail_level is 0
540 # detail_level is 0
539 'string_form' : str,
541 'string_form' : str,
540
542
541 # For objects with a __class__ attribute this will be set
543 # For objects with a __class__ attribute this will be set
542 'base_class' : str,
544 'base_class' : str,
543
545
544 # For objects with a __len__ attribute this will be set
546 # For objects with a __len__ attribute this will be set
545 'length' : int,
547 'length' : int,
546
548
547 # If the object is a function, class or method whose file we can find,
549 # If the object is a function, class or method whose file we can find,
548 # we give its full path
550 # we give its full path
549 'file' : str,
551 'file' : str,
550
552
551 # For pure Python callable objects, we can reconstruct the object
553 # For pure Python callable objects, we can reconstruct the object
552 # definition line which provides its call signature. For convenience this
554 # definition line which provides its call signature. For convenience this
553 # is returned as a single 'definition' field, but below the raw parts that
555 # is returned as a single 'definition' field, but below the raw parts that
554 # compose it are also returned as the argspec field.
556 # compose it are also returned as the argspec field.
555 'definition' : str,
557 'definition' : str,
556
558
557 # The individual parts that together form the definition string. Clients
559 # The individual parts that together form the definition string. Clients
558 # with rich display capabilities may use this to provide a richer and more
560 # with rich display capabilities may use this to provide a richer and more
559 # precise representation of the definition line (e.g. by highlighting
561 # precise representation of the definition line (e.g. by highlighting
560 # arguments based on the user's cursor position). For non-callable
562 # arguments based on the user's cursor position). For non-callable
561 # objects, this field is empty.
563 # objects, this field is empty.
562 'argspec' : { # The names of all the arguments
564 'argspec' : { # The names of all the arguments
563 args : list,
565 args : list,
564 # The name of the varargs (*args), if any
566 # The name of the varargs (*args), if any
565 varargs : str,
567 varargs : str,
566 # The name of the varkw (**kw), if any
568 # The name of the varkw (**kw), if any
567 varkw : str,
569 varkw : str,
568 # The values (as strings) of all default arguments. Note
570 # The values (as strings) of all default arguments. Note
569 # that these must be matched *in reverse* with the 'args'
571 # that these must be matched *in reverse* with the 'args'
570 # list above, since the first positional args have no default
572 # list above, since the first positional args have no default
571 # value at all.
573 # value at all.
572 defaults : list,
574 defaults : list,
573 },
575 },
574
576
575 # For instances, provide the constructor signature (the definition of
577 # For instances, provide the constructor signature (the definition of
576 # the __init__ method):
578 # the __init__ method):
577 'init_definition' : str,
579 'init_definition' : str,
578
580
579 # Docstrings: for any object (function, method, module, package) with a
581 # Docstrings: for any object (function, method, module, package) with a
580 # docstring, we show it. But in addition, we may provide additional
582 # docstring, we show it. But in addition, we may provide additional
581 # docstrings. For example, for instances we will show the constructor
583 # docstrings. For example, for instances we will show the constructor
582 # and class docstrings as well, if available.
584 # and class docstrings as well, if available.
583 'docstring' : str,
585 'docstring' : str,
584
586
585 # For instances, provide the constructor and class docstrings
587 # For instances, provide the constructor and class docstrings
586 'init_docstring' : str,
588 'init_docstring' : str,
587 'class_docstring' : str,
589 'class_docstring' : str,
588
590
589 # If it's a callable object whose call method has a separate docstring and
591 # If it's a callable object whose call method has a separate docstring and
590 # definition line:
592 # definition line:
591 'call_def' : str,
593 'call_def' : str,
592 'call_docstring' : str,
594 'call_docstring' : str,
593
595
594 # If detail_level was 1, we also try to find the source code that
596 # If detail_level was 1, we also try to find the source code that
595 # defines the object, if possible. The string 'None' will indicate
597 # defines the object, if possible. The string 'None' will indicate
596 # that no source was found.
598 # that no source was found.
597 'source' : str,
599 'source' : str,
598 }
600 }
599
601
600
602
601 Complete
603 Complete
602 --------
604 --------
603
605
604 Message type: ``complete_request``::
606 Message type: ``complete_request``::
605
607
606 content = {
608 content = {
607 # The text to be completed, such as 'a.is'
609 # The text to be completed, such as 'a.is'
608 # this may be an empty string if the frontend does not do any lexing,
610 # this may be an empty string if the frontend does not do any lexing,
609 # in which case the kernel must figure out the completion
611 # in which case the kernel must figure out the completion
610 # based on 'line' and 'cursor_pos'.
612 # based on 'line' and 'cursor_pos'.
611 'text' : str,
613 'text' : str,
612
614
613 # The full line, such as 'print a.is'. This allows completers to
615 # The full line, such as 'print a.is'. This allows completers to
614 # make decisions that may require information about more than just the
616 # make decisions that may require information about more than just the
615 # current word.
617 # current word.
616 'line' : str,
618 'line' : str,
617
619
618 # The entire block of text where the line is. This may be useful in the
620 # The entire block of text where the line is. This may be useful in the
619 # case of multiline completions where more context may be needed. Note: if
621 # case of multiline completions where more context may be needed. Note: if
620 # in practice this field proves unnecessary, remove it to lighten the
622 # in practice this field proves unnecessary, remove it to lighten the
621 # messages.
623 # messages.
622
624
623 'block' : str or null/None,
625 'block' : str or null/None,
624
626
625 # The position of the cursor where the user hit 'TAB' on the line.
627 # The position of the cursor where the user hit 'TAB' on the line.
626 'cursor_pos' : int,
628 'cursor_pos' : int,
627 }
629 }
628
630
629 Message type: ``complete_reply``::
631 Message type: ``complete_reply``::
630
632
631 content = {
633 content = {
632 # The list of all matches to the completion request, such as
634 # The list of all matches to the completion request, such as
633 # ['a.isalnum', 'a.isalpha'] for the above example.
635 # ['a.isalnum', 'a.isalpha'] for the above example.
634 'matches' : list,
636 'matches' : list,
635
637
636 # the substring of the matched text
638 # the substring of the matched text
637 # this is typically the common prefix of the matches,
639 # this is typically the common prefix of the matches,
638 # and the text that is already in the block that would be replaced by the full completion.
640 # and the text that is already in the block that would be replaced by the full completion.
639 # This would be 'a.is' in the above example.
641 # This would be 'a.is' in the above example.
640 'matched_text' : str,
642 'matched_text' : str,
641
643
642 # status should be 'ok' unless an exception was raised during the request,
644 # status should be 'ok' unless an exception was raised during the request,
643 # in which case it should be 'error', along with the usual error message content
645 # in which case it should be 'error', along with the usual error message content
644 # in other messages.
646 # in other messages.
645 'status' : 'ok'
647 'status' : 'ok'
646 }
648 }
647
649
648
650
649 History
651 History
650 -------
652 -------
651
653
652 For clients to explicitly request history from a kernel. The kernel has all
654 For clients to explicitly request history from a kernel. The kernel has all
653 the actual execution history stored in a single location, so clients can
655 the actual execution history stored in a single location, so clients can
654 request it from the kernel when needed.
656 request it from the kernel when needed.
655
657
656 Message type: ``history_request``::
658 Message type: ``history_request``::
657
659
658 content = {
660 content = {
659
661
660 # If True, also return output history in the resulting dict.
662 # If True, also return output history in the resulting dict.
661 'output' : bool,
663 'output' : bool,
662
664
663 # If True, return the raw input history, else the transformed input.
665 # If True, return the raw input history, else the transformed input.
664 'raw' : bool,
666 'raw' : bool,
665
667
666 # So far, this can be 'range', 'tail' or 'search'.
668 # So far, this can be 'range', 'tail' or 'search'.
667 'hist_access_type' : str,
669 'hist_access_type' : str,
668
670
669 # If hist_access_type is 'range', get a range of input cells. session can
671 # If hist_access_type is 'range', get a range of input cells. session can
670 # be a positive session number, or a negative number to count back from
672 # be a positive session number, or a negative number to count back from
671 # the current session.
673 # the current session.
672 'session' : int,
674 'session' : int,
673 # start and stop are line numbers within that session.
675 # start and stop are line numbers within that session.
674 'start' : int,
676 'start' : int,
675 'stop' : int,
677 'stop' : int,
676
678
677 # If hist_access_type is 'tail' or 'search', get the last n cells.
679 # If hist_access_type is 'tail' or 'search', get the last n cells.
678 'n' : int,
680 'n' : int,
679
681
680 # If hist_access_type is 'search', get cells matching the specified glob
682 # If hist_access_type is 'search', get cells matching the specified glob
681 # pattern (with * and ? as wildcards).
683 # pattern (with * and ? as wildcards).
682 'pattern' : str,
684 'pattern' : str,
683
685
684 # If hist_access_type is 'search' and unique is true, do not
686 # If hist_access_type is 'search' and unique is true, do not
685 # include duplicated history. Default is false.
687 # include duplicated history. Default is false.
686 'unique' : bool,
688 'unique' : bool,
687
689
688 }
690 }
689
691
690 .. versionadded:: 4.0
692 .. versionadded:: 4.0
691 The key ``unique`` for ``history_request``.
693 The key ``unique`` for ``history_request``.
692
694
693 Message type: ``history_reply``::
695 Message type: ``history_reply``::
694
696
695 content = {
697 content = {
696 # A list of 3 tuples, either:
698 # A list of 3 tuples, either:
697 # (session, line_number, input) or
699 # (session, line_number, input) or
698 # (session, line_number, (input, output)),
700 # (session, line_number, (input, output)),
699 # depending on whether output was False or True, respectively.
701 # depending on whether output was False or True, respectively.
700 'history' : list,
702 'history' : list,
701 }
703 }
702
704
703
705
704 Connect
706 Connect
705 -------
707 -------
706
708
707 When a client connects to the request/reply socket of the kernel, it can issue
709 When a client connects to the request/reply socket of the kernel, it can issue
708 a connect request to get basic information about the kernel, such as the ports
710 a connect request to get basic information about the kernel, such as the ports
709 the other ZeroMQ sockets are listening on. This allows clients to only have
711 the other ZeroMQ sockets are listening on. This allows clients to only have
710 to know about a single port (the shell channel) to connect to a kernel.
712 to know about a single port (the shell channel) to connect to a kernel.
711
713
712 Message type: ``connect_request``::
714 Message type: ``connect_request``::
713
715
714 content = {
716 content = {
715 }
717 }
716
718
717 Message type: ``connect_reply``::
719 Message type: ``connect_reply``::
718
720
719 content = {
721 content = {
720 'shell_port' : int, # The port the shell ROUTER socket is listening on.
722 'shell_port' : int, # The port the shell ROUTER socket is listening on.
721 'iopub_port' : int, # The port the PUB socket is listening on.
723 'iopub_port' : int, # The port the PUB socket is listening on.
722 'stdin_port' : int, # The port the stdin ROUTER socket is listening on.
724 'stdin_port' : int, # The port the stdin ROUTER socket is listening on.
723 'hb_port' : int, # The port the heartbeat socket is listening on.
725 'hb_port' : int, # The port the heartbeat socket is listening on.
724 }
726 }
725
727
726
728
727 Kernel info
729 Kernel info
728 -----------
730 -----------
729
731
730 If a client needs to know information about the kernel, it can
732 If a client needs to know information about the kernel, it can
731 make a request of the kernel's information.
733 make a request of the kernel's information.
732 This message can be used to fetch core information of the
734 This message can be used to fetch core information of the
733 kernel, including language (e.g., Python), language version number and
735 kernel, including language (e.g., Python), language version number and
734 IPython version number, and the IPython message spec version number.
736 IPython version number, and the IPython message spec version number.
735
737
736 Message type: ``kernel_info_request``::
738 Message type: ``kernel_info_request``::
737
739
738 content = {
740 content = {
739 }
741 }
740
742
741 Message type: ``kernel_info_reply``::
743 Message type: ``kernel_info_reply``::
742
744
743 content = {
745 content = {
744 # Version of messaging protocol (mandatory).
746 # Version of messaging protocol (mandatory).
745 # The first integer indicates major version. It is incremented when
747 # The first integer indicates major version. It is incremented when
746 # there is any backward incompatible change.
748 # there is any backward incompatible change.
747 # The second integer indicates minor version. It is incremented when
749 # The second integer indicates minor version. It is incremented when
748 # there is any backward compatible change.
750 # there is any backward compatible change.
749 'protocol_version': [int, int],
751 'protocol_version': [int, int],
750
752
751 # IPython version number (optional).
753 # IPython version number (optional).
752 # Non-python kernel backend may not have this version number.
754 # Non-python kernel backend may not have this version number.
753 # The last component is an extra field, which may be 'dev' or
755 # The last component is an extra field, which may be 'dev' or
754 # 'rc1' in development version. It is an empty string for
756 # 'rc1' in development version. It is an empty string for
755 # released version.
757 # released version.
756 'ipython_version': [int, int, int, str],
758 'ipython_version': [int, int, int, str],
757
759
758 # Language version number (mandatory).
760 # Language version number (mandatory).
759 # It is Python version number (e.g., [2, 7, 3]) for the kernel
761 # It is Python version number (e.g., [2, 7, 3]) for the kernel
760 # included in IPython.
762 # included in IPython.
761 'language_version': [int, ...],
763 'language_version': [int, ...],
762
764
763 # Programming language in which kernel is implemented (mandatory).
765 # Programming language in which kernel is implemented (mandatory).
764 # Kernel included in IPython returns 'python'.
766 # Kernel included in IPython returns 'python'.
765 'language': str,
767 'language': str,
766 }
768 }
767
769
768
770
769 Kernel shutdown
771 Kernel shutdown
770 ---------------
772 ---------------
771
773
772 The clients can request the kernel to shut itself down; this is used in
774 The clients can request the kernel to shut itself down; this is used in
773 multiple cases:
775 multiple cases:
774
776
775 - when the user chooses to close the client application via a menu or window
777 - when the user chooses to close the client application via a menu or window
776 control.
778 control.
777 - when the user types 'exit' or 'quit' (or their uppercase magic equivalents).
779 - when the user types 'exit' or 'quit' (or their uppercase magic equivalents).
778 - when the user chooses a GUI method (like the 'Ctrl-C' shortcut in the
780 - when the user chooses a GUI method (like the 'Ctrl-C' shortcut in the
779 IPythonQt client) to force a kernel restart to get a clean kernel without
781 IPythonQt client) to force a kernel restart to get a clean kernel without
780 losing client-side state like history or inlined figures.
782 losing client-side state like history or inlined figures.
781
783
782 The client sends a shutdown request to the kernel, and once it receives the
784 The client sends a shutdown request to the kernel, and once it receives the
783 reply message (which is otherwise empty), it can assume that the kernel has
785 reply message (which is otherwise empty), it can assume that the kernel has
784 completed shutdown safely.
786 completed shutdown safely.
785
787
786 Upon their own shutdown, client applications will typically execute a last
788 Upon their own shutdown, client applications will typically execute a last
787 minute sanity check and forcefully terminate any kernel that is still alive, to
789 minute sanity check and forcefully terminate any kernel that is still alive, to
788 avoid leaving stray processes in the user's machine.
790 avoid leaving stray processes in the user's machine.
789
791
790 Message type: ``shutdown_request``::
792 Message type: ``shutdown_request``::
791
793
792 content = {
794 content = {
793 'restart' : bool # whether the shutdown is final, or precedes a restart
795 'restart' : bool # whether the shutdown is final, or precedes a restart
794 }
796 }
795
797
796 Message type: ``shutdown_reply``::
798 Message type: ``shutdown_reply``::
797
799
798 content = {
800 content = {
799 'restart' : bool # whether the shutdown is final, or precedes a restart
801 'restart' : bool # whether the shutdown is final, or precedes a restart
800 }
802 }
801
803
802 .. Note::
804 .. Note::
803
805
804 When the clients detect a dead kernel thanks to inactivity on the heartbeat
806 When the clients detect a dead kernel thanks to inactivity on the heartbeat
805 socket, they simply send a forceful process termination signal, since a dead
807 socket, they simply send a forceful process termination signal, since a dead
806 process is unlikely to respond in any useful way to messages.
808 process is unlikely to respond in any useful way to messages.
807
809
808
810
809 Messages on the PUB/SUB socket
811 Messages on the PUB/SUB socket
810 ==============================
812 ==============================
811
813
812 Streams (stdout, stderr, etc)
814 Streams (stdout, stderr, etc)
813 ------------------------------
815 ------------------------------
814
816
815 Message type: ``stream``::
817 Message type: ``stream``::
816
818
817 content = {
819 content = {
818 # The name of the stream is one of 'stdout', 'stderr'
820 # The name of the stream is one of 'stdout', 'stderr'
819 'name' : str,
821 'name' : str,
820
822
821 # The data is an arbitrary string to be written to that stream
823 # The data is an arbitrary string to be written to that stream
822 'data' : str,
824 'data' : str,
823 }
825 }
824
826
825 Display Data
827 Display Data
826 ------------
828 ------------
827
829
828 This type of message is used to bring back data that should be displayed (text,
830 This type of message is used to bring back data that should be displayed (text,
829 html, svg, etc.) in the frontends. This data is published to all frontends.
831 html, svg, etc.) in the frontends. This data is published to all frontends.
830 Each message can have multiple representations of the data; it is up to the
832 Each message can have multiple representations of the data; it is up to the
831 frontend to decide which to use and how. A single message should contain all
833 frontend to decide which to use and how. A single message should contain all
832 possible representations of the same information. Each representation should
834 possible representations of the same information. Each representation should
833 be a JSON'able data structure, and should be a valid MIME type.
835 be a JSON'able data structure, and should be a valid MIME type.
834
836
835 Some questions remain about this design:
836
837 * Do we use this message type for pyout/displayhook? Probably not, because
838 the displayhook also has to handle the Out prompt display. On the other hand
839 we could put that information into the metadata section.
840
841 Message type: ``display_data``::
837 Message type: ``display_data``::
842
838
843 content = {
839 content = {
844
840
845 # Who create the data
841 # Who create the data
846 'source' : str,
842 'source' : str,
847
843
848 # The data dict contains key/value pairs, where the keys are MIME
844 # The data dict contains key/value pairs, where the keys are MIME
849 # types and the values are the raw data of the representation in that
845 # types and the values are the raw data of the representation in that
850 # format.
846 # format.
851 'data' : dict,
847 'data' : dict,
852
848
853 # Any metadata that describes the data
849 # Any metadata that describes the data
854 'metadata' : dict
850 'metadata' : dict
855 }
851 }
856
852
857
853
858 The ``metadata`` contains any metadata that describes the output.
854 The ``metadata`` contains any metadata that describes the output.
859 Global keys are assumed to apply to the output as a whole.
855 Global keys are assumed to apply to the output as a whole.
860 The ``metadata`` dict can also contain mime-type keys, which will be sub-dictionaries,
856 The ``metadata`` dict can also contain mime-type keys, which will be sub-dictionaries,
861 which are interpreted as applying only to output of that type.
857 which are interpreted as applying only to output of that type.
862 Third parties should put any data they write into a single dict
858 Third parties should put any data they write into a single dict
863 with a reasonably unique name to avoid conflicts.
859 with a reasonably unique name to avoid conflicts.
864
860
865 The only metadata keys currently defined in IPython are the width and height
861 The only metadata keys currently defined in IPython are the width and height
866 of images::
862 of images::
867
863
868 'metadata' : {
864 'metadata' : {
869 'image/png' : {
865 'image/png' : {
870 'width': 640,
866 'width': 640,
871 'height': 480
867 'height': 480
872 }
868 }
873 }
869 }
874
870
875
871
876 Raw Data Publication
872 Raw Data Publication
877 --------------------
873 --------------------
878
874
879 ``display_data`` lets you publish *representations* of data, such as images and html.
875 ``display_data`` lets you publish *representations* of data, such as images and html.
880 This ``data_pub`` message lets you publish *actual raw data*, sent via message buffers.
876 This ``data_pub`` message lets you publish *actual raw data*, sent via message buffers.
881
877
882 data_pub messages are constructed via the :func:`IPython.lib.datapub.publish_data` function:
878 data_pub messages are constructed via the :func:`IPython.lib.datapub.publish_data` function:
883
879
884 .. sourcecode:: python
880 .. sourcecode:: python
885
881
886 from IPython.kernel.zmq.datapub import publish_data
882 from IPython.kernel.zmq.datapub import publish_data
887 ns = dict(x=my_array)
883 ns = dict(x=my_array)
888 publish_data(ns)
884 publish_data(ns)
889
885
890
886
891 Message type: ``data_pub``::
887 Message type: ``data_pub``::
892
888
893 content = {
889 content = {
894 # the keys of the data dict, after it has been unserialized
890 # the keys of the data dict, after it has been unserialized
895 keys = ['a', 'b']
891 keys = ['a', 'b']
896 }
892 }
897 # the namespace dict will be serialized in the message buffers,
893 # the namespace dict will be serialized in the message buffers,
898 # which will have a length of at least one
894 # which will have a length of at least one
899 buffers = ['pdict', ...]
895 buffers = ['pdict', ...]
900
896
901
897
902 The interpretation of a sequence of data_pub messages for a given parent request should be
898 The interpretation of a sequence of data_pub messages for a given parent request should be
903 to update a single namespace with subsequent results.
899 to update a single namespace with subsequent results.
904
900
905 .. note::
901 .. note::
906
902
907 No frontends directly handle data_pub messages at this time.
903 No frontends directly handle data_pub messages at this time.
908 It is currently only used by the client/engines in :mod:`IPython.parallel`,
904 It is currently only used by the client/engines in :mod:`IPython.parallel`,
909 where engines may publish *data* to the Client,
905 where engines may publish *data* to the Client,
910 of which the Client can then publish *representations* via ``display_data``
906 of which the Client can then publish *representations* via ``display_data``
911 to various frontends.
907 to various frontends.
912
908
913 Python inputs
909 Python inputs
914 -------------
910 -------------
915
911
916 These messages are the re-broadcast of the ``execute_request``.
912 To let all frontends know what code is being executed at any given time, these
913 messages contain a re-broadcast of the ``code`` portion of an
914 :ref:`execute_request <execute>`, along with the :ref:`execution_count
915 <execution_counter>`.
917
916
918 Message type: ``pyin``::
917 Message type: ``pyin``::
919
918
920 content = {
919 content = {
921 'code' : str, # Source code to be executed, one or more lines
920 'code' : str, # Source code to be executed, one or more lines
922
921
923 # The counter for this execution is also provided so that clients can
922 # The counter for this execution is also provided so that clients can
924 # display it, since IPython automatically creates variables called _iN
923 # display it, since IPython automatically creates variables called _iN
925 # (for input prompt In[N]).
924 # (for input prompt In[N]).
926 'execution_count' : int
925 'execution_count' : int
927 }
926 }
928
927
929 Python outputs
928 Python outputs
930 --------------
929 --------------
931
930
932 When Python produces output from code that has been compiled in with the
931 When Python produces output from code that has been compiled in with the
933 'single' flag to :func:`compile`, any expression that produces a value (such as
932 'single' flag to :func:`compile`, any expression that produces a value (such as
934 ``1+1``) is passed to ``sys.displayhook``, which is a callable that can do with
933 ``1+1``) is passed to ``sys.displayhook``, which is a callable that can do with
935 this value whatever it wants. The default behavior of ``sys.displayhook`` in
934 this value whatever it wants. The default behavior of ``sys.displayhook`` in
936 the Python interactive prompt is to print to ``sys.stdout`` the :func:`repr` of
935 the Python interactive prompt is to print to ``sys.stdout`` the :func:`repr` of
937 the value as long as it is not ``None`` (which isn't printed at all). In our
936 the value as long as it is not ``None`` (which isn't printed at all). In our
938 case, the kernel instantiates as ``sys.displayhook`` an object which has
937 case, the kernel instantiates as ``sys.displayhook`` an object which has
939 similar behavior, but which instead of printing to stdout, broadcasts these
938 similar behavior, but which instead of printing to stdout, broadcasts these
940 values as ``pyout`` messages for clients to display appropriately.
939 values as ``pyout`` messages for clients to display appropriately.
941
940
942 IPython's displayhook can handle multiple simultaneous formats depending on its
941 IPython's displayhook can handle multiple simultaneous formats depending on its
943 configuration. The default pretty-printed repr text is always given with the
942 configuration. The default pretty-printed repr text is always given with the
944 ``data`` entry in this message. Any other formats are provided in the
943 ``data`` entry in this message. Any other formats are provided in the
945 ``extra_formats`` list. Frontends are free to display any or all of these
944 ``extra_formats`` list. Frontends are free to display any or all of these
946 according to its capabilities. ``extra_formats`` list contains 3-tuples of an ID
945 according to its capabilities. ``extra_formats`` list contains 3-tuples of an ID
947 string, a type string, and the data. The ID is unique to the formatter
946 string, a type string, and the data. The ID is unique to the formatter
948 implementation that created the data. Frontends will typically ignore the ID
947 implementation that created the data. Frontends will typically ignore the ID
949 unless if it has requested a particular formatter. The type string tells the
948 unless if it has requested a particular formatter. The type string tells the
950 frontend how to interpret the data. It is often, but not always a MIME type.
949 frontend how to interpret the data. It is often, but not always a MIME type.
951 Frontends should ignore types that it does not understand. The data itself is
950 Frontends should ignore types that it does not understand. The data itself is
952 any JSON object and depends on the format. It is often, but not always a string.
951 any JSON object and depends on the format. It is often, but not always a string.
953
952
954 Message type: ``pyout``::
953 Message type: ``pyout``::
955
954
956 content = {
955 content = {
957
956
958 # The counter for this execution is also provided so that clients can
957 # The counter for this execution is also provided so that clients can
959 # display it, since IPython automatically creates variables called _N
958 # display it, since IPython automatically creates variables called _N
960 # (for prompt N).
959 # (for prompt N).
961 'execution_count' : int,
960 'execution_count' : int,
962
961
963 # data and metadata are identical to a display_data message.
962 # data and metadata are identical to a display_data message.
964 # the object being displayed is that passed to the display hook,
963 # the object being displayed is that passed to the display hook,
965 # i.e. the *result* of the execution.
964 # i.e. the *result* of the execution.
966 'data' : dict,
965 'data' : dict,
967 'metadata' : dict,
966 'metadata' : dict,
968 }
967 }
969
968
970 Python errors
969 Python errors
971 -------------
970 -------------
972
971
973 When an error occurs during code execution
972 When an error occurs during code execution
974
973
975 Message type: ``pyerr``::
974 Message type: ``pyerr``::
976
975
977 content = {
976 content = {
978 # Similar content to the execute_reply messages for the 'error' case,
977 # Similar content to the execute_reply messages for the 'error' case,
979 # except the 'status' field is omitted.
978 # except the 'status' field is omitted.
980 }
979 }
981
980
982 Kernel status
981 Kernel status
983 -------------
982 -------------
984
983
985 This message type is used by frontends to monitor the status of the kernel.
984 This message type is used by frontends to monitor the status of the kernel.
986
985
987 Message type: ``status``::
986 Message type: ``status``::
988
987
989 content = {
988 content = {
990 # When the kernel starts to execute code, it will enter the 'busy'
989 # When the kernel starts to execute code, it will enter the 'busy'
991 # state and when it finishes, it will enter the 'idle' state.
990 # state and when it finishes, it will enter the 'idle' state.
992 # The kernel will publish state 'starting' exactly once at process startup.
991 # The kernel will publish state 'starting' exactly once at process startup.
993 execution_state : ('busy', 'idle', 'starting')
992 execution_state : ('busy', 'idle', 'starting')
994 }
993 }
995
994
996 Clear output
995 Clear output
997 ------------
996 ------------
998
997
999 This message type is used to clear the output that is visible on the frontend.
998 This message type is used to clear the output that is visible on the frontend.
1000
999
1001 Message type: ``clear_output``::
1000 Message type: ``clear_output``::
1002
1001
1003 content = {
1002 content = {
1004
1003
1005 # Wait to clear the output until new output is available. Clears the
1004 # Wait to clear the output until new output is available. Clears the
1006 # existing output immediately before the new output is displayed.
1005 # existing output immediately before the new output is displayed.
1007 # Useful for creating simple animations with minimal flickering.
1006 # Useful for creating simple animations with minimal flickering.
1008 'wait' : bool,
1007 'wait' : bool,
1009 }
1008 }
1010
1009
1011 .. versionchanged:: 4.1
1010 .. versionchanged:: 4.1
1012
1011
1013 'stdout', 'stderr', and 'display' boolean keys for selective clearing are removed,
1012 'stdout', 'stderr', and 'display' boolean keys for selective clearing are removed,
1014 and 'wait' is added.
1013 and 'wait' is added.
1015 The selective clearing keys are ignored in v4 and the default behavior remains the same,
1014 The selective clearing keys are ignored in v4 and the default behavior remains the same,
1016 so v4 clear_output messages will be safely handled by a v4.1 frontend.
1015 so v4 clear_output messages will be safely handled by a v4.1 frontend.
1017
1016
1018
1017
1019 Messages on the stdin ROUTER/DEALER sockets
1018 Messages on the stdin ROUTER/DEALER sockets
1020 ===========================================
1019 ===========================================
1021
1020
1022 This is a socket where the request/reply pattern goes in the opposite direction:
1021 This is a socket where the request/reply pattern goes in the opposite direction:
1023 from the kernel to a *single* frontend, and its purpose is to allow
1022 from the kernel to a *single* frontend, and its purpose is to allow
1024 ``raw_input`` and similar operations that read from ``sys.stdin`` on the kernel
1023 ``raw_input`` and similar operations that read from ``sys.stdin`` on the kernel
1025 to be fulfilled by the client. The request should be made to the frontend that
1024 to be fulfilled by the client. The request should be made to the frontend that
1026 made the execution request that prompted ``raw_input`` to be called. For now we
1025 made the execution request that prompted ``raw_input`` to be called. For now we
1027 will keep these messages as simple as possible, since they only mean to convey
1026 will keep these messages as simple as possible, since they only mean to convey
1028 the ``raw_input(prompt)`` call.
1027 the ``raw_input(prompt)`` call.
1029
1028
1030 Message type: ``input_request``::
1029 Message type: ``input_request``::
1031
1030
1032 content = { 'prompt' : str }
1031 content = { 'prompt' : str }
1033
1032
1034 Message type: ``input_reply``::
1033 Message type: ``input_reply``::
1035
1034
1036 content = { 'value' : str }
1035 content = { 'value' : str }
1037
1036
1038 .. note::
1037 .. note::
1039
1038
1040 The stdin socket of the client is required to have the same zmq IDENTITY
1039 The stdin socket of the client is required to have the same zmq IDENTITY
1041 as the client's shell socket.
1040 as the client's shell socket.
1042 Because of this, the ``input_request`` must be sent with the same IDENTITY
1041 Because of this, the ``input_request`` must be sent with the same IDENTITY
1043 routing prefix as the ``execute_reply`` in order for the frontend to receive
1042 routing prefix as the ``execute_reply`` in order for the frontend to receive
1044 the message.
1043 the message.
1045
1044
1046 .. note::
1045 .. note::
1047
1046
1048 We do not explicitly try to forward the raw ``sys.stdin`` object, because in
1047 We do not explicitly try to forward the raw ``sys.stdin`` object, because in
1049 practice the kernel should behave like an interactive program. When a
1048 practice the kernel should behave like an interactive program. When a
1050 program is opened on the console, the keyboard effectively takes over the
1049 program is opened on the console, the keyboard effectively takes over the
1051 ``stdin`` file descriptor, and it can't be used for raw reading anymore.
1050 ``stdin`` file descriptor, and it can't be used for raw reading anymore.
1052 Since the IPython kernel effectively behaves like a console program (albeit
1051 Since the IPython kernel effectively behaves like a console program (albeit
1053 one whose "keyboard" is actually living in a separate process and
1052 one whose "keyboard" is actually living in a separate process and
1054 transported over the zmq connection), raw ``stdin`` isn't expected to be
1053 transported over the zmq connection), raw ``stdin`` isn't expected to be
1055 available.
1054 available.
1056
1055
1057
1056
1058 Heartbeat for kernels
1057 Heartbeat for kernels
1059 =====================
1058 =====================
1060
1059
1061 Initially we had considered using messages like those above over ZMQ for a
1060 Initially we had considered using messages like those above over ZMQ for a
1062 kernel 'heartbeat' (a way to detect quickly and reliably whether a kernel is
1061 kernel 'heartbeat' (a way to detect quickly and reliably whether a kernel is
1063 alive at all, even if it may be busy executing user code). But this has the
1062 alive at all, even if it may be busy executing user code). But this has the
1064 problem that if the kernel is locked inside extension code, it wouldn't execute
1063 problem that if the kernel is locked inside extension code, it wouldn't execute
1065 the python heartbeat code. But it turns out that we can implement a basic
1064 the python heartbeat code. But it turns out that we can implement a basic
1066 heartbeat with pure ZMQ, without using any Python messaging at all.
1065 heartbeat with pure ZMQ, without using any Python messaging at all.
1067
1066
1068 The monitor sends out a single zmq message (right now, it is a str of the
1067 The monitor sends out a single zmq message (right now, it is a str of the
1069 monitor's lifetime in seconds), and gets the same message right back, prefixed
1068 monitor's lifetime in seconds), and gets the same message right back, prefixed
1070 with the zmq identity of the DEALER socket in the heartbeat process. This can be
1069 with the zmq identity of the DEALER socket in the heartbeat process. This can be
1071 a uuid, or even a full message, but there doesn't seem to be a need for packing
1070 a uuid, or even a full message, but there doesn't seem to be a need for packing
1072 up a message when the sender and receiver are the exact same Python object.
1071 up a message when the sender and receiver are the exact same Python object.
1073
1072
1074 The model is this::
1073 The model is this::
1075
1074
1076 monitor.send(str(self.lifetime)) # '1.2345678910'
1075 monitor.send(str(self.lifetime)) # '1.2345678910'
1077
1076
1078 and the monitor receives some number of messages of the form::
1077 and the monitor receives some number of messages of the form::
1079
1078
1080 ['uuid-abcd-dead-beef', '1.2345678910']
1079 ['uuid-abcd-dead-beef', '1.2345678910']
1081
1080
1082 where the first part is the zmq.IDENTITY of the heart's DEALER on the engine, and
1081 where the first part is the zmq.IDENTITY of the heart's DEALER on the engine, and
1083 the rest is the message sent by the monitor. No Python code ever has any
1082 the rest is the message sent by the monitor. No Python code ever has any
1084 access to the message between the monitor's send, and the monitor's recv.
1083 access to the message between the monitor's send, and the monitor's recv.
1085
1084
1086 Custom Messages
1085 Custom Messages
1087 ===============
1086 ===============
1088
1087
1089 .. versionadded:: 4.1
1088 .. versionadded:: 4.1
1090
1089
1091 IPython 2.0 (msgspec v4.1) adds a messaging system for developers to add their own objects with Frontend
1090 IPython 2.0 (msgspec v4.1) adds a messaging system for developers to add their own objects with Frontend
1092 and Kernel-side components, and allow them to communicate with each other.
1091 and Kernel-side components, and allow them to communicate with each other.
1093 To do this, IPython adds a notion of a ``Comm``, which exists on both sides,
1092 To do this, IPython adds a notion of a ``Comm``, which exists on both sides,
1094 and can communicate in either direction.
1093 and can communicate in either direction.
1095
1094
1096 These messages are fully symmetrical - both the Kernel and the Frontend can send each message,
1095 These messages are fully symmetrical - both the Kernel and the Frontend can send each message,
1097 and no messages expect a reply.
1096 and no messages expect a reply.
1098 The Kernel listens for these messages on the Shell channel,
1097 The Kernel listens for these messages on the Shell channel,
1099 and the Frontend listens for them on the IOPub channel.
1098 and the Frontend listens for them on the IOPub channel.
1100
1099
1101 Opening a Comm
1100 Opening a Comm
1102 --------------
1101 --------------
1103
1102
1104 Opening a Comm produces a ``comm_open`` message, to be sent to the other side::
1103 Opening a Comm produces a ``comm_open`` message, to be sent to the other side::
1105
1104
1106 {
1105 {
1107 'comm_id' : 'u-u-i-d',
1106 'comm_id' : 'u-u-i-d',
1108 'target_name' : 'my_comm',
1107 'target_name' : 'my_comm',
1109 'data' : {}
1108 'data' : {}
1110 }
1109 }
1111
1110
1112 Every Comm has an ID and a target name.
1111 Every Comm has an ID and a target name.
1113 The code handling the message on the receiving side is responsible for maintaining a mapping
1112 The code handling the message on the receiving side is responsible for maintaining a mapping
1114 of target_name keys to constructors.
1113 of target_name keys to constructors.
1115 After a ``comm_open`` message has been sent,
1114 After a ``comm_open`` message has been sent,
1116 there should be a corresponding Comm instance on both sides.
1115 there should be a corresponding Comm instance on both sides.
1117 The ``data`` key is always a dict and can be any extra JSON information used in initialization of the comm.
1116 The ``data`` key is always a dict and can be any extra JSON information used in initialization of the comm.
1118
1117
1119 If the ``target_name`` key is not found on the receiving side,
1118 If the ``target_name`` key is not found on the receiving side,
1120 then it should immediately reply with a ``comm_close`` message to avoid an inconsistent state.
1119 then it should immediately reply with a ``comm_close`` message to avoid an inconsistent state.
1121
1120
1122 Comm Messages
1121 Comm Messages
1123 -------------
1122 -------------
1124
1123
1125 Comm messages are one-way communications to update comm state,
1124 Comm messages are one-way communications to update comm state,
1126 used for synchronizing widget state, or simply requesting actions of a comm's counterpart.
1125 used for synchronizing widget state, or simply requesting actions of a comm's counterpart.
1127
1126
1128 Essentially, each comm pair defines their own message specification implemented inside the ``data`` dict.
1127 Essentially, each comm pair defines their own message specification implemented inside the ``data`` dict.
1129
1128
1130 There are no expected replies (of course, one side can send another ``comm_msg`` in reply).
1129 There are no expected replies (of course, one side can send another ``comm_msg`` in reply).
1131
1130
1132 Message type: ``comm_msg``::
1131 Message type: ``comm_msg``::
1133
1132
1134 {
1133 {
1135 'comm_id' : 'u-u-i-d',
1134 'comm_id' : 'u-u-i-d',
1136 'data' : {}
1135 'data' : {}
1137 }
1136 }
1138
1137
1139 Tearing Down Comms
1138 Tearing Down Comms
1140 ------------------
1139 ------------------
1141
1140
1142 Since comms live on both sides, when a comm is destroyed the other side must be notified.
1141 Since comms live on both sides, when a comm is destroyed the other side must be notified.
1143 This is done with a ``comm_close`` message.
1142 This is done with a ``comm_close`` message.
1144
1143
1145 Message type: ``comm_close``::
1144 Message type: ``comm_close``::
1146
1145
1147 {
1146 {
1148 'comm_id' : 'u-u-i-d',
1147 'comm_id' : 'u-u-i-d',
1149 'data' : {}
1148 'data' : {}
1150 }
1149 }
1151
1150
1152 Output Side Effects
1151 Output Side Effects
1153 -------------------
1152 -------------------
1154
1153
1155 Since comm messages can execute arbitrary user code,
1154 Since comm messages can execute arbitrary user code,
1156 handlers should set the parent header and publish status busy / idle,
1155 handlers should set the parent header and publish status busy / idle,
1157 just like an execute request.
1156 just like an execute request.
1158
1157
1159
1158
1160 ToDo
1159 ToDo
1161 ====
1160 ====
1162
1161
1163 Missing things include:
1162 Missing things include:
1164
1163
1165 * Important: finish thinking through the payload concept and API.
1164 * Important: finish thinking through the payload concept and API.
1166
1165
1167 * Important: ensure that we have a good solution for magics like %edit. It's
1166 * Important: ensure that we have a good solution for magics like %edit. It's
1168 likely that with the payload concept we can build a full solution, but not
1167 likely that with the payload concept we can build a full solution, but not
1169 100% clear yet.
1168 100% clear yet.
1170
1169
1171 .. include:: ../links.txt
1170 .. include:: ../links.txt
General Comments 0
You need to be logged in to leave comments. Login now