| 1 |
.. _messaging: |
| 2 |
|
| 3 |
====================== |
| 4 |
Messaging in Jupyter |
| 5 |
====================== |
| 6 |
|
| 7 |
This document explains the basic communications design and messaging |
| 8 |
specification for how Jupyter frontends and kernels communicate. |
| 9 |
The ZeroMQ_ library provides the low-level transport layer over which these |
| 10 |
messages are sent. |
| 11 |
|
| 12 |
.. important:: |
| 13 |
This document contains the authoritative description of the |
| 14 |
IPython messaging protocol. All developers are strongly encouraged to |
| 15 |
keep it updated as the implementation evolves, so that we have a single |
| 16 |
common reference for all protocol details. |
| 17 |
|
| 18 |
|
| 19 |
Versioning |
| 20 |
========== |
| 21 |
|
| 22 |
The Jupyter message specification is versioned independently of the packages |
| 23 |
that use it. |
| 24 |
The current version of the specification is 5.5. |
| 25 |
|
| 26 |
.. note:: |
| 27 |
*New in* and *Changed in* messages in this document refer to versions of the |
| 28 |
**Jupyter message specification**, not versions of :mod:`jupyter_client`. |
| 29 |
|
| 30 |
Introduction |
| 31 |
============ |
| 32 |
|
| 33 |
The basic design is explained in the following diagram: |
| 34 |
|
| 35 |
.. image:: figs/frontend-kernel.png |
| 36 |
:width: 450px |
| 37 |
:alt: IPython kernel/frontend messaging architecture. |
| 38 |
:align: center |
| 39 |
:target: ./figs/frontend-kernel.png |
| 40 |
|
| 41 |
A single kernel can be simultaneously connected to one or more frontends. The |
| 42 |
kernel has dedicated sockets for the following functions: |
| 43 |
|
| 44 |
1. **Shell**: this single ROUTER socket allows multiple incoming connections from |
| 45 |
frontends, and this is the socket where requests for code execution, object |
| 46 |
information, prompts, etc. are made to the kernel by any frontend. The |
| 47 |
communication on this socket is a sequence of request/reply actions from |
| 48 |
each frontend and the kernel. |
| 49 |
|
| 50 |
2. **IOPub**: this socket is the 'broadcast channel' where the kernel publishes all |
| 51 |
side effects (stdout, stderr, debugging events etc.) as well as the requests |
| 52 |
coming from any client over the shell socket and its own requests on the |
| 53 |
stdin socket. There are a number of actions in Python which generate side |
| 54 |
effects: :func:`print` writes to ``sys.stdout``, errors generate tracebacks, |
| 55 |
etc. Additionally, in a multi-client scenario, we want all frontends to be |
| 56 |
able to know what each other has sent to the kernel (this can be useful in |
| 57 |
collaborative scenarios, for example). This socket allows both side effects |
| 58 |
and the information about communications taking place with one client over |
| 59 |
the shell channel to be made available to all clients in a uniform manner. |
| 60 |
|
| 61 |
3. **stdin**: this ROUTER socket is connected to all frontends, and it allows |
| 62 |
the kernel to request input from the active frontend when :func:`raw_input` is called. |
| 63 |
The frontend that executed the code has a DEALER socket that acts as a 'virtual keyboard' |
| 64 |
for the kernel while this communication is happening (illustrated in the |
| 65 |
figure by the black outline around the central keyboard). In practice, |
| 66 |
frontends may display such kernel requests using a special input widget or |
| 67 |
otherwise indicating that the user is to type input for the kernel instead |
| 68 |
of normal commands in the frontend. |
| 69 |
|
| 70 |
All messages are tagged with enough information (details below) for clients |
| 71 |
to know which messages come from their own interaction with the kernel and |
| 72 |
which ones are from other clients, so they can display each type |
| 73 |
appropriately. |
| 74 |
|
| 75 |
4. **Control**: This channel is identical to Shell, but operates on a separate |
| 76 |
socket to avoid queueing behind execution requests. The control channel is |
| 77 |
used for shutdown and restart messages, as well as for debugging messages. |
| 78 |
|
| 79 |
For a smoother user experience, we recommend running the control channel in |
| 80 |
a separate thread from the shell channel, so that e.g. shutdown or debug |
| 81 |
messages can be processed immediately without waiting for a long-running |
| 82 |
shell message to be finished processing (such as an expensive execute request). |
| 83 |
|
| 84 |
5. **Heartbeat**: This socket allows for simple bytestring messages to be sent |
| 85 |
between the frontend and the kernel to ensure that they are still connected. |
| 86 |
|
| 87 |
.. versionchanged:: 5.5 |
| 88 |
The **IOPub** PUB socket is replaced with an XPUB socket, |
| 89 |
to enable the ``iopub_welcome`` message. |
| 90 |
There is no other difference between kernel PUB and XPUB sockets from the client perspective. |
| 91 |
|
| 92 |
The actual format of the messages allowed on each of these channels is |
| 93 |
specified below. Messages are dicts of dicts with string keys and values that |
| 94 |
are reasonably representable in JSON. |
| 95 |
|
| 96 |
General Message Format |
| 97 |
====================== |
| 98 |
|
| 99 |
A message is composed of five dictionaries. |
| 100 |
|
| 101 |
Message Header |
| 102 |
-------------- |
| 103 |
|
| 104 |
The message ``header`` contains information about the message, |
| 105 |
such as unique identifiers for the originating session and the actual message ID, |
| 106 |
the type of message, the version of the Jupyter protocol, |
| 107 |
and the date the message was created. |
| 108 |
In addition, there is a username field, e.g. for the process that generated the |
| 109 |
message, if applicable. |
| 110 |
This can be useful in collaborative settings where multiple users may be |
| 111 |
interacting with the same kernel simultaneously, |
| 112 |
so that frontends can label the various messages in a meaningful way. |
| 113 |
|
| 114 |
.. sourcecode:: python |
| 115 |
|
| 116 |
{ |
| 117 |
"msg_id": str, # typically UUID, must be unique per message |
| 118 |
"session": str, # typically UUID, should be unique per session |
| 119 |
"username": str, |
| 120 |
# ISO 8601 timestamp for when the message is created |
| 121 |
"date": str, |
| 122 |
# All recognized message type strings are listed below. |
| 123 |
"msg_type": str, |
| 124 |
# the message protocol version |
| 125 |
"version": "5.0", |
| 126 |
# Optional subshell_id |
| 127 |
"subshell_id": str | None, |
| 128 |
} |
| 129 |
|
| 130 |
.. note:: |
| 131 |
|
| 132 |
The ``session`` ID in a message header identifies a unique entity with state, |
| 133 |
such as a kernel process or client process. |
| 134 |
|
| 135 |
A client session ID, in message headers from a client, should be unique among |
| 136 |
all clients connected to a kernel. When a client reconnects to a kernel, it |
| 137 |
should use the same client session ID in its message headers. When a client |
| 138 |
restarts, it should generate a new client session ID. |
| 139 |
|
| 140 |
A kernel session ID, in message headers from a kernel, should identify a |
| 141 |
particular kernel process. If a kernel is restarted, the kernel session ID |
| 142 |
should be regenerated. |
| 143 |
|
| 144 |
The session ID in a message header can be used to identify the sending entity. |
| 145 |
For example, if a client disconnects and reconnects to a kernel, and messages |
| 146 |
from the kernel have a different kernel session ID than prior to the disconnect, |
| 147 |
the client should assume that the kernel was restarted. |
| 148 |
|
| 149 |
The ``subshell_id`` is only used in shell messages of kernels that support |
| 150 |
subshells (:ref:`kernel_subshells`). If it is not included or is ``None`` then the |
| 151 |
shell message is handled by the parent subshell (main shell), if it is a string |
| 152 |
subshell ID then it is handled by the subshell with that ID. |
| 153 |
|
| 154 |
.. versionchanged:: 5.0 |
| 155 |
|
| 156 |
``version`` key added to the header. |
| 157 |
|
| 158 |
.. versionchanged:: 5.1 |
| 159 |
|
| 160 |
``date`` in the header was accidentally omitted from the spec prior to 5.1, |
| 161 |
but it has always been in the canonical implementation, |
| 162 |
so implementers are strongly encouraged to include it. |
| 163 |
It will be mandatory in 5.1. |
| 164 |
|
| 165 |
.. versionchanged:: 5.5 |
| 166 |
|
| 167 |
``subshell_id`` added to the header. |
| 168 |
|
| 169 |
Parent header |
| 170 |
------------- |
| 171 |
|
| 172 |
When a message is the "result" of another message, |
| 173 |
such as a side-effect (output or status) or direct reply, |
| 174 |
the ``parent_header`` is a copy of the ``header`` of the message |
| 175 |
that "caused" the current message. |
| 176 |
``_reply`` messages MUST have a ``parent_header``, |
| 177 |
and side-effects *typically* have a parent. |
| 178 |
If there is no parent, an empty dict should be used. |
| 179 |
This parent is used by clients to route message handling to the right place, |
| 180 |
such as outputs to a cell. |
| 181 |
|
| 182 |
.. sourcecode:: |
| 183 |
|
| 184 |
{ |
| 185 |
# parent_header is a copy of the request's header |
| 186 |
'msg_id': '...', |
| 187 |
... |
| 188 |
} |
| 189 |
|
| 190 |
Metadata |
| 191 |
-------- |
| 192 |
|
| 193 |
The ``metadata`` dict contains information about the message that is not part of the content. |
| 194 |
This is not often used, but can be an extra location to store information about |
| 195 |
requests and replies, |
| 196 |
such as extensions adding information about request or execution context. |
| 197 |
|
| 198 |
Content |
| 199 |
------- |
| 200 |
|
| 201 |
The ``content`` dict is the body of the message. |
| 202 |
Its structure is dictated by the ``msg_type`` field in the header, |
| 203 |
described in detail for each message below. |
| 204 |
|
| 205 |
Buffers |
| 206 |
------- |
| 207 |
|
| 208 |
Finally, a list of additional binary buffers can be associated with a message. |
| 209 |
While this is part of the protocol, |
| 210 |
no official messages make use of these buffers. |
| 211 |
They are used by extension messages, such as IPython Parallel's ``apply`` |
| 212 |
and some of ipywidgets' ``comm`` messages. |
| 213 |
|
| 214 |
A full message |
| 215 |
-------------- |
| 216 |
|
| 217 |
Combining all of these together, |
| 218 |
a complete message can be represented as the following dictionary of dictionaries (and one list):: |
| 219 |
|
| 220 |
{ |
| 221 |
"header" : { |
| 222 |
"msg_id": "...", |
| 223 |
"msg_type": "...", |
| 224 |
... |
| 225 |
}, |
| 226 |
"parent_header": {}, |
| 227 |
"metadata": {}, |
| 228 |
"content": {}, |
| 229 |
"buffers": [], |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
.. note:: |
| 234 |
|
| 235 |
This dictionary structure is *not* part of the Jupyter protocol |
| 236 |
that must be implemented by kernels and frontends; |
| 237 |
that would be :ref:`wire_protocol`, |
| 238 |
which dictates how this information is serialized over the wire. |
| 239 |
Deserialization is up to the Kernel or frontend implementation, |
| 240 |
but a dict like this would be a logical choice in most contexts. |
| 241 |
|
| 242 |
.. _msging_compatibility: |
| 243 |
|
| 244 |
Compatibility |
| 245 |
============= |
| 246 |
|
| 247 |
Kernels must implement the :ref:`execute <execute>` and :ref:`kernel info |
| 248 |
<msging_kernel_info>` messages, along with the associated busy and idle |
| 249 |
:ref:`status` messages. All other message types are |
| 250 |
optional, although we recommend implementing :ref:`completion |
| 251 |
<msging_completion>` if possible. Kernels do not need to send any reply for |
| 252 |
messages they don't handle, and frontends should provide sensible behaviour if |
| 253 |
no reply arrives (except for the required execution and kernel info messages). |
| 254 |
|
| 255 |
:ref:`stdin messages <stdin_messages>` are unique in that the request comes from |
| 256 |
the kernel, and the reply from the frontend. The frontend is not required to |
| 257 |
support this, but if it does not, it must set ``'allow_stdin' : False`` in |
| 258 |
its :ref:`execute requests <execute>`. In this case, the kernel may not send |
| 259 |
stdin requests. If that field is true, the kernel may send stdin requests and |
| 260 |
block waiting for a reply, so the frontend must answer. |
| 261 |
|
| 262 |
Both sides should allow unexpected message types, and extra fields in known |
| 263 |
message types, so that additions to the protocol do not break existing code. |
| 264 |
|
| 265 |
.. _wire_protocol: |
| 266 |
|
| 267 |
The Wire Protocol |
| 268 |
================= |
| 269 |
|
| 270 |
|
| 271 |
The above message format is only a logical representation of the contents of Jupyter messages, |
| 272 |
but does not describe the actual *implementation* at the wire level in zeromq. |
| 273 |
This section describes the protocol that must be implemented by Jupyter kernels and clients |
| 274 |
talking to each other over zeromq. |
| 275 |
|
| 276 |
The reference implementation of the message spec is our |
| 277 |
:class:`~jupyter_client.session.Session` class. |
| 278 |
|
| 279 |
.. note:: |
| 280 |
|
| 281 |
This section should only be relevant to non-Python consumers of the protocol. |
| 282 |
Python consumers should import and the use implementation of the wire |
| 283 |
protocol in :class:`jupyter_client.session.Session`. |
| 284 |
|
| 285 |
Every message is serialized to a sequence of at least six blobs of bytes: |
| 286 |
|
| 287 |
.. sourcecode:: python |
| 288 |
|
| 289 |
[ |
| 290 |
b"u-u-i-d", # zmq identity(ies) |
| 291 |
b"<IDS|MSG>", # delimiter |
| 292 |
b"baddad42", # HMAC signature |
| 293 |
b"{header}", # serialized header dict |
| 294 |
b"{parent_header}", # serialized parent header dict |
| 295 |
b"{metadata}", # serialized metadata dict |
| 296 |
b"{content}", # serialized content dict |
| 297 |
b"\xf0\x9f\x90\xb1" # extra raw data buffer(s) |
| 298 |
# ... |
| 299 |
] |
| 300 |
|
| 301 |
The front of the message is the ZeroMQ routing prefix, |
| 302 |
which can be zero or more socket identities. |
| 303 |
This is every piece of the message prior to the delimiter key ``<IDS|MSG>``. |
| 304 |
In the case of IOPub, there should be just one prefix component, |
| 305 |
which is the topic for IOPub subscribers, e.g. ``execute_result``, ``display_data``. |
| 306 |
|
| 307 |
.. note:: |
| 308 |
|
| 309 |
In most cases, the IOPub topics are irrelevant and completely ignored, |
| 310 |
because frontends just subscribe to all topics. |
| 311 |
The convention used in the IPython kernel is to use the msg_type as the topic, |
| 312 |
and possibly extra information about the message, e.g. ``kernel.{u-u-i-d}.execute_result`` or ``stream.stdout`` |
| 313 |
|
| 314 |
After the delimiter is the `HMAC`_ signature of the message, used for authentication. |
| 315 |
If authentication is disabled, this should be an empty string. |
| 316 |
By default, the hashing function used for computing these signatures is sha256. |
| 317 |
|
| 318 |
.. _HMAC: https://en.wikipedia.org/wiki/HMAC |
| 319 |
|
| 320 |
.. note:: |
| 321 |
|
| 322 |
To disable authentication and signature checking, |
| 323 |
set the ``key`` field of a connection file to an empty string. |
| 324 |
|
| 325 |
The signature is the HMAC hex digest of the concatenation of: |
| 326 |
|
| 327 |
- A shared key (typically the ``key`` field of a connection file) |
| 328 |
- The serialized header dict |
| 329 |
- The serialized parent header dict |
| 330 |
- The serialized metadata dict |
| 331 |
- The serialized content dict |
| 332 |
|
| 333 |
In Python, this is implemented via: |
| 334 |
|
| 335 |
.. sourcecode:: python |
| 336 |
|
| 337 |
# once: |
| 338 |
digester = HMAC(key, digestmod=hashlib.sha256) |
| 339 |
|
| 340 |
# for each message |
| 341 |
d = digester.copy() |
| 342 |
for serialized_dict in (header, parent, metadata, content): |
| 343 |
d.update(serialized_dict) |
| 344 |
signature = d.hexdigest() |
| 345 |
|
| 346 |
After the signature is the actual message, always in four frames of bytes. |
| 347 |
The four dictionaries that compose a message are serialized separately, |
| 348 |
in the order of header, parent header, metadata, and content. |
| 349 |
These can be serialized by any function that turns a dict into bytes. |
| 350 |
The default and most common serialization is JSON, but msgpack and pickle |
| 351 |
are common alternatives. |
| 352 |
|
| 353 |
After the serialized dicts are zero to many raw data buffers, |
| 354 |
which can be used by message types that support binary data, |
| 355 |
which can be used in custom messages, such as comms and extensions to the protocol. |
| 356 |
|
| 357 |
|
| 358 |
Python API |
| 359 |
========== |
| 360 |
|
| 361 |
As messages can be represented as dicts, they map naturally to a ``func(**kw)`` call form. We |
| 362 |
should develop, at a few key points, functional forms of all the requests that |
| 363 |
take arguments in this manner and automatically construct the necessary dict |
| 364 |
for sending. |
| 365 |
|
| 366 |
In addition, the Python implementation of the message specification extends |
| 367 |
messages upon deserialization to the following form for convenience:: |
| 368 |
|
| 369 |
{ |
| 370 |
'header' : dict, |
| 371 |
# The msg's unique identifier and type are always stored in the header, |
| 372 |
# but the Python implementation copies them to the top level. |
| 373 |
'msg_id' : str, |
| 374 |
'msg_type' : str, |
| 375 |
'parent_header' : dict, |
| 376 |
'content' : dict, |
| 377 |
'metadata' : dict, |
| 378 |
'buffers': list, |
| 379 |
} |
| 380 |
|
| 381 |
All messages sent to or received by any IPython message handler should have this |
| 382 |
extended structure. |
| 383 |
|
| 384 |
|
| 385 |
Messages on the shell (ROUTER/DEALER) channel |
| 386 |
============================================= |
| 387 |
|
| 388 |
.. _request_reply: |
| 389 |
|
| 390 |
Request-Reply |
| 391 |
------------- |
| 392 |
|
| 393 |
In general, the ROUTER/DEALER sockets follow a request-reply pattern: |
| 394 |
|
| 395 |
The client sends an ``<action>_request`` message (such as ``execute_request``) |
| 396 |
on its shell (DEALER) socket. |
| 397 |
The kernel receives that request and immediately publishes a ``status: busy`` message on IOPub. |
| 398 |
The kernel then processes the request and sends the appropriate |
| 399 |
``<action>_reply`` message, such as ``execute_reply``. |
| 400 |
After processing the request and publishing associated IOPub messages, if any, |
| 401 |
the kernel publishes a ``status: idle`` message. |
| 402 |
This idle status message indicates that IOPub messages associated with a given |
| 403 |
request have all been received. |
| 404 |
|
| 405 |
All reply messages have a ``'status'`` field, which will have one of the following values: |
| 406 |
|
| 407 |
- ``status='ok'``: The request was processed successfully, and the remaining |
| 408 |
content of the reply is specified in the appropriate section below. |
| 409 |
- ``status='error'``: The request failed due to an error. |
| 410 |
When status is 'error', the usual content of a successful reply should be omitted, |
| 411 |
instead the following fields should be present:: |
| 412 |
|
| 413 |
{ |
| 414 |
'status' : 'error', |
| 415 |
'ename' : str, # Exception name, as a string |
| 416 |
'evalue' : str, # Exception value, as a string |
| 417 |
'traceback' : list(str), # traceback frames as strings |
| 418 |
} |
| 419 |
|
| 420 |
- ``status='abort'``: This is the same as ``status='error'`` |
| 421 |
but with no information about the error. |
| 422 |
No fields should be present other that ``status``. |
| 423 |
|
| 424 |
As a special case, ``execute_reply`` messages (see :ref:`execution_results`) |
| 425 |
have an ``execution_count`` field regardless of their status. |
| 426 |
|
| 427 |
.. versionchanged:: 5.1 |
| 428 |
|
| 429 |
``status='abort'`` has not proved useful, and is considered deprecated. |
| 430 |
Kernels should send ``status='error'`` instead. |
| 431 |
|
| 432 |
|
| 433 |
.. _execute: |
| 434 |
|
| 435 |
Execute |
| 436 |
------- |
| 437 |
|
| 438 |
This message type is used by frontends to ask the kernel to execute code on |
| 439 |
behalf of the user, in a namespace reserved to the user's variables (and thus |
| 440 |
separate from the kernel's own internal code and variables). |
| 441 |
|
| 442 |
Message type: ``execute_request``:: |
| 443 |
|
| 444 |
content = { |
| 445 |
# Source code to be executed by the kernel, one or more lines. |
| 446 |
'code' : str, |
| 447 |
|
| 448 |
# A boolean flag which, if True, signals the kernel to execute |
| 449 |
# this code as quietly as possible. |
| 450 |
# silent=True forces store_history to be False, |
| 451 |
# and will *not*: |
| 452 |
# - broadcast output on the IOPUB channel |
| 453 |
# - have an execute_result |
| 454 |
# The default is False. |
| 455 |
'silent' : bool, |
| 456 |
|
| 457 |
# A boolean flag which, if True, signals the kernel to populate history |
| 458 |
# The default is True if silent is False. If silent is True, store_history |
| 459 |
# is forced to be False. |
| 460 |
'store_history' : bool, |
| 461 |
|
| 462 |
# A dict mapping names to expressions to be evaluated in the |
| 463 |
# user's dict. The rich display-data representation of each will be evaluated after execution. |
| 464 |
# See the display_data content for the structure of the representation data. |
| 465 |
'user_expressions' : dict, |
| 466 |
|
| 467 |
# Some frontends do not support stdin requests. |
| 468 |
# If this is true, code running in the kernel can prompt the user for input |
| 469 |
# with an input_request message (see below). If it is false, the kernel |
| 470 |
# should not send these messages. |
| 471 |
'allow_stdin' : True, |
| 472 |
|
| 473 |
# A boolean flag, which, if True, aborts the execution queue if an exception is encountered. |
| 474 |
# If False, queued execute_requests will execute even if this request generates an exception. |
| 475 |
'stop_on_error' : True, |
| 476 |
} |
| 477 |
|
| 478 |
.. versionchanged:: 5.0 |
| 479 |
|
| 480 |
``user_variables`` removed, because it is redundant with user_expressions. |
| 481 |
|
| 482 |
The ``code`` field contains a single string (possibly multiline) to be executed. |
| 483 |
|
| 484 |
The ``user_expressions`` field deserves a detailed explanation. In the past, IPython had |
| 485 |
the notion of a prompt string that allowed arbitrary code to be evaluated, and |
| 486 |
this was put to good use by many in creating prompts that displayed system |
| 487 |
status, path information, and even more esoteric uses like remote instrument |
| 488 |
status acquired over the network. But now that IPython has a clean separation |
| 489 |
between the kernel and the clients, the kernel has no prompt knowledge; prompts |
| 490 |
are a frontend feature, and it should be even possible for different |
| 491 |
frontends to display different prompts while interacting with the same kernel. |
| 492 |
``user_expressions`` can be used to retrieve this information. |
| 493 |
|
| 494 |
Any error in evaluating any expression in ``user_expressions`` will result in |
| 495 |
only that key containing a standard error message, of the form:: |
| 496 |
|
| 497 |
{ |
| 498 |
'status' : 'error', |
| 499 |
'ename' : 'NameError', |
| 500 |
'evalue' : 'foo', |
| 501 |
'traceback' : ... |
| 502 |
} |
| 503 |
|
| 504 |
.. Note:: |
| 505 |
|
| 506 |
In order to obtain the current execution counter for the purposes of |
| 507 |
displaying input prompts, frontends may make an execution request with an |
| 508 |
empty code string and ``silent=True``. |
| 509 |
|
| 510 |
Upon completion of the execution request, the kernel *always* sends a reply, |
| 511 |
with a status code indicating what happened and additional data depending on |
| 512 |
the outcome. See :ref:`below <execution_results>` for the possible return |
| 513 |
codes and associated data. |
| 514 |
|
| 515 |
.. seealso:: |
| 516 |
|
| 517 |
:ref:`execution_semantics` |
| 518 |
|
| 519 |
.. _execution_counter: |
| 520 |
|
| 521 |
Execution counter (prompt number) |
| 522 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 523 |
|
| 524 |
The kernel should have a single, monotonically increasing counter of all execution |
| 525 |
requests that are made with ``store_history=True``. This counter is used to populate |
| 526 |
the ``In[n]`` and ``Out[n]`` prompts. The value of this counter will be returned as the |
| 527 |
``execution_count`` field of all ``execute_reply`` and ``execute_input`` messages. |
| 528 |
|
| 529 |
.. _execution_results: |
| 530 |
|
| 531 |
Execution results |
| 532 |
~~~~~~~~~~~~~~~~~ |
| 533 |
|
| 534 |
Message type: ``execute_reply``:: |
| 535 |
|
| 536 |
content = { |
| 537 |
# One of: 'ok' OR 'error' OR 'aborted' |
| 538 |
'status' : str, |
| 539 |
|
| 540 |
# The global kernel counter that increases by one with each request that |
| 541 |
# stores history. This will typically be used by clients to display |
| 542 |
# prompt numbers to the user. If the request did not store history, this will |
| 543 |
# be the current value of the counter in the kernel. |
| 544 |
'execution_count' : int, |
| 545 |
} |
| 546 |
|
| 547 |
When status is 'ok', the following extra fields are present:: |
| 548 |
|
| 549 |
{ |
| 550 |
# 'payload' will be a list of payload dicts, and is optional. |
| 551 |
# payloads are considered deprecated. |
| 552 |
# The only requirement of each payload dict is that it have a 'source' key, |
| 553 |
# which is a string classifying the payload (e.g. 'page'). |
| 554 |
|
| 555 |
'payload' : list(dict), |
| 556 |
|
| 557 |
# Results for the user_expressions. |
| 558 |
'user_expressions' : dict, |
| 559 |
} |
| 560 |
|
| 561 |
.. versionchanged:: 5.0 |
| 562 |
|
| 563 |
``user_variables`` is removed, use user_expressions instead. |
| 564 |
|
| 565 |
|
| 566 |
Payloads (DEPRECATED) |
| 567 |
~~~~~~~~~~~~~~~~~~~~~ |
| 568 |
|
| 569 |
.. admonition:: Execution payloads |
| 570 |
|
| 571 |
Payloads are considered **deprecated**, though their replacement is not yet implemented. |
| 572 |
|
| 573 |
Payloads are a way to trigger frontend actions from the kernel. Current payloads: |
| 574 |
|
| 575 |
**page**: display data in a pager. |
| 576 |
|
| 577 |
Pager output is used for introspection, or other displayed information that's not considered output. |
| 578 |
Pager payloads are generally displayed in a separate pane, that can be viewed alongside code, |
| 579 |
and are not included in notebook documents. |
| 580 |
|
| 581 |
.. sourcecode:: python |
| 582 |
|
| 583 |
{ |
| 584 |
"source": "page", |
| 585 |
# mime-bundle of data to display in the pager. |
| 586 |
# Must include text/plain. |
| 587 |
"data": mimebundle, |
| 588 |
# line offset to start from |
| 589 |
"start": int, |
| 590 |
} |
| 591 |
|
| 592 |
**set_next_input**: create a new output |
| 593 |
|
| 594 |
used to create new cells in the notebook, |
| 595 |
or set the next input in a console interface. |
| 596 |
The main example being ``%load``. |
| 597 |
|
| 598 |
.. sourcecode:: python |
| 599 |
|
| 600 |
{ |
| 601 |
"source": "set_next_input", |
| 602 |
# the text contents of the cell to create |
| 603 |
"text": "some cell content", |
| 604 |
# If true, replace the current cell in document UIs instead of inserting |
| 605 |
# a cell. Ignored in console UIs. |
| 606 |
"replace": bool, |
| 607 |
} |
| 608 |
|
| 609 |
**edit_magic**: open a file for editing. |
| 610 |
|
| 611 |
Triggered by ``%edit``. Only the QtConsole currently supports edit payloads. |
| 612 |
|
| 613 |
.. sourcecode:: python |
| 614 |
|
| 615 |
{ |
| 616 |
"source": "edit_magic", |
| 617 |
"filename": "/path/to/file.py", # the file to edit |
| 618 |
"line_number": int, # the line number to start with |
| 619 |
} |
| 620 |
|
| 621 |
**ask_exit**: instruct the frontend to prompt the user for exit |
| 622 |
|
| 623 |
Allows the kernel to request exit, e.g. via ``%exit`` in IPython. |
| 624 |
Only for console frontends. |
| 625 |
|
| 626 |
.. sourcecode:: python |
| 627 |
|
| 628 |
{ |
| 629 |
"source": "ask_exit", |
| 630 |
# whether the kernel should be left running, only closing the client |
| 631 |
"keepkernel": bool, |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
.. _msging_inspection: |
| 636 |
|
| 637 |
Introspection |
| 638 |
------------- |
| 639 |
|
| 640 |
Code can be inspected to show useful information to the user. |
| 641 |
It is up to the Kernel to decide what information should be displayed, and its formatting. |
| 642 |
|
| 643 |
Message type: ``inspect_request``:: |
| 644 |
|
| 645 |
content = { |
| 646 |
# The code context in which introspection is requested |
| 647 |
# this may be up to an entire multiline cell. |
| 648 |
'code' : str, |
| 649 |
|
| 650 |
# The cursor position within 'code' (in unicode characters) where inspection is requested |
| 651 |
'cursor_pos' : int, |
| 652 |
|
| 653 |
# The level of detail desired. In IPython, the default (0) is equivalent to typing |
| 654 |
# 'x?' at the prompt, 1 is equivalent to 'x??'. |
| 655 |
# The difference is up to kernels, but in IPython level 1 includes the source code |
| 656 |
# if available. |
| 657 |
'detail_level' : 0 or 1, |
| 658 |
} |
| 659 |
|
| 660 |
.. versionchanged:: 5.0 |
| 661 |
|
| 662 |
``object_info_request`` renamed to ``inspect_request``. |
| 663 |
|
| 664 |
.. versionchanged:: 5.0 |
| 665 |
|
| 666 |
``name`` key replaced with ``code`` and ``cursor_pos``, |
| 667 |
moving the lexing responsibility to the kernel. |
| 668 |
|
| 669 |
.. versionchanged:: 5.2 |
| 670 |
|
| 671 |
Due to a widespread bug in many frontends, ``cursor_pos`` |
| 672 |
in versions prior to 5.2 is ambiguous in the presence of "astral-plane" characters. |
| 673 |
In 5.2, cursor_pos **must be** the actual encoding-independent offset in unicode codepoints. |
| 674 |
See :ref:`cursor_pos_unicode_note` for more. |
| 675 |
|
| 676 |
|
| 677 |
The reply is a mime-bundle, like a `display_data`_ message, |
| 678 |
which should be a formatted representation of information about the context. |
| 679 |
In the notebook, this is used to show tooltips over function calls, etc. |
| 680 |
|
| 681 |
Message type: ``inspect_reply``:: |
| 682 |
|
| 683 |
content = { |
| 684 |
# 'ok' if the request succeeded or 'error', with error information as in all other replies. |
| 685 |
'status' : 'ok', |
| 686 |
|
| 687 |
# found should be true if an object was found, false otherwise |
| 688 |
'found' : bool, |
| 689 |
|
| 690 |
# data can be empty if nothing is found |
| 691 |
'data' : dict, |
| 692 |
'metadata' : dict, |
| 693 |
} |
| 694 |
|
| 695 |
.. versionchanged:: 5.0 |
| 696 |
|
| 697 |
``object_info_reply`` renamed to ``inspect_reply``. |
| 698 |
|
| 699 |
.. versionchanged:: 5.0 |
| 700 |
|
| 701 |
Reply is changed from structured data to a mime bundle, allowing formatting decisions to be made by the kernel. |
| 702 |
|
| 703 |
.. _msging_completion: |
| 704 |
|
| 705 |
Completion |
| 706 |
---------- |
| 707 |
|
| 708 |
Message type: ``complete_request``:: |
| 709 |
|
| 710 |
content = { |
| 711 |
# The code context in which completion is requested |
| 712 |
# this may be up to an entire multiline cell, such as |
| 713 |
# 'foo = a.isal' |
| 714 |
'code' : str, |
| 715 |
|
| 716 |
# The cursor position within 'code' (in unicode characters) where completion is requested |
| 717 |
'cursor_pos' : int, |
| 718 |
} |
| 719 |
|
| 720 |
.. versionchanged:: 5.0 |
| 721 |
|
| 722 |
``line``, ``block``, and ``text`` keys are removed in favor of a single ``code`` for context. |
| 723 |
Lexing is up to the kernel. |
| 724 |
|
| 725 |
.. versionchanged:: 5.2 |
| 726 |
|
| 727 |
Due to a widespread bug in many frontends, ``cursor_pos`` |
| 728 |
in versions prior to 5.2 is ambiguous in the presence of "astral-plane" characters. |
| 729 |
In 5.2, cursor_pos **must be** the actual encoding-independent offset in unicode codepoints. |
| 730 |
See :ref:`cursor_pos_unicode_note` for more. |
| 731 |
|
| 732 |
|
| 733 |
Message type: ``complete_reply``:: |
| 734 |
|
| 735 |
content = { |
| 736 |
# status should be 'ok' unless an exception was raised during the request, |
| 737 |
# in which case it should be 'error', along with the usual error message content |
| 738 |
# in other messages. |
| 739 |
'status' : 'ok' |
| 740 |
|
| 741 |
# The list of all matches to the completion request, such as |
| 742 |
# ['a.isalnum', 'a.isalpha'] for the above example. |
| 743 |
'matches' : list, |
| 744 |
|
| 745 |
# The range of text that should be replaced by the above matches when a completion is accepted. |
| 746 |
# typically cursor_end is the same as cursor_pos in the request. |
| 747 |
'cursor_start' : int, |
| 748 |
'cursor_end' : int, |
| 749 |
|
| 750 |
# Information that frontend plugins might use for extra display information about completions. |
| 751 |
'metadata' : dict, |
| 752 |
} |
| 753 |
|
| 754 |
.. versionchanged:: 5.0 |
| 755 |
|
| 756 |
- ``matched_text`` is removed in favor of ``cursor_start`` and ``cursor_end``. |
| 757 |
- ``metadata`` is added for extended information. |
| 758 |
|
| 759 |
.. _msging_history: |
| 760 |
|
| 761 |
History |
| 762 |
------- |
| 763 |
|
| 764 |
For clients to explicitly request history from a kernel. The kernel has all |
| 765 |
the actual execution history stored in a single location, so clients can |
| 766 |
request it from the kernel when needed. |
| 767 |
|
| 768 |
Message type: ``history_request``:: |
| 769 |
|
| 770 |
content = { |
| 771 |
|
| 772 |
# If True, also return output history in the resulting dict. |
| 773 |
'output' : bool, |
| 774 |
|
| 775 |
# If True, return the raw input history, else the transformed input. |
| 776 |
'raw' : bool, |
| 777 |
|
| 778 |
# So far, this can be 'range', 'tail' or 'search'. |
| 779 |
'hist_access_type' : str, |
| 780 |
|
| 781 |
# If hist_access_type is 'range', get a range of input cells. session |
| 782 |
# is a number counting up each time the kernel starts; you can give |
| 783 |
# a positive session number, or a negative number to count back from |
| 784 |
# the current session. |
| 785 |
'session' : int, |
| 786 |
# start and stop are line (cell) numbers within that session. |
| 787 |
'start' : int, |
| 788 |
'stop' : int, |
| 789 |
|
| 790 |
# If hist_access_type is 'tail' or 'search', get the last n cells. |
| 791 |
'n' : int, |
| 792 |
|
| 793 |
# If hist_access_type is 'search', get cells matching the specified glob |
| 794 |
# pattern (with * and ? as wildcards). |
| 795 |
'pattern' : str, |
| 796 |
|
| 797 |
# If hist_access_type is 'search' and unique is true, do not |
| 798 |
# include duplicated history. Default is false. |
| 799 |
'unique' : bool, |
| 800 |
|
| 801 |
} |
| 802 |
|
| 803 |
.. versionadded:: 4.0 |
| 804 |
The key ``unique`` for ``history_request``. |
| 805 |
|
| 806 |
Message type: ``history_reply``:: |
| 807 |
|
| 808 |
content = { |
| 809 |
# 'ok' if the request succeeded or 'error', with error information as in all other replies. |
| 810 |
'status' : 'ok', |
| 811 |
|
| 812 |
# A list of 3 tuples, either: |
| 813 |
# (session, line_number, input) or |
| 814 |
# (session, line_number, (input, output)), |
| 815 |
# depending on whether output was False or True, respectively. |
| 816 |
'history' : list, |
| 817 |
} |
| 818 |
|
| 819 |
.. note:: |
| 820 |
|
| 821 |
Most of the history messaging options are not used by Jupyter frontends, and |
| 822 |
many kernels do not implement them. If you're implementing these messages in |
| 823 |
a kernel, the 'tail' request is the most useful; this is used by the Qt |
| 824 |
console, for example. The notebook interface does not use history messages |
| 825 |
at all. |
| 826 |
|
| 827 |
This interface was designed by exposing all the main options of IPython's |
| 828 |
history interface. We may remove some options in a future version of the |
| 829 |
message spec. |
| 830 |
|
| 831 |
.. _msging_is_complete: |
| 832 |
|
| 833 |
Code completeness |
| 834 |
----------------- |
| 835 |
|
| 836 |
.. versionadded:: 5.0 |
| 837 |
|
| 838 |
When the user enters a line in a console style interface, the console must |
| 839 |
decide whether to immediately execute the current code, or whether to show a |
| 840 |
continuation prompt for further input. For instance, in Python ``a = 5`` would |
| 841 |
be executed immediately, while ``for i in range(5):`` would expect further input. |
| 842 |
|
| 843 |
There are four possible replies: |
| 844 |
|
| 845 |
- *complete* code is ready to be executed |
| 846 |
- *incomplete* code should prompt for another line |
| 847 |
- *invalid* code will typically be sent for execution, so that the user sees the |
| 848 |
error soonest. |
| 849 |
- *unknown* - if the kernel is not able to determine this. The frontend should |
| 850 |
also handle the kernel not replying promptly. It may default to sending the |
| 851 |
code for execution, or it may implement simple fallback heuristics for whether |
| 852 |
to execute the code (e.g. execute after a blank line). |
| 853 |
|
| 854 |
Frontends may have ways to override this, forcing the code to be sent for |
| 855 |
execution or forcing a continuation prompt. |
| 856 |
|
| 857 |
Message type: ``is_complete_request``:: |
| 858 |
|
| 859 |
content = { |
| 860 |
# The code entered so far as a multiline string |
| 861 |
'code' : str, |
| 862 |
} |
| 863 |
|
| 864 |
Message type: ``is_complete_reply``:: |
| 865 |
|
| 866 |
content = { |
| 867 |
# One of 'complete', 'incomplete', 'invalid', 'unknown' |
| 868 |
'status' : str, |
| 869 |
|
| 870 |
# If status is 'incomplete', indent should contain the characters to use |
| 871 |
# to indent the next line. This is only a hint: frontends may ignore it |
| 872 |
# and use their own autoindentation rules. For other statuses, this |
| 873 |
# field does not exist. |
| 874 |
'indent': str, |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
Connect |
| 879 |
------- |
| 880 |
|
| 881 |
.. deprecated:: 5.1 |
| 882 |
|
| 883 |
connect_request/reply have not proved useful, and are considered deprecated. |
| 884 |
Kernels are not expected to implement handlers for this message. |
| 885 |
|
| 886 |
When a client connects to the request/reply socket of the kernel, it can issue |
| 887 |
a connect request to get basic information about the kernel, such as the ports |
| 888 |
the other ZeroMQ sockets are listening on. This allows clients to only have |
| 889 |
to know about a single port (the shell channel) to connect to a kernel. |
| 890 |
The ports for any additional channels the kernel is listening on should be included in the reply. |
| 891 |
If any ports are omitted from the reply, this indicates that the channels are not running. |
| 892 |
|
| 893 |
Message type: ``connect_request``:: |
| 894 |
|
| 895 |
content = {} |
| 896 |
|
| 897 |
For example, a kernel with all channels running: |
| 898 |
|
| 899 |
Message type: ``connect_reply``:: |
| 900 |
|
| 901 |
content = { |
| 902 |
'shell_port' : int, # The port the shell ROUTER socket is listening on. |
| 903 |
'iopub_port' : int, # The port the PUB socket is listening on. |
| 904 |
'stdin_port' : int, # The port the stdin ROUTER socket is listening on. |
| 905 |
'hb_port' : int, # The port the heartbeat socket is listening on. |
| 906 |
'control_port' : int, # The port the control ROUTER socket is listening on. |
| 907 |
} |
| 908 |
|
| 909 |
|
| 910 |
.. _msging_comm_info: |
| 911 |
|
| 912 |
Comm info |
| 913 |
--------- |
| 914 |
|
| 915 |
When a client needs the currently open comms in the kernel, it can issue a |
| 916 |
request for the currently open comms. When the optional ``target_name`` is |
| 917 |
specified, the reply only contains the currently open comms for the target. |
| 918 |
|
| 919 |
Message type: ``comm_info_request``:: |
| 920 |
|
| 921 |
content = { |
| 922 |
# Optional, the target name |
| 923 |
'target_name': str, |
| 924 |
} |
| 925 |
|
| 926 |
Message type: ``comm_info_reply``:: |
| 927 |
|
| 928 |
content = { |
| 929 |
# 'ok' if the request succeeded or 'error', with error information as in all other replies. |
| 930 |
'status' : 'ok', |
| 931 |
|
| 932 |
# A dictionary of the comms, indexed by UUIDs. |
| 933 |
'comms': { |
| 934 |
comm_id: { |
| 935 |
'target_name': str, |
| 936 |
}, |
| 937 |
}, |
| 938 |
} |
| 939 |
|
| 940 |
.. versionadded:: 5.1 |
| 941 |
|
| 942 |
.. _msging_kernel_info: |
| 943 |
|
| 944 |
Kernel info |
| 945 |
----------- |
| 946 |
|
| 947 |
If a client needs to know information about the kernel, it can |
| 948 |
make a request of the kernel's information. |
| 949 |
This message can be used to fetch core information of the |
| 950 |
kernel, including language (e.g., Python), language version number and |
| 951 |
IPython version number, and the IPython message spec version number. |
| 952 |
|
| 953 |
Message type: ``kernel_info_request``:: |
| 954 |
|
| 955 |
content = { |
| 956 |
} |
| 957 |
|
| 958 |
Message type: ``kernel_info_reply``:: |
| 959 |
|
| 960 |
content = { |
| 961 |
# 'ok' if the request succeeded or 'error', with error information as in all other replies. |
| 962 |
'status' : 'ok', |
| 963 |
|
| 964 |
# Version of messaging protocol. |
| 965 |
# The first integer indicates major version. It is incremented when |
| 966 |
# there is any backward incompatible change. |
| 967 |
# The second integer indicates minor version. It is incremented when |
| 968 |
# there is any backward compatible change. |
| 969 |
'protocol_version': 'X.Y.Z', |
| 970 |
|
| 971 |
# The kernel implementation name |
| 972 |
# (e.g. 'ipython' for the IPython kernel) |
| 973 |
'implementation': str, |
| 974 |
|
| 975 |
# Implementation version number. |
| 976 |
# The version number of the kernel's implementation |
| 977 |
# (e.g. IPython.__version__ for the IPython kernel) |
| 978 |
'implementation_version': 'X.Y.Z', |
| 979 |
|
| 980 |
# Information about the language of code for the kernel |
| 981 |
'language_info': { |
| 982 |
# Name of the programming language that the kernel implements. |
| 983 |
# Kernel included in IPython returns 'python'. |
| 984 |
'name': str, |
| 985 |
|
| 986 |
# Language version number. |
| 987 |
# It is Python version number (e.g., '2.7.3') for the kernel |
| 988 |
# included in IPython. |
| 989 |
'version': 'X.Y.Z', |
| 990 |
|
| 991 |
# mimetype for script files in this language |
| 992 |
'mimetype': str, |
| 993 |
|
| 994 |
# Extension including the dot, e.g. '.py' |
| 995 |
'file_extension': str, |
| 996 |
|
| 997 |
# Pygments lexer, for highlighting |
| 998 |
# Only needed if it differs from the 'name' field. |
| 999 |
'pygments_lexer': str, |
| 1000 |
|
| 1001 |
# Codemirror mode, for highlighting in the notebook. |
| 1002 |
# Only needed if it differs from the 'name' field. |
| 1003 |
'codemirror_mode': str or dict, |
| 1004 |
|
| 1005 |
# Nbconvert exporter, if notebooks written with this kernel should |
| 1006 |
# be exported with something other than the general 'script' |
| 1007 |
# exporter. |
| 1008 |
'nbconvert_exporter': str, |
| 1009 |
}, |
| 1010 |
|
| 1011 |
# A banner of information about the kernel, |
| 1012 |
# which may be displayed in console environments. |
| 1013 |
'banner': str, |
| 1014 |
|
| 1015 |
# A boolean flag which tells if the kernel supports debugging in the notebook. |
| 1016 |
# Default is False. |
| 1017 |
# Deprecated as replaced by 'supported_features'=['debugger'] (see below). |
| 1018 |
'debugger': bool, |
| 1019 |
|
| 1020 |
# Optional: A list of dictionaries, each with keys 'text' and 'url'. |
| 1021 |
# These will be displayed in the help menu in the notebook UI. |
| 1022 |
'help_links': [ |
| 1023 |
{'text': str, 'url': str} |
| 1024 |
], |
| 1025 |
|
| 1026 |
# Optional: A list of optional features such as 'debugger' and |
| 1027 |
# 'kernel subshells'. Introduced by Jupyter Enhancement Proposal 92 |
| 1028 |
# https://github.com/jupyter/enhancement-proposals/pull/92 |
| 1029 |
'supported_features': [str] |
| 1030 |
} |
| 1031 |
|
| 1032 |
Refer to the lists of available `Pygments lexers <http://pygments.org/docs/lexers/>`_ |
| 1033 |
and `codemirror modes <http://codemirror.net/mode/index.html>`_ for those fields. |
| 1034 |
|
| 1035 |
.. versionchanged:: 5.0 |
| 1036 |
|
| 1037 |
Versions changed from lists of integers to strings. |
| 1038 |
|
| 1039 |
.. versionchanged:: 5.0 |
| 1040 |
|
| 1041 |
``ipython_version`` is removed. |
| 1042 |
|
| 1043 |
.. versionchanged:: 5.0 |
| 1044 |
|
| 1045 |
``language_info``, ``implementation``, ``implementation_version``, ``banner`` |
| 1046 |
and ``help_links`` keys are added. |
| 1047 |
|
| 1048 |
.. versionchanged:: 5.0 |
| 1049 |
|
| 1050 |
``language_version`` moved to ``language_info.version`` |
| 1051 |
|
| 1052 |
.. versionchanged:: 5.0 |
| 1053 |
|
| 1054 |
``language`` moved to ``language_info.name`` |
| 1055 |
|
| 1056 |
.. versionchanged:: 5.5 |
| 1057 |
|
| 1058 |
``supported_features`` added and ``debugger`` deprecated. |
| 1059 |
|
| 1060 |
Messages on the Control (ROUTER/DEALER) channel |
| 1061 |
=============================================== |
| 1062 |
|
| 1063 |
.. _msging_shutdown: |
| 1064 |
|
| 1065 |
Kernel shutdown |
| 1066 |
--------------- |
| 1067 |
|
| 1068 |
The clients can request the kernel to shut itself down; this is used in |
| 1069 |
multiple cases: |
| 1070 |
|
| 1071 |
- when the user chooses to close the client application via a menu or window |
| 1072 |
control. |
| 1073 |
- when the user types 'exit' or 'quit' (or their uppercase magic equivalents). |
| 1074 |
- when the user chooses a GUI method (like the 'Ctrl-C' shortcut in the |
| 1075 |
IPythonQt client) to force a kernel restart to get a clean kernel without |
| 1076 |
losing client-side state like history or inlined figures. |
| 1077 |
|
| 1078 |
Implementation recommendation for starting kernels: A restart should optimally |
| 1079 |
preserve as many resources outside the kernel as possible (e.g. only restart the |
| 1080 |
kernel and its subprocesses and not any parent processes). That is, ideally a |
| 1081 |
restart should be "in-place". For local kernels, there is typically no parent |
| 1082 |
process so a "hard" restart and an in-place restart are identical whereas for |
| 1083 |
remote kernels this is not generally the same. As an example, if a remote kernel |
| 1084 |
is run in a container, during an in-place restart the container may be kept |
| 1085 |
running and a new kernel process within it would be started. |
| 1086 |
|
| 1087 |
The client sends a shutdown request to the kernel, and once it receives the |
| 1088 |
reply message (which is otherwise empty), it can assume that the kernel has |
| 1089 |
completed shutdown safely. The request is sent on the ``control`` channel. |
| 1090 |
|
| 1091 |
Upon their own shutdown, client applications will typically execute a last |
| 1092 |
minute sanity check and forcefully terminate any kernel that is still alive, to |
| 1093 |
avoid leaving stray processes in the user's machine. |
| 1094 |
|
| 1095 |
Message type: ``shutdown_request``:: |
| 1096 |
|
| 1097 |
content = { |
| 1098 |
'restart' : bool # False if final shutdown, or True if shutdown precedes a restart |
| 1099 |
} |
| 1100 |
|
| 1101 |
Message type: ``shutdown_reply``:: |
| 1102 |
|
| 1103 |
content = { |
| 1104 |
# 'ok' if the request succeeded or 'error', with error information as in all other replies. |
| 1105 |
'status' : 'ok', |
| 1106 |
|
| 1107 |
'restart' : bool # False if final shutdown, or True if shutdown precedes a restart |
| 1108 |
} |
| 1109 |
|
| 1110 |
.. Note:: |
| 1111 |
|
| 1112 |
When the clients detect a dead kernel thanks to inactivity on the heartbeat |
| 1113 |
socket, they simply send a forceful process termination signal, since a dead |
| 1114 |
process is unlikely to respond in any useful way to messages. |
| 1115 |
|
| 1116 |
.. versionchanged:: 5.4 |
| 1117 |
|
| 1118 |
Sending a ``shutdown_request`` message on the ``shell`` channel is deprecated. |
| 1119 |
|
| 1120 |
.. _msging_interrupt: |
| 1121 |
|
| 1122 |
Kernel interrupt |
| 1123 |
---------------- |
| 1124 |
|
| 1125 |
In case a kernel can not catch operating system interrupt signals (e.g. the used |
| 1126 |
runtime handles signals and does not allow a user program to define a callback), |
| 1127 |
a kernel can choose to be notified using a message instead. For this to work, |
| 1128 |
the kernels kernelspec must set ``interrupt_mode`` to ``message``. An interruption |
| 1129 |
will then result in the following message on the ``control`` channel: |
| 1130 |
|
| 1131 |
Message type: ``interrupt_request``:: |
| 1132 |
|
| 1133 |
content = {} |
| 1134 |
|
| 1135 |
Message type: ``interrupt_reply``:: |
| 1136 |
|
| 1137 |
content = { |
| 1138 |
# 'ok' if the request succeeded or 'error', with error information as in all other replies. |
| 1139 |
'status' : 'ok' |
| 1140 |
} |
| 1141 |
|
| 1142 |
.. versionadded:: 5.3 |
| 1143 |
|
| 1144 |
Kernel info |
| 1145 |
----------- |
| 1146 |
|
| 1147 |
This is the same :ref:`kernel info <msging_kernel_info>` message as that received on the Shell channel. |
| 1148 |
|
| 1149 |
.. versionadded:: 5.5 |
| 1150 |
|
| 1151 |
Debug request |
| 1152 |
------------- |
| 1153 |
|
| 1154 |
This message type is used with debugging kernels to request specific actions |
| 1155 |
to be performed by the debugger such as adding a breakpoint or stepping into |
| 1156 |
a code. |
| 1157 |
Kernels supporting subshells must include ``'debugger'`` in ``'supported_features'`` |
| 1158 |
in :ref:`kernel info <msging_kernel_info>` reply messages. |
| 1159 |
|
| 1160 |
Message type: ``debug_request``:: |
| 1161 |
|
| 1162 |
content = {} |
| 1163 |
|
| 1164 |
Message type: ``debug_reply``:: |
| 1165 |
|
| 1166 |
content = {} |
| 1167 |
|
| 1168 |
The ``content`` dicts of the ``debug_request`` and ``debug_reply`` messages respectively follow the |
| 1169 |
specification of the ``Request`` and ``Response`` messages from the |
| 1170 |
`Debug Adapter Protocol (DAP) <https://microsoft.github.io/debug-adapter-protocol/>`_ as of version 1.39 or later. |
| 1171 |
|
| 1172 |
Debug requests and replies are sent over the ``control`` channel to prevent |
| 1173 |
queuing behind execution requests. |
| 1174 |
|
| 1175 |
.. versionadded:: 5.5 |
| 1176 |
|
| 1177 |
Additions to the DAP |
| 1178 |
~~~~~~~~~~~~~~~~~~~~ |
| 1179 |
|
| 1180 |
The Jupyter debugger protocol makes several additions to the DAP: |
| 1181 |
|
| 1182 |
- the `dumpCell`_ request and response messages |
| 1183 |
- the `debugInfo`_ request and response messages |
| 1184 |
- the `inspectVariables`_ request and response messages |
| 1185 |
- the `richInspectVariables`_ request and response messages |
| 1186 |
- the `copyToGlobals`_ request and response messages |
| 1187 |
|
| 1188 |
dumpCell |
| 1189 |
######## |
| 1190 |
|
| 1191 |
In order to support the debugging of notebook cells and of Jupyter consoles, |
| 1192 |
which are not based on source files, we need a message to submit code to the |
| 1193 |
debugger to which breakpoints can be added. |
| 1194 |
|
| 1195 |
Content of the ``dumpCell`` request:: |
| 1196 |
|
| 1197 |
{ |
| 1198 |
'type' : 'request', |
| 1199 |
'command' : 'dumpCell', |
| 1200 |
'arguments' : { |
| 1201 |
'code' : str # the content of the cell being submitted. |
| 1202 |
} |
| 1203 |
} |
| 1204 |
|
| 1205 |
Content of the ``dumpCell`` response:: |
| 1206 |
|
| 1207 |
{ |
| 1208 |
'type' : 'response', |
| 1209 |
'success': bool, |
| 1210 |
'body': { |
| 1211 |
'sourcePath': str # filename for the dumped source |
| 1212 |
} |
| 1213 |
} |
| 1214 |
|
| 1215 |
.. versionadded:: 5.5 |
| 1216 |
|
| 1217 |
debugInfo |
| 1218 |
######### |
| 1219 |
|
| 1220 |
In order to support page reloading, or a client connecting at a later stage, |
| 1221 |
Jupyter kernels must store the state of the debugger (such as breakpoints, |
| 1222 |
whether the debugger is currently stopped). The ``debugInfo`` request is a DAP |
| 1223 |
``Request`` with no extra argument. |
| 1224 |
|
| 1225 |
Content of the ``debugInfo`` request:: |
| 1226 |
|
| 1227 |
{ |
| 1228 |
'type' : 'request', |
| 1229 |
'command' : 'debugInfo' |
| 1230 |
} |
| 1231 |
|
| 1232 |
Content of the ``debugInfo`` response:: |
| 1233 |
|
| 1234 |
{ |
| 1235 |
'type' : 'response', |
| 1236 |
'success' : bool, |
| 1237 |
'body' : { |
| 1238 |
'isStarted' : bool, # whether the debugger is started, |
| 1239 |
'hashMethod' : str, # the hash method for code cell. Default is 'Murmur2', |
| 1240 |
'hashSeed' : str, # the seed for the hashing of code cells, |
| 1241 |
'tmpFilePrefix' : str, # prefix for temporary file names |
| 1242 |
'tmpFileSuffix' : str, # suffix for temporary file names |
| 1243 |
'breakpoints' : [ # breakpoints currently registered in the debugger. |
| 1244 |
{ |
| 1245 |
'source' : str, # source file |
| 1246 |
'breakpoints' : list(source_breakpoints) # list of breakpoints for that source file |
| 1247 |
} |
| 1248 |
], |
| 1249 |
'stoppedThreads' : list(int), # threads in which the debugger is currently in a stopped state |
| 1250 |
'richRendering' : bool, # whether the debugger supports rich rendering of variables |
| 1251 |
'exceptionPaths' : list(str), # exception names used to match leaves or nodes in a tree of exception |
| 1252 |
'copyToGlobals' : bool, # whether the debugger supports supports the copyToGlobals request |
| 1253 |
} |
| 1254 |
} |
| 1255 |
|
| 1256 |
The ``source_breakpoint`` schema is specified by the Debug Adapter Protocol. |
| 1257 |
|
| 1258 |
.. versionadded:: 5.5 |
| 1259 |
|
| 1260 |
inspectVariables |
| 1261 |
################ |
| 1262 |
|
| 1263 |
The ``inspectVariables`` is meant to retrieve the values of all the variables |
| 1264 |
that have been defined in the kernel. It is a DAP ``Request`` with no extra |
| 1265 |
argument. |
| 1266 |
|
| 1267 |
Content of the ``inspectVariables`` request:: |
| 1268 |
|
| 1269 |
{ |
| 1270 |
'type' : 'request', |
| 1271 |
'command' : 'inspectVariables' |
| 1272 |
} |
| 1273 |
|
| 1274 |
Content of the ``inspectVariables`` response:: |
| 1275 |
|
| 1276 |
{ |
| 1277 |
'type' : 'response', |
| 1278 |
'success' : bool, |
| 1279 |
'body' : { |
| 1280 |
'variables' : [ # variables defined in the notebook. |
| 1281 |
{ |
| 1282 |
'name' : str, |
| 1283 |
'variablesReference' : int, |
| 1284 |
'value' : str, |
| 1285 |
'type' : str |
| 1286 |
} |
| 1287 |
] |
| 1288 |
} |
| 1289 |
} |
| 1290 |
|
| 1291 |
.. versionadded:: 5.5 |
| 1292 |
|
| 1293 |
richInspectVariables |
| 1294 |
#################### |
| 1295 |
|
| 1296 |
The ``richInspectVariables`` request allows to get the rich representation of a |
| 1297 |
variable that has been defined in the kernel. |
| 1298 |
|
| 1299 |
Content of the ``richInspectVariables`` request:: |
| 1300 |
|
| 1301 |
{ |
| 1302 |
'type' : 'request', |
| 1303 |
'command' : 'richInspectVariables', |
| 1304 |
'arguments' : { |
| 1305 |
'variableName' : str, |
| 1306 |
# The frameId is used when the debugger hit a breakpoint only. |
| 1307 |
'frameId' : int |
| 1308 |
} |
| 1309 |
} |
| 1310 |
|
| 1311 |
Content of the ``richInspectVariables`` response:: |
| 1312 |
|
| 1313 |
{ |
| 1314 |
'type' : 'response', |
| 1315 |
'success' : bool, |
| 1316 |
'body' : { |
| 1317 |
# Dictionary of rich representations of the variable |
| 1318 |
'data' : dict, |
| 1319 |
'metadata' : dict |
| 1320 |
} |
| 1321 |
} |
| 1322 |
|
| 1323 |
.. versionadded:: 5.5 |
| 1324 |
|
| 1325 |
copyToGlobals |
| 1326 |
############# |
| 1327 |
|
| 1328 |
The ``copyToGlobals`` request allows to copy a variable from the local variable panel |
| 1329 |
of the debugger to the ``global`` scope to inspect it after debug session. |
| 1330 |
The support for this request is optional and should be indicated to the client via |
| 1331 |
the ``copyToGlobals`` boolean field in the debugInfo reply. |
| 1332 |
|
| 1333 |
Content of the ``copyToGlobals`` request:: |
| 1334 |
|
| 1335 |
{ |
| 1336 |
'type': 'request', |
| 1337 |
'command': 'copyToGlobals', |
| 1338 |
'arguments': { |
| 1339 |
# the variable to copy from the frame corresponding to `srcFrameId` |
| 1340 |
'srcVariableName': str, |
| 1341 |
'srcFrameId': int, |
| 1342 |
# the copied variable name in the global scope |
| 1343 |
'dstVariableName': str |
| 1344 |
} |
| 1345 |
} |
| 1346 |
|
| 1347 |
Content of the ``copyToGlobals`` response:: |
| 1348 |
|
| 1349 |
{ |
| 1350 |
'type': 'response', |
| 1351 |
'success': bool, |
| 1352 |
'command': 'setExpression', |
| 1353 |
'body': { |
| 1354 |
# string representation of the copied variable |
| 1355 |
'value': str, |
| 1356 |
# type of the copied variable |
| 1357 |
'type': str, |
| 1358 |
'variablesReference': int |
| 1359 |
} |
| 1360 |
} |
| 1361 |
|
| 1362 |
.. versionadded:: 5.5 |
| 1363 |
|
| 1364 |
.. _kernel_subshells: |
| 1365 |
|
| 1366 |
Kernel subshells |
| 1367 |
---------------- |
| 1368 |
|
| 1369 |
Kernel subshells are separate threads of execution within the same kernel process that |
| 1370 |
were introduced by |
| 1371 |
`Jupyter Enhancement Proposal 91 <https://github.com/jupyter/enhancement-proposals/pull/91>`_. |
| 1372 |
Kernels supporting subshells must include ``'kernel subshells'`` in ``'supported_features'`` |
| 1373 |
in :ref:`kernel info <msging_kernel_info>` reply messages. |
| 1374 |
|
| 1375 |
Create subshell |
| 1376 |
~~~~~~~~~~~~~~~ |
| 1377 |
|
| 1378 |
In a kernel that supports subshells, this creates a new subshell (running in a separate thread) |
| 1379 |
and returns its unique ID. In a kernel that does not support subshells an error is logged and |
| 1380 |
no reply is sent. |
| 1381 |
|
| 1382 |
Message type: ``create_subshell_request``:: |
| 1383 |
|
| 1384 |
content = { |
| 1385 |
} |
| 1386 |
|
| 1387 |
Message type: ``create_subshell_reply``:: |
| 1388 |
|
| 1389 |
content = { |
| 1390 |
# 'ok' if the request succeeded or 'error', with error information as in all other replies. |
| 1391 |
'status' : 'ok', |
| 1392 |
|
| 1393 |
# The ID of the subshell, unique within the kernel. |
| 1394 |
'subshell_id': str, |
| 1395 |
} |
| 1396 |
|
| 1397 |
.. versionadded:: 5.5 |
| 1398 |
|
| 1399 |
Delete subshell |
| 1400 |
~~~~~~~~~~~~~~~ |
| 1401 |
|
| 1402 |
In a kernel that supports subshells, this deletes a subshell identified by its unique ID. |
| 1403 |
In a kernel that does not support subshells an error is logged and no reply is sent. |
| 1404 |
|
| 1405 |
Message type: ``delete_subshell_request``:: |
| 1406 |
|
| 1407 |
content = { |
| 1408 |
# The ID of the subshell. |
| 1409 |
'subshell_id': str |
| 1410 |
} |
| 1411 |
|
| 1412 |
Message type: ``delete_subshell_reply``:: |
| 1413 |
|
| 1414 |
content = { |
| 1415 |
# 'ok' if the request succeeded or 'error', with error information as in all other replies. |
| 1416 |
'status': 'ok', |
| 1417 |
} |
| 1418 |
|
| 1419 |
.. versionadded:: 5.5 |
| 1420 |
|
| 1421 |
List subshell |
| 1422 |
~~~~~~~~~~~~~ |
| 1423 |
|
| 1424 |
In a kernel that supports subshells, this returns a list of the IDs of all subshells that exist |
| 1425 |
in that kernel. In a kernel that does not support subshells an error is logged and no reply is sent. |
| 1426 |
|
| 1427 |
Message type: ``list_subshell_request``:: |
| 1428 |
|
| 1429 |
content = { |
| 1430 |
} |
| 1431 |
|
| 1432 |
Message type: ``list_subshell_reply``:: |
| 1433 |
|
| 1434 |
content = { |
| 1435 |
# 'ok' if the request succeeded or 'error', with error information as in all other replies. |
| 1436 |
'status': 'ok', |
| 1437 |
|
| 1438 |
# A list of subshell IDs. |
| 1439 |
'subshell_id': [str] |
| 1440 |
} |
| 1441 |
|
| 1442 |
.. versionadded:: 5.5 |
| 1443 |
|
| 1444 |
Messages on the IOPub (XPUB/SUB) channel |
| 1445 |
======================================== |
| 1446 |
|
| 1447 |
Welcome message |
| 1448 |
--------------- |
| 1449 |
|
| 1450 |
This message is sent to a client SUB socket the first time it connects to the |
| 1451 |
XPUB kernel socket, to notify the client that the connection is established. |
| 1452 |
|
| 1453 |
message type: ``iopub_welcome``:: |
| 1454 |
|
| 1455 |
content = { |
| 1456 |
# The topic the SUB has subscribed to. Can be empty string if |
| 1457 |
# the client has subscribed to all topics. |
| 1458 |
'subscription' : str, |
| 1459 |
} |
| 1460 |
|
| 1461 |
.. note:: |
| 1462 |
|
| 1463 |
This message has no parent header. |
| 1464 |
|
| 1465 |
.. note:: |
| 1466 |
|
| 1467 |
Welcome messages do not and cannot identify the client whose subscription is being received. |
| 1468 |
Receiving an iopub_welcome message with your subscription does not mean it is in response to |
| 1469 |
your own subscription. However, receiving a message does mean that a matching subscription has |
| 1470 |
been registered for your client, otherwise no message will be received. So if only one |
| 1471 |
subscription is registered, as is normally the case, receiving any welcome message is sufficient |
| 1472 |
to indicate that your client's subscription is fully established. The gist is that receiving a |
| 1473 |
welcome message is a sufficient condition to establish the subscription-propagation event, and |
| 1474 |
additional welcome messages should be expected and ignored. |
| 1475 |
|
| 1476 |
.. versionadded:: 5.5 |
| 1477 |
|
| 1478 |
Streams (stdout, stderr, etc) |
| 1479 |
------------------------------ |
| 1480 |
|
| 1481 |
Message type: ``stream``:: |
| 1482 |
|
| 1483 |
content = { |
| 1484 |
# The name of the stream is one of 'stdout', 'stderr' |
| 1485 |
'name' : str, |
| 1486 |
|
| 1487 |
# The text is an arbitrary string to be written to that stream |
| 1488 |
'text' : str, |
| 1489 |
} |
| 1490 |
|
| 1491 |
.. versionchanged:: 5.0 |
| 1492 |
|
| 1493 |
'data' key renamed to 'text' for consistency with the notebook format. |
| 1494 |
|
| 1495 |
Display Data |
| 1496 |
------------ |
| 1497 |
|
| 1498 |
This type of message is used to bring back data that should be displayed (text, |
| 1499 |
html, svg, etc.) in the frontends. This data is published to all frontends. |
| 1500 |
Each message can have multiple representations of the data; it is up to the |
| 1501 |
frontend to decide which to use and how. A single message should contain all |
| 1502 |
possible representations of the same information. Each representation should |
| 1503 |
be a JSON'able data structure, and should be a valid MIME type. |
| 1504 |
|
| 1505 |
Some questions remain about this design: |
| 1506 |
|
| 1507 |
* Do we use this message type for execute_result/displayhook? Probably not, because |
| 1508 |
the displayhook also has to handle the Out prompt display. On the other hand |
| 1509 |
we could put that information into the metadata section. |
| 1510 |
|
| 1511 |
.. _display_data: |
| 1512 |
|
| 1513 |
Message type: ``display_data``:: |
| 1514 |
|
| 1515 |
content = { |
| 1516 |
|
| 1517 |
# Who create the data |
| 1518 |
# Used in V4. Removed in V5. |
| 1519 |
# 'source' : str, |
| 1520 |
|
| 1521 |
# The data dict contains key/value pairs, where the keys are MIME |
| 1522 |
# types and the values are the raw data of the representation in that |
| 1523 |
# format. |
| 1524 |
'data' : dict, |
| 1525 |
|
| 1526 |
# Any metadata that describes the data |
| 1527 |
'metadata' : dict, |
| 1528 |
|
| 1529 |
# Optional transient data introduced in 5.1. Information not to be |
| 1530 |
# persisted to a notebook or other documents. Intended to live only |
| 1531 |
# during a live kernel session. |
| 1532 |
'transient': dict, |
| 1533 |
} |
| 1534 |
|
| 1535 |
|
| 1536 |
The ``metadata`` contains any metadata that describes the output. |
| 1537 |
Global keys are assumed to apply to the output as a whole. |
| 1538 |
The ``metadata`` dict can also contain mime-type keys, which will be sub-dictionaries, |
| 1539 |
which are interpreted as applying only to output of that type. |
| 1540 |
Third parties should put any data they write into a single dict |
| 1541 |
with a reasonably unique name to avoid conflicts. |
| 1542 |
|
| 1543 |
The only metadata keys currently defined in IPython are the width and height |
| 1544 |
of images:: |
| 1545 |
|
| 1546 |
metadata = { |
| 1547 |
'image/png' : { |
| 1548 |
'width': 640, |
| 1549 |
'height': 480 |
| 1550 |
} |
| 1551 |
} |
| 1552 |
|
| 1553 |
and expanded for JSON data:: |
| 1554 |
|
| 1555 |
metadata = { |
| 1556 |
'application/json' : { |
| 1557 |
'expanded': True |
| 1558 |
} |
| 1559 |
} |
| 1560 |
|
| 1561 |
|
| 1562 |
The ``transient`` dict contains runtime metadata that should not be persisted to |
| 1563 |
document formats and is fully optional. The only transient key currently defined in Jupyter is |
| 1564 |
``display_id``:: |
| 1565 |
|
| 1566 |
transient = { |
| 1567 |
'display_id': 'abcd' |
| 1568 |
} |
| 1569 |
|
| 1570 |
.. versionchanged:: 5.0 |
| 1571 |
|
| 1572 |
`application/json` data should be unpacked JSON data, |
| 1573 |
not double-serialized as a JSON string. |
| 1574 |
|
| 1575 |
.. versionchanged:: 5.1 |
| 1576 |
|
| 1577 |
`transient` is a new field. |
| 1578 |
|
| 1579 |
Update Display Data |
| 1580 |
------------------- |
| 1581 |
|
| 1582 |
.. versionadded:: 5.1 |
| 1583 |
|
| 1584 |
Displays can now be named with a ``display_id`` within the ``transient`` field of |
| 1585 |
``display_data`` or ``execute_result``. |
| 1586 |
|
| 1587 |
When a ``display_id`` is specified for a display, it can be updated later |
| 1588 |
with an ``update_display_data`` message. This message has the same format as `display_data`_ |
| 1589 |
messages and must contain a ``transient`` field with a ``display_id``. |
| 1590 |
|
| 1591 |
.. _update_display_data: |
| 1592 |
|
| 1593 |
Message type: ``update_display_data``:: |
| 1594 |
|
| 1595 |
content = { |
| 1596 |
|
| 1597 |
# The data dict contains key/value pairs, where the keys are MIME |
| 1598 |
# types and the values are the raw data of the representation in that |
| 1599 |
# format. |
| 1600 |
'data' : dict, |
| 1601 |
|
| 1602 |
# Any metadata that describes the data |
| 1603 |
'metadata' : dict, |
| 1604 |
|
| 1605 |
# Any information not to be persisted to a notebook or other environment |
| 1606 |
# Intended to live only during a kernel session |
| 1607 |
'transient': dict, |
| 1608 |
} |
| 1609 |
|
| 1610 |
Frontends can choose how they update prior outputs (or if they regard this as a |
| 1611 |
regular ``display_data`` message). Within the jupyter and nteract_ notebooks, |
| 1612 |
all displays that match the ``display_id`` are updated (even if there are multiple). |
| 1613 |
|
| 1614 |
Code inputs |
| 1615 |
----------- |
| 1616 |
|
| 1617 |
To let all frontends know what code is being executed at any given time, these |
| 1618 |
messages contain a re-broadcast of the ``code`` portion of an |
| 1619 |
:ref:`execute_request <execute>`, along with the :ref:`execution_count |
| 1620 |
<execution_counter>`. |
| 1621 |
|
| 1622 |
Message type: ``execute_input``:: |
| 1623 |
|
| 1624 |
content = { |
| 1625 |
'code' : str, # Source code to be executed, one or more lines |
| 1626 |
|
| 1627 |
# The counter for this execution is also provided so that clients can |
| 1628 |
# display it, since IPython automatically creates variables called _iN |
| 1629 |
# (for input prompt In[N]). |
| 1630 |
'execution_count' : int |
| 1631 |
} |
| 1632 |
|
| 1633 |
.. versionchanged:: 5.0 |
| 1634 |
|
| 1635 |
``pyin`` is renamed to ``execute_input``. |
| 1636 |
|
| 1637 |
|
| 1638 |
Execution results |
| 1639 |
----------------- |
| 1640 |
|
| 1641 |
Results of an execution are published as an ``execute_result``. |
| 1642 |
These are identical to `display_data`_ messages, with the addition of an ``execution_count`` key. |
| 1643 |
|
| 1644 |
Results can have multiple simultaneous formats depending on its |
| 1645 |
configuration. A plain text representation should always be provided |
| 1646 |
in the ``text/plain`` mime-type. Frontends are free to display any or all of these |
| 1647 |
according to its capabilities. |
| 1648 |
Frontends should ignore mime-types they do not understand. The data itself is |
| 1649 |
any JSON object and depends on the format. It is often, but not always a string. |
| 1650 |
|
| 1651 |
Message type: ``execute_result``:: |
| 1652 |
|
| 1653 |
content = { |
| 1654 |
|
| 1655 |
# The counter for this execution is also provided so that clients can |
| 1656 |
# display it, since IPython automatically creates variables called _N |
| 1657 |
# (for prompt N). |
| 1658 |
'execution_count' : int, |
| 1659 |
|
| 1660 |
# data and metadata are identical to a display_data message. |
| 1661 |
# the object being displayed is that passed to the display hook, |
| 1662 |
# i.e. the *result* of the execution. |
| 1663 |
'data' : dict, |
| 1664 |
'metadata' : dict, |
| 1665 |
} |
| 1666 |
|
| 1667 |
Execution errors |
| 1668 |
---------------- |
| 1669 |
|
| 1670 |
When an error occurs during code execution |
| 1671 |
|
| 1672 |
Message type: ``error``:: |
| 1673 |
|
| 1674 |
content = { |
| 1675 |
# Similar content to the execute_reply messages for the 'error' case, |
| 1676 |
# except the 'status' and 'execution_count' fields are omitted. |
| 1677 |
} |
| 1678 |
|
| 1679 |
.. versionchanged:: 5.0 |
| 1680 |
|
| 1681 |
``pyerr`` renamed to ``error`` |
| 1682 |
|
| 1683 |
.. _status: |
| 1684 |
|
| 1685 |
Kernel status |
| 1686 |
------------- |
| 1687 |
|
| 1688 |
This message type is used by frontends to monitor the status of the kernel. |
| 1689 |
|
| 1690 |
Message type: ``status``:: |
| 1691 |
|
| 1692 |
content = { |
| 1693 |
# When the kernel starts to handle a message, it will enter the 'busy' |
| 1694 |
# state and when it finishes, it will enter the 'idle' state. |
| 1695 |
# The kernel will publish state 'starting' exactly once at process startup. |
| 1696 |
execution_state : ('busy', 'idle', 'starting') |
| 1697 |
} |
| 1698 |
|
| 1699 |
When a kernel receives a request and begins processing it, |
| 1700 |
the kernel shall immediately publish a status message with ``execution_state: 'busy'``. |
| 1701 |
When that kernel has completed processing the request |
| 1702 |
and has finished publishing associated IOPub messages, if any, |
| 1703 |
it shall publish a status message with ``execution_state: 'idle'``. |
| 1704 |
Thus, the outputs associated with a given execution shall generally arrive |
| 1705 |
between the busy and idle status messages associated with a given request. |
| 1706 |
|
| 1707 |
.. note:: |
| 1708 |
|
| 1709 |
**A caveat for asynchronous output** |
| 1710 |
|
| 1711 |
Asynchronous output (e.g. from background threads) may be produced after the kernel |
| 1712 |
has sent the idle status message that signals the completion of the request. |
| 1713 |
The handling of these out-of-order output messages is currently undefined in this specification, |
| 1714 |
but the Jupyter Notebook continues to handle IOPub messages associated with a given request |
| 1715 |
after the idle message has arrived, |
| 1716 |
as long as the output area corresponding to that request is still active. |
| 1717 |
|
| 1718 |
.. versionchanged:: 5.0 |
| 1719 |
|
| 1720 |
Busy and idle messages should be sent before/after handling every request, |
| 1721 |
not just execution. |
| 1722 |
|
| 1723 |
Clear output |
| 1724 |
------------ |
| 1725 |
|
| 1726 |
This message type is used to clear the output that is visible on the frontend. |
| 1727 |
|
| 1728 |
Message type: ``clear_output``:: |
| 1729 |
|
| 1730 |
content = { |
| 1731 |
|
| 1732 |
# Wait to clear the output until new output is available. Clears the |
| 1733 |
# existing output immediately before the new output is displayed. |
| 1734 |
# Useful for creating simple animations with minimal flickering. |
| 1735 |
'wait' : bool, |
| 1736 |
} |
| 1737 |
|
| 1738 |
.. versionchanged:: 4.1 |
| 1739 |
|
| 1740 |
``stdout``, ``stderr``, and ``display`` boolean keys for selective clearing are removed, |
| 1741 |
and ``wait`` is added. |
| 1742 |
The selective clearing keys are ignored in v4 and the default behavior remains the same, |
| 1743 |
so v4 clear_output messages will be safely handled by a v4.1 frontend. |
| 1744 |
|
| 1745 |
.. _debug_event: |
| 1746 |
|
| 1747 |
Debug event |
| 1748 |
----------- |
| 1749 |
|
| 1750 |
This message type is used by debugging kernels to send debugging events to the |
| 1751 |
frontend. |
| 1752 |
|
| 1753 |
Message type: ``debug_event``:: |
| 1754 |
|
| 1755 |
content = {} |
| 1756 |
|
| 1757 |
The ``content`` dict follows the specification of the ``Event`` message from the `Debug Adapter Protocol (DAP) <https://microsoft.github.io/debug-adapter-protocol/>`_. |
| 1758 |
|
| 1759 |
.. versionadded:: 5.5 |
| 1760 |
|
| 1761 |
.. _stdin_messages: |
| 1762 |
|
| 1763 |
Messages on the stdin (ROUTER/DEALER) channel |
| 1764 |
============================================= |
| 1765 |
|
| 1766 |
With the stdin ROUTER/DEALER socket, the request/reply pattern goes in the |
| 1767 |
opposite direction of most kernel communication. |
| 1768 |
With the stdin socket, the kernel makes the request, and the single frontend |
| 1769 |
provides the response. |
| 1770 |
This pattern allows code to prompt the user for a line of input, |
| 1771 |
which would normally be read from stdin in a terminal. |
| 1772 |
|
| 1773 |
Many programming languages provide a function which displays a prompt, blocks |
| 1774 |
until the user presses return, and returns the text they typed before pressing |
| 1775 |
return. In Python 3, this is the ``input()`` function; in R it is called |
| 1776 |
``readline()``. If the :ref:`execute_request <execute>` message has |
| 1777 |
``allow_stdin==True``, kernels may implement these functions so that they send |
| 1778 |
an ``input_request`` message and wait for a corresponding ``input_reply``. The |
| 1779 |
frontend is responsible for displaying the prompt and getting the user's input. |
| 1780 |
|
| 1781 |
If ``allow_stdin`` is False, the kernel must not send ``stdin_request``. The |
| 1782 |
kernel may decide what to do instead, but it's most likely that calls to the |
| 1783 |
'prompt for input' function should fail immediately in this case. |
| 1784 |
|
| 1785 |
Message type: ``input_request``:: |
| 1786 |
|
| 1787 |
content = { |
| 1788 |
# the text to show at the prompt |
| 1789 |
'prompt' : str, |
| 1790 |
# Is the request for a password? |
| 1791 |
# If so, the frontend shouldn't echo input. |
| 1792 |
'password' : bool |
| 1793 |
} |
| 1794 |
|
| 1795 |
Message type: ``input_reply``:: |
| 1796 |
|
| 1797 |
content = { 'value' : str } |
| 1798 |
|
| 1799 |
|
| 1800 |
When ``password`` is True, the frontend should not show the input as it is entered. |
| 1801 |
Different frontends may obscure it in different ways; e.g. showing each |
| 1802 |
character entered as the same neutral symbol, or not showing anything at all as |
| 1803 |
the user types. |
| 1804 |
|
| 1805 |
.. versionchanged:: 5.0 |
| 1806 |
|
| 1807 |
``password`` key added. |
| 1808 |
|
| 1809 |
.. note:: |
| 1810 |
|
| 1811 |
The stdin socket of the client is required to have the same zmq IDENTITY |
| 1812 |
as the client's shell socket. |
| 1813 |
Because of this, the ``input_request`` must be sent with the same IDENTITY |
| 1814 |
routing prefix as the ``execute_reply`` in order for the frontend to receive |
| 1815 |
the message. |
| 1816 |
|
| 1817 |
.. note:: |
| 1818 |
|
| 1819 |
This pattern of requesting user input is quite different from how stdin works |
| 1820 |
at a lower level. The Jupyter protocol does not support everything code |
| 1821 |
running in a terminal can do with stdin, but we believe that this enables the |
| 1822 |
most common use cases. |
| 1823 |
|
| 1824 |
.. _kernel_heartbeat: |
| 1825 |
|
| 1826 |
Heartbeat for kernels |
| 1827 |
===================== |
| 1828 |
|
| 1829 |
Clients send ping messages on a REQ socket, which are echoed right back |
| 1830 |
from the Kernel's REP socket. These are simple bytestrings, not full JSON messages described above. |
| 1831 |
|
| 1832 |
Custom Messages |
| 1833 |
=============== |
| 1834 |
|
| 1835 |
.. versionadded:: 4.1 |
| 1836 |
|
| 1837 |
Message spec 4.1 (IPython 2.0) added a messaging system for developers to add |
| 1838 |
their own objects with Frontend |
| 1839 |
and Kernel-side components, and allow them to communicate with each other. |
| 1840 |
To do this, IPython adds a notion of a ``Comm``, which exists on both sides, |
| 1841 |
and can communicate in either direction. |
| 1842 |
|
| 1843 |
These messages are fully symmetrical - both the Kernel and the Frontend can send each message, |
| 1844 |
and no messages expect a reply. The Kernel listens for these messages on the Shell channel, |
| 1845 |
and the Frontend listens for them on the IOPub channel. |
| 1846 |
|
| 1847 |
Opening a Comm |
| 1848 |
-------------- |
| 1849 |
|
| 1850 |
Opening a Comm produces a ``comm_open`` message, to be sent to the other side:: |
| 1851 |
|
| 1852 |
{ |
| 1853 |
'comm_id' : 'u-u-i-d', |
| 1854 |
'target_name' : 'my_comm', |
| 1855 |
'data' : {} |
| 1856 |
} |
| 1857 |
|
| 1858 |
Every Comm has an ID and a target name. |
| 1859 |
The code handling the message on the receiving side is responsible for maintaining a mapping |
| 1860 |
of target_name keys to constructors. |
| 1861 |
After a ``comm_open`` message has been sent, |
| 1862 |
there should be a corresponding Comm instance on both sides. |
| 1863 |
The ``data`` key is always a dict and can be any extra JSON information used in |
| 1864 |
initialization of the comm. |
| 1865 |
|
| 1866 |
If the ``target_name`` key is not found on the receiving side, |
| 1867 |
then it should immediately reply with a ``comm_close`` message to avoid an inconsistent state. |
| 1868 |
|
| 1869 |
|
| 1870 |
Comm Messages |
| 1871 |
------------- |
| 1872 |
|
| 1873 |
Comm messages are one-way communications to update comm state, |
| 1874 |
used for synchronizing widget state, or simply requesting actions of a comm's counterpart. |
| 1875 |
|
| 1876 |
Essentially, each comm pair defines their own message specification implemented |
| 1877 |
inside the ``data`` dict. |
| 1878 |
|
| 1879 |
There are no expected replies (of course, one side can send another ``comm_msg`` in reply). |
| 1880 |
|
| 1881 |
Message type: ``comm_msg``:: |
| 1882 |
|
| 1883 |
{ |
| 1884 |
'comm_id' : 'u-u-i-d', |
| 1885 |
'data' : {} |
| 1886 |
} |
| 1887 |
|
| 1888 |
Tearing Down Comms |
| 1889 |
------------------ |
| 1890 |
|
| 1891 |
Since comms live on both sides, when a comm is destroyed the other side must be notified. |
| 1892 |
This is done with a ``comm_close`` message. |
| 1893 |
|
| 1894 |
Message type: ``comm_close``:: |
| 1895 |
|
| 1896 |
{ |
| 1897 |
'comm_id' : 'u-u-i-d', |
| 1898 |
'data' : {} |
| 1899 |
} |
| 1900 |
|
| 1901 |
Output Side Effects |
| 1902 |
------------------- |
| 1903 |
|
| 1904 |
Since comm messages can execute arbitrary user code, |
| 1905 |
handlers should set the parent header and publish status busy / idle, |
| 1906 |
just like an execute request. |
| 1907 |
|
| 1908 |
|
| 1909 |
Changelog |
| 1910 |
========= |
| 1911 |
|
| 1912 |
5.5 |
| 1913 |
--- |
| 1914 |
|
| 1915 |
- Added ``debug_request/reply`` and ``debug_event`` messages. |
| 1916 |
- Replaced **IOPUB** PUB socket with an XPUB socket. |
| 1917 |
- Added support for :ref:`kernel info <msging_kernel_info>` request on the Control channel. |
| 1918 |
- Added ``supported_features`` in :ref:`kernel info <msging_kernel_info>` reply messages. |
| 1919 |
- Deprecated ``debugger`` in :ref:`kernel info <msging_kernel_info>` reply messages as |
| 1920 |
replaced with ``supported_features``. |
| 1921 |
- Added ``create_subshell``, ``delete_subshell`` and ``list_subshell`` messages. |
| 1922 |
- Added ``copyToGlobals`` debug request |
| 1923 |
|
| 1924 |
5.4 |
| 1925 |
--- |
| 1926 |
|
| 1927 |
- Sending a ``shutdown_request`` message on the ``shell`` channel is deprecated. |
| 1928 |
It should be sent on the control channel. |
| 1929 |
|
| 1930 |
5.3 |
| 1931 |
--- |
| 1932 |
|
| 1933 |
- Kernels can now opt to be interrupted by a message sent on the control channel |
| 1934 |
instead of a system signal. See :ref:`kernelspecs` and :ref:`msging_interrupt`. |
| 1935 |
|
| 1936 |
5.2 |
| 1937 |
--- |
| 1938 |
|
| 1939 |
- Resolve ambiguity of ``cursor_pos`` field in the presence |
| 1940 |
of unicode surrogate pairs. |
| 1941 |
In 5.2, cursor_pos **must be** the actual encoding-independent offset in unicode codepoints. |
| 1942 |
|
| 1943 |
.. seealso:: |
| 1944 |
|
| 1945 |
:ref:`cursor_pos_unicode_note` |
| 1946 |
|
| 1947 |
5.1 |
| 1948 |
--- |
| 1949 |
|
| 1950 |
- ``date`` in the header was accidentally omitted from the spec prior to 5.1, |
| 1951 |
but it has always been in the canonical implementation, |
| 1952 |
so implementers are strongly encouraged to include it. |
| 1953 |
It is mandatory in 5.1. |
| 1954 |
- ``status='abort'`` in replies has not proved useful, and is considered deprecated. |
| 1955 |
Kernels should send ``status='error'`` instead. |
| 1956 |
- ``comm_info_request/reply`` added |
| 1957 |
- ``connect_request/reply`` have not proved useful, and are considered deprecated. |
| 1958 |
Kernels are not expected to implement handlers for this message. |
| 1959 |
- new ``transient`` field in ``display_data`` |
| 1960 |
- new ``update_display_data`` message |
| 1961 |
|
| 1962 |
5.0 |
| 1963 |
--- |
| 1964 |
|
| 1965 |
General changes: |
| 1966 |
|
| 1967 |
- ``version`` key added to message headers |
| 1968 |
- busy and idle status messages should be sent before/after handling every request, |
| 1969 |
not just execution |
| 1970 |
|
| 1971 |
Message renames to remove Python-specific-ness: |
| 1972 |
|
| 1973 |
- ``pyin`` message renamed to ``execute_input`` |
| 1974 |
- ``pyerr`` renamed to ``error`` |
| 1975 |
- ``object_info_request/reply`` messages renamed to ``inspect_request/reply`` |
| 1976 |
|
| 1977 |
Kernel info: |
| 1978 |
|
| 1979 |
- versions changed from lists of integers to strings |
| 1980 |
- ``ipython_version`` is removed |
| 1981 |
- ``language_info``, ``implementation``, ``implementation_version``, ``banner`` |
| 1982 |
and ``help_links`` keys are added. |
| 1983 |
- ``language_version`` is moved to ``language_info.version`` |
| 1984 |
- ``language`` is moved to ``language_info.name`` |
| 1985 |
|
| 1986 |
Execution: |
| 1987 |
|
| 1988 |
- ``user_variables`` is removed from ``execute_request/reply`` because it is |
| 1989 |
redundant with ``user_expressions`` |
| 1990 |
- ``password`` key added to ``input_request`` |
| 1991 |
|
| 1992 |
Output: |
| 1993 |
|
| 1994 |
- ``data`` key in stream messages renamed to ``text`` for consistency with the |
| 1995 |
notebook format. |
| 1996 |
- ``application/json`` in mimebundles should be unpacked JSON data, |
| 1997 |
not a double-serialized JSON string. |
| 1998 |
|
| 1999 |
Inspection: |
| 2000 |
|
| 2001 |
- ``name`` key in ``inspect_request`` replaced with ``code`` and ``cursor_pos``, |
| 2002 |
moving the lexing responsibility to the kernel. |
| 2003 |
- ``object_info_reply`` is now a mimebundle, |
| 2004 |
allowing formatting decisions to be made by the kernel. |
| 2005 |
|
| 2006 |
Completion: |
| 2007 |
|
| 2008 |
- ``complete_request``: ``line``, ``block``, and ``text`` keys are removed in |
| 2009 |
favor of a single ``code`` for context. |
| 2010 |
Lexing is up to the kernel. |
| 2011 |
- ``complete_reply``: |
| 2012 |
- ``matched_text`` is removed in favor of ``cursor_start`` and ``cursor_end``. |
| 2013 |
- ``metadata`` is added for extended information. |
| 2014 |
- new ``is_complete_request`` and ``is_complete_reply`` messages |
| 2015 |
|
| 2016 |
4.1 |
| 2017 |
--- |
| 2018 |
|
| 2019 |
- ``comm_open/close/msg`` messages added |
| 2020 |
- ``clear_output``: ``stdout``, ``stderr``, and ``display`` boolean keys for |
| 2021 |
selective clearing are removed, |
| 2022 |
and ``wait`` is added. |
| 2023 |
The selective clearing keys are ignored in v4 and the default behavior remains the same, |
| 2024 |
so v4 ``clear_output`` messages will be safely handled by a v4.1 frontend. |
| 2025 |
|
| 2026 |
|
| 2027 |
Notes |
| 2028 |
===== |
| 2029 |
|
| 2030 |
.. _cursor_pos_unicode_note: |
| 2031 |
|
| 2032 |
``cursor_pos`` and unicode offsets |
| 2033 |
---------------------------------- |
| 2034 |
|
| 2035 |
Many frontends, especially those implemented in javascript, |
| 2036 |
reported cursor_pos as the interpreter's string index, |
| 2037 |
which is not the same as the unicode character offset if the interpreter uses |
| 2038 |
UTF-16 (e.g. javascript or Python 2 on macOS), |
| 2039 |
which stores "astral-plane" characters such as ``𝐚 (U+1D41A)`` as surrogate pairs, |
| 2040 |
taking up two indices instead of one, causing a unicode offset |
| 2041 |
drift of one per astral-plane character. |
| 2042 |
Not all frontends have this behavior, however, |
| 2043 |
and after JSON serialization information about which encoding was used |
| 2044 |
when calculating the offset is lost, |
| 2045 |
so assuming ``cursor_pos`` is calculated in UTF-16 could result in a similarly incorrect offset |
| 2046 |
for frontends that did the right thing. |
| 2047 |
|
| 2048 |
For this reason, in protocol versions prior to 5.2, ``cursor_pos`` |
| 2049 |
is officially ambiguous in the presence of astral plane unicode characters. |
| 2050 |
Frontends claiming to implement protocol 5.2 **MUST** identify cursor_pos as |
| 2051 |
the encoding-independent unicode character offset. |
| 2052 |
Kernels may choose to expect the UTF-16 offset from requests implementing |
| 2053 |
protocol 5.1 and earlier, in order to behave correctly with the most popular |
| 2054 |
frontends. |
| 2055 |
But they should know that doing so *introduces* the inverse bug for the |
| 2056 |
frontends that do not have this bug. |
| 2057 |
|
| 2058 |
As an example, use a python3 kernel and evaluate ``𨭎𨭎𨭎𨭎𨭎 = 10``. Then type |
| 2059 |
``𨭎𨭎`` followed by the tab key and see if it properly completes. |
| 2060 |
|
| 2061 |
Known affected frontends (as of 2017-06): |
| 2062 |
|
| 2063 |
- Jupyter Notebook < 5.1 |
| 2064 |
- JupyterLab < 0.24 |
| 2065 |
- nteract < 0.2.0 |
| 2066 |
- Jupyter Console and QtConsole with Python 2 on macOS and Windows |
| 2067 |
|
| 2068 |
Known *not* affected frontends: |
| 2069 |
|
| 2070 |
- QtConsole, Jupyter Console with Python 3 or Python 2 on Linux, CoCalc |
| 2071 |
|
| 2072 |
.. seealso:: |
| 2073 |
|
| 2074 |
`Discussion on GitHub <https://github.com/jupyter/jupyter_client/issues/259>`_ |
| 2075 |
|
| 2076 |
|
| 2077 |
.. _ZeroMQ: http://zeromq.org |
| 2078 |
.. _nteract: https://nteract.io |