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