##// END OF EJS Templates
Merge pull request #7563 from minrk/kernel-msg-docs...
Thomas Kluyver -
r20115:52e60683 merge
parent child Browse files
Show More
@@ -1,158 +1,164 b''
1 {% extends "page.html" %}
1 {% extends "page.html" %}
2
2
3 {% block title %}{{page_title}}{% endblock %}
3 {% block title %}{{page_title}}{% endblock %}
4
4
5
5
6 {% block params %}
6 {% block params %}
7
7
8 data-base-url="{{base_url}}"
8 data-base-url="{{base_url}}"
9 data-notebook-path="{{notebook_path}}"
9 data-notebook-path="{{notebook_path}}"
10 data-terminals-available="{{terminals_available}}"
10 data-terminals-available="{{terminals_available}}"
11
11
12 {% endblock %}
12 {% endblock %}
13
13
14
14
15 {% block site %}
15 {% block site %}
16
16
17 <div id="ipython-main-app" class="container">
17 <div id="ipython-main-app" class="container">
18 <div id="tab_content" class="tabbable">
18 <div id="tab_content" class="tabbable">
19 <ul id="tabs" class="nav nav-tabs">
19 <ul id="tabs" class="nav nav-tabs">
20 <li class="active"><a href="#notebooks" data-toggle="tab">Files</a></li>
20 <li class="active"><a href="#notebooks" data-toggle="tab">Files</a></li>
21 <li><a href="#running" data-toggle="tab">Running</a></li>
21 <li><a href="#running" data-toggle="tab">Running</a></li>
22 <li><a href="#clusters" data-toggle="tab">Clusters</a></li>
22 <li><a href="#clusters" data-toggle="tab">Clusters</a></li>
23 </ul>
23 </ul>
24 <div class="tab-content">
24 <div class="tab-content">
25 <div id="notebooks" class="tab-pane active">
25 <div id="notebooks" class="tab-pane active">
26 <div id="notebook_toolbar" class="row">
26 <div id="notebook_toolbar" class="row">
27 <div class="col-sm-8 no-padding">
27 <div class="col-sm-8 no-padding">
28 <form id='alternate_upload' class='alternate_upload'>
28 <form id='alternate_upload' class='alternate_upload'>
29 <span id="notebook_list_info">
29 <span id="notebook_list_info">
30 To import a notebook, drag the file onto the listing below or
30 To import a notebook, drag the file onto the listing below or
31 <span class="input-overlay">
31 <span class="input-overlay">
32 <input type="file" name="datafile" class="fileinput" multiple='multiple'>
32 <input type="file" name="datafile" class="fileinput" multiple='multiple'>
33 click here.
33 click here.
34 </span>
34 </span>
35 </span>
35 </span>
36 </form>
36 </form>
37 </div>
37 </div>
38 <div class="col-sm-4 no-padding tree-buttons">
38 <div class="col-sm-4 no-padding tree-buttons">
39 <div class="pull-right">
39 <div class="pull-right">
40 <div id="new-buttons" class="btn-group">
40 <div id="new-buttons" class="btn-group">
41 <button class="dropdown-toggle btn btn-default btn-xs" data-toggle="dropdown">
41 <button class="dropdown-toggle btn btn-default btn-xs" data-toggle="dropdown">
42 <span>New</span>
42 <span>New</span>
43 <span class="caret"></span>
43 <span class="caret"></span>
44 </button>
44 </button>
45 <ul id="new-menu" class="dropdown-menu">
45 <ul id="new-menu" class="dropdown-menu">
46 <li role="presentation" id="new-file">
46 <li role="presentation" id="new-file">
47 <a role="menuitem" tabindex="-1" href="#">File</a>
47 <a role="menuitem" tabindex="-1" href="#">File</a>
48 </li>
48 </li>
49 <li role="presentation" id="new-folder">
49 <li role="presentation" id="new-folder">
50 <a role="menuitem" tabindex="-1" href="#">Folder</a>
50 <a role="menuitem" tabindex="-1" href="#">Folder</a>
51 </li>
51 </li>
52 {% if terminals_available %}
52 {% if terminals_available %}
53 <li role="presentation" id="new-terminal">
53 <li role="presentation" id="new-terminal">
54 <a role="menuitem" tabindex="-1" href="#">Terminal</a>
54 <a role="menuitem" tabindex="-1" href="#">Terminal</a>
55 </li>
55 </li>
56 {% else %}
57 <li role="presentation" id="new-terminal-disabled" class="disabled">
58 <a role="menuitem" tabindex="-1" href="#">Terminals Unavailable</a>
59 </li>
56 {% endif %}
60 {% endif %}
57 <li role="presentation" class="divider"></li>
61 <li role="presentation" class="divider"></li>
58 <li role="presentation" class="dropdown-header" id="notebook-kernels">Notebooks</li>
62 <li role="presentation" class="dropdown-header" id="notebook-kernels">Notebooks</li>
59 </ul>
63 </ul>
60 </div>
64 </div>
61 <div class="btn-group">
65 <div class="btn-group">
62 <button id="refresh_notebook_list" title="Refresh notebook list" class="btn btn-default btn-xs"><i class="fa fa-refresh"></i></button>
66 <button id="refresh_notebook_list" title="Refresh notebook list" class="btn btn-default btn-xs"><i class="fa fa-refresh"></i></button>
63 </div>
67 </div>
64 </div>
68 </div>
65 </div>
69 </div>
66 </div>
70 </div>
67 <div id="notebook_list">
71 <div id="notebook_list">
68 <div id="notebook_list_header" class="row list_header">
72 <div id="notebook_list_header" class="row list_header">
69 <div id="project_name">
73 <div id="project_name">
70 <ul class="breadcrumb">
74 <ul class="breadcrumb">
71 <li><a href="{{breadcrumbs[0][0]}}"><i class="fa fa-home"></i></a></li>
75 <li><a href="{{breadcrumbs[0][0]}}"><i class="fa fa-home"></i></a></li>
72 {% for crumb in breadcrumbs[1:] %}
76 {% for crumb in breadcrumbs[1:] %}
73 <li><a href="{{crumb[0]}}">{{crumb[1]}}</a></li>
77 <li><a href="{{crumb[0]}}">{{crumb[1]}}</a></li>
74 {% endfor %}
78 {% endfor %}
75 </ul>
79 </ul>
76 </div>
80 </div>
77 </div>
81 </div>
78 </div>
82 </div>
79 </div>
83 </div>
80 <div id="running" class="tab-pane">
84 <div id="running" class="tab-pane">
81 <div id="running_toolbar" class="row">
85 <div id="running_toolbar" class="row">
82 <div class="col-sm-8 no-padding">
86 <div class="col-sm-8 no-padding">
83 <span id="running_list_info">Currently running Jupyter processes</span>
87 <span id="running_list_info">Currently running Jupyter processes</span>
84 </div>
88 </div>
85 <div class="col-sm-4 no-padding tree-buttons">
89 <div class="col-sm-4 no-padding tree-buttons">
86 <span id="running_buttons" class="pull-right">
90 <span id="running_buttons" class="pull-right">
87 <button id="refresh_running_list" title="Refresh running list" class="btn btn-default btn-xs"><i class="fa fa-refresh"></i></button>
91 <button id="refresh_running_list" title="Refresh running list" class="btn btn-default btn-xs"><i class="fa fa-refresh"></i></button>
88 </span>
92 </span>
89 </div>
93 </div>
90 </div>
94 </div>
91 <div class="panel-group" id="accordion" >
95 <div class="panel-group" id="accordion" >
92 <div class="panel panel-default">
96 <div class="panel panel-default">
93 <div class="panel-heading">
97 <div class="panel-heading">
94 <a data-toggle="collapse" data-target="#collapseOne" href="#">
98 <a data-toggle="collapse" data-target="#collapseOne" href="#">
95 Terminals
99 Terminals
96 </a>
100 </a>
97 </div>
101 </div>
98 <div id="collapseOne" class=" collapse in">
102 <div id="collapseOne" class=" collapse in">
99 <div class="panel-body">
103 <div class="panel-body">
100 {% if terminals_available %}
104 <div id="terminal_list">
101 <div id="terminal_list">
105 <div id="terminal_list_header" class="row list_header">
102 <div id="terminal_list_header" class="row list_header">
106 {% if terminals_available %}
103 <div> There are no terminals running. </div>
107 <div> There are no terminals running. </div>
104 </div>
108 {% else %}
105 </div>
109 <div> Terminals are unavailable. </div>
106 {% endif %}
110 {% endif %}
111 </div>
112 </div>
107 </div>
113 </div>
108 </div>
114 </div>
109 </div>
115 </div>
110 <div class="panel panel-default">
116 <div class="panel panel-default">
111 <div class="panel-heading">
117 <div class="panel-heading">
112 <a data-toggle="collapse" data-target="#collapseTwo" href="#">
118 <a data-toggle="collapse" data-target="#collapseTwo" href="#">
113 Notebooks
119 Notebooks
114 </a>
120 </a>
115 </div>
121 </div>
116 <div id="collapseTwo" class=" collapse in">
122 <div id="collapseTwo" class=" collapse in">
117 <div class="panel-body">
123 <div class="panel-body">
118 <div id="running_list">
124 <div id="running_list">
119 <div id="running_list_header" class="row list_header">
125 <div id="running_list_header" class="row list_header">
120 <div> There are no notebooks running. </div>
126 <div> There are no notebooks running. </div>
121 </div>
127 </div>
122 </div>
128 </div>
123 </div>
129 </div>
124 </div>
130 </div>
125 </div>
131 </div>
126 </div>
132 </div>
127 </div>
133 </div>
128 <div id="clusters" class="tab-pane">
134 <div id="clusters" class="tab-pane">
129 <div id="cluster_toolbar" class="row">
135 <div id="cluster_toolbar" class="row">
130 <div class="col-xs-8 no-padding">
136 <div class="col-xs-8 no-padding">
131 <span id="cluster_list_info">IPython parallel computing clusters</span>
137 <span id="cluster_list_info">IPython parallel computing clusters</span>
132 </div>
138 </div>
133 <div class="col-xs-4 no-padding tree-buttons">
139 <div class="col-xs-4 no-padding tree-buttons">
134 <span id="cluster_buttons" class="pull-right">
140 <span id="cluster_buttons" class="pull-right">
135 <button id="refresh_cluster_list" title="Refresh cluster list" class="btn btn-default btn-xs"><i class="fa fa-refresh"></i></button>
141 <button id="refresh_cluster_list" title="Refresh cluster list" class="btn btn-default btn-xs"><i class="fa fa-refresh"></i></button>
136 </span>
142 </span>
137 </div>
143 </div>
138 </div>
144 </div>
139 <div id="cluster_list">
145 <div id="cluster_list">
140 <div id="cluster_list_header" class="row list_header">
146 <div id="cluster_list_header" class="row list_header">
141 <div class="profile_col col-xs-4">profile</div>
147 <div class="profile_col col-xs-4">profile</div>
142 <div class="status_col col-xs-3">status</div>
148 <div class="status_col col-xs-3">status</div>
143 <div class="engines_col col-xs-3" title="Enter the number of engines to start or empty for default"># of engines</div>
149 <div class="engines_col col-xs-3" title="Enter the number of engines to start or empty for default"># of engines</div>
144 <div class="action_col col-xs-2">action</div>
150 <div class="action_col col-xs-2">action</div>
145 </div>
151 </div>
146 </div>
152 </div>
147 </div>
153 </div>
148 </div><!-- class:tab-content -->
154 </div><!-- class:tab-content -->
149 </div><!-- id:tab_content -->
155 </div><!-- id:tab_content -->
150 </div><!-- ipython-main-app -->
156 </div><!-- ipython-main-app -->
151
157
152 {% endblock %}
158 {% endblock %}
153
159
154 {% block script %}
160 {% block script %}
155 {{super()}}
161 {{super()}}
156
162
157 <script src="{{ static_url("tree/js/main.js") }}" type="text/javascript" charset="utf-8"></script>
163 <script src="{{ static_url("tree/js/main.js") }}" type="text/javascript" charset="utf-8"></script>
158 {% endblock %}
164 {% endblock %}
@@ -1,1208 +1,1216 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 5.0.
12 The current version of the specification is 5.0.
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. Shell: this single ROUTER socket allows multiple incoming connections from
41 1. Shell: this single ROUTER socket allows multiple incoming connections from
42 frontends, and this is the socket where requests for code execution, object
42 frontends, and this is the socket where requests for code execution, object
43 information, prompts, etc. are made to the kernel by any frontend. The
43 information, prompts, etc. are made to the kernel by any frontend. The
44 communication on this socket is a sequence of request/reply actions from
44 communication on this socket is a sequence of request/reply actions from
45 each frontend and the kernel.
45 each frontend and the kernel.
46
46
47 2. IOPub: this socket is the 'broadcast channel' where the kernel publishes all
47 2. IOPub: this socket is the 'broadcast channel' where the kernel publishes all
48 side effects (stdout, stderr, etc.) as well as the requests coming from any
48 side effects (stdout, stderr, etc.) as well as the requests coming from any
49 client over the shell socket and its own requests on the stdin socket. There
49 client over the shell socket and its own requests on the stdin socket. There
50 are a number of actions in Python which generate side effects: :func:`print`
50 are a number of actions in Python which generate side effects: :func:`print`
51 writes to ``sys.stdout``, errors generate tracebacks, etc. Additionally, in
51 writes to ``sys.stdout``, errors generate tracebacks, etc. Additionally, in
52 a multi-client scenario, we want all frontends to be able to know what each
52 a multi-client scenario, we want all frontends to be able to know what each
53 other has sent to the kernel (this can be useful in collaborative scenarios,
53 other has sent to the kernel (this can be useful in collaborative scenarios,
54 for example). This socket allows both side effects and the information
54 for example). This socket allows both side effects and the information
55 about communications taking place with one client over the shell channel
55 about communications taking place with one client over the shell channel
56 to be made available to all clients in a uniform manner.
56 to be made available to all clients in a uniform manner.
57
57
58 3. stdin: this ROUTER socket is connected to all frontends, and it allows
58 3. stdin: this ROUTER socket is connected to all frontends, and it allows
59 the kernel to request input from the active frontend when :func:`raw_input` is called.
59 the kernel to request input from the active frontend when :func:`raw_input` is called.
60 The frontend that executed the code has a DEALER socket that acts as a 'virtual keyboard'
60 The frontend that executed the code has a DEALER socket that acts as a 'virtual keyboard'
61 for the kernel while this communication is happening (illustrated in the
61 for the kernel while this communication is happening (illustrated in the
62 figure by the black outline around the central keyboard). In practice,
62 figure by the black outline around the central keyboard). In practice,
63 frontends may display such kernel requests using a special input widget or
63 frontends may display such kernel requests using a special input widget or
64 otherwise indicating that the user is to type input for the kernel instead
64 otherwise indicating that the user is to type input for the kernel instead
65 of normal commands in the frontend.
65 of normal commands in the frontend.
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 4. Control: This channel is identical to Shell, but operates on a separate socket,
72 4. Control: This channel is identical to Shell, but operates on a separate socket,
73 to allow important messages to avoid queueing behind execution requests (e.g. shutdown or abort).
73 to allow important messages to avoid queueing behind execution requests (e.g. shutdown or abort).
74
74
75 The actual format of the messages allowed on each of these channels is
75 The actual format of the messages allowed on each of these channels is
76 specified below. Messages are dicts of dicts with string keys and values that
76 specified below. Messages are dicts of dicts with string keys and values that
77 are reasonably representable in JSON. Our current implementation uses JSON
77 are reasonably representable in JSON. Our current implementation uses JSON
78 explicitly as its message format, but this shouldn't be considered a permanent
78 explicitly as its message format, but this shouldn't be considered a permanent
79 feature. As we've discovered that JSON has non-trivial performance issues due
79 feature. As we've discovered that JSON has non-trivial performance issues due
80 to excessive copying, we may in the future move to a pure pickle-based raw
80 to excessive copying, we may in the future move to a pure pickle-based raw
81 message format. However, it should be possible to easily convert from the raw
81 message format. However, it should be possible to easily convert from the raw
82 objects to JSON, since we may have non-python clients (e.g. a web frontend).
82 objects to JSON, since we may have non-python clients (e.g. a web frontend).
83 As long as it's easy to make a JSON version of the objects that is a faithful
83 As long as it's easy to make a JSON version of the objects that is a faithful
84 representation of all the data, we can communicate with such clients.
84 representation of all the data, we can communicate with such clients.
85
85
86 .. Note::
86 .. Note::
87
87
88 Not all of these have yet been fully fleshed out, but the key ones are, see
88 Not all of these have yet been fully fleshed out, but the key ones are, see
89 kernel and frontend files for actual implementation details.
89 kernel and frontend files for actual implementation details.
90
90
91 General Message Format
91 General Message Format
92 ======================
92 ======================
93
93
94 A message is defined by the following four-dictionary structure::
94 A message is defined by the following four-dictionary structure::
95
95
96 {
96 {
97 # The message header contains a pair of unique identifiers for the
97 # The message header contains a pair of unique identifiers for the
98 # originating session and the actual message id, in addition to the
98 # originating session and the actual message id, in addition to the
99 # username for the process that generated the message. This is useful in
99 # username for the process that generated the message. This is useful in
100 # collaborative settings where multiple users may be interacting with the
100 # collaborative settings where multiple users may be interacting with the
101 # same kernel simultaneously, so that frontends can label the various
101 # same kernel simultaneously, so that frontends can label the various
102 # messages in a meaningful way.
102 # messages in a meaningful way.
103 'header' : {
103 'header' : {
104 'msg_id' : uuid,
104 'msg_id' : uuid,
105 'username' : str,
105 'username' : str,
106 'session' : uuid,
106 'session' : uuid,
107 # All recognized message type strings are listed below.
107 # All recognized message type strings are listed below.
108 'msg_type' : str,
108 'msg_type' : str,
109 # the message protocol version
109 # the message protocol version
110 'version' : '5.0',
110 'version' : '5.0',
111 },
111 },
112
112
113 # In a chain of messages, the header from the parent is copied so that
113 # In a chain of messages, the header from the parent is copied so that
114 # clients can track where messages come from.
114 # clients can track where messages come from.
115 'parent_header' : dict,
115 'parent_header' : dict,
116
116
117 # Any metadata associated with the message.
117 # Any metadata associated with the message.
118 'metadata' : dict,
118 'metadata' : dict,
119
119
120 # The actual content of the message must be a dict, whose structure
120 # The actual content of the message must be a dict, whose structure
121 # depends on the message type.
121 # depends on the message type.
122 'content' : dict,
122 'content' : dict,
123 }
123 }
124
124
125 .. versionchanged:: 5.0
125 .. versionchanged:: 5.0
126
126
127 ``version`` key added to the header.
127 ``version`` key added to the header.
128
128
129 .. _wire_protocol:
129 .. _wire_protocol:
130
130
131 The Wire Protocol
131 The Wire Protocol
132 =================
132 =================
133
133
134
134
135 This message format exists at a high level,
135 This message format exists at a high level,
136 but does not describe the actual *implementation* at the wire level in zeromq.
136 but does not describe the actual *implementation* at the wire level in zeromq.
137 The canonical implementation of the message spec is our :class:`~IPython.kernel.zmq.session.Session` class.
137 The canonical implementation of the message spec is our :class:`~IPython.kernel.zmq.session.Session` class.
138
138
139 .. note::
139 .. note::
140
140
141 This section should only be relevant to non-Python consumers of the protocol.
141 This section should only be relevant to non-Python consumers of the protocol.
142 Python consumers should simply import and use IPython's own implementation of the wire protocol
142 Python consumers should simply import and use IPython's own implementation of the wire protocol
143 in the :class:`IPython.kernel.zmq.session.Session` object.
143 in the :class:`IPython.kernel.zmq.session.Session` object.
144
144
145 Every message is serialized to a sequence of at least six blobs of bytes:
145 Every message is serialized to a sequence of at least six blobs of bytes:
146
146
147 .. sourcecode:: python
147 .. sourcecode:: python
148
148
149 [
149 [
150 b'u-u-i-d', # zmq identity(ies)
150 b'u-u-i-d', # zmq identity(ies)
151 b'<IDS|MSG>', # delimiter
151 b'<IDS|MSG>', # delimiter
152 b'baddad42', # HMAC signature
152 b'baddad42', # HMAC signature
153 b'{header}', # serialized header dict
153 b'{header}', # serialized header dict
154 b'{parent_header}', # serialized parent header dict
154 b'{parent_header}', # serialized parent header dict
155 b'{metadata}', # serialized metadata dict
155 b'{metadata}', # serialized metadata dict
156 b'{content}, # serialized content dict
156 b'{content}, # serialized content dict
157 b'blob', # extra raw data buffer(s)
157 b'blob', # extra raw data buffer(s)
158 ...
158 ...
159 ]
159 ]
160
160
161 The front of the message is the ZeroMQ routing prefix,
161 The front of the message is the ZeroMQ routing prefix,
162 which can be zero or more socket identities.
162 which can be zero or more socket identities.
163 This is every piece of the message prior to the delimiter key ``<IDS|MSG>``.
163 This is every piece of the message prior to the delimiter key ``<IDS|MSG>``.
164 In the case of IOPub, there should be just one prefix component,
164 In the case of IOPub, there should be just one prefix component,
165 which is the topic for IOPub subscribers, e.g. ``execute_result``, ``display_data``.
165 which is the topic for IOPub subscribers, e.g. ``execute_result``, ``display_data``.
166
166
167 .. note::
167 .. note::
168
168
169 In most cases, the IOPub topics are irrelevant and completely ignored,
169 In most cases, the IOPub topics are irrelevant and completely ignored,
170 because frontends just subscribe to all topics.
170 because frontends just subscribe to all topics.
171 The convention used in the IPython kernel is to use the msg_type as the topic,
171 The convention used in the IPython kernel is to use the msg_type as the topic,
172 and possibly extra information about the message, e.g. ``execute_result`` or ``stream.stdout``
172 and possibly extra information about the message, e.g. ``execute_result`` or ``stream.stdout``
173
173
174 After the delimiter is the `HMAC`_ signature of the message, used for authentication.
174 After the delimiter is the `HMAC`_ signature of the message, used for authentication.
175 If authentication is disabled, this should be an empty string.
175 If authentication is disabled, this should be an empty string.
176 By default, the hashing function used for computing these signatures is sha256.
176 By default, the hashing function used for computing these signatures is sha256.
177
177
178 .. _HMAC: http://en.wikipedia.org/wiki/HMAC
178 .. _HMAC: http://en.wikipedia.org/wiki/HMAC
179
179
180 .. note::
180 .. note::
181
181
182 To disable authentication and signature checking,
182 To disable authentication and signature checking,
183 set the `key` field of a connection file to an empty string.
183 set the `key` field of a connection file to an empty string.
184
184
185 The signature is the HMAC hex digest of the concatenation of:
185 The signature is the HMAC hex digest of the concatenation of:
186
186
187 - A shared key (typically the ``key`` field of a connection file)
187 - A shared key (typically the ``key`` field of a connection file)
188 - The serialized header dict
188 - The serialized header dict
189 - The serialized parent header dict
189 - The serialized parent header dict
190 - The serialized metadata dict
190 - The serialized metadata dict
191 - The serialized content dict
191 - The serialized content dict
192
192
193 In Python, this is implemented via:
193 In Python, this is implemented via:
194
194
195 .. sourcecode:: python
195 .. sourcecode:: python
196
196
197 # once:
197 # once:
198 digester = HMAC(key, digestmod=hashlib.sha256)
198 digester = HMAC(key, digestmod=hashlib.sha256)
199
199
200 # for each message
200 # for each message
201 d = digester.copy()
201 d = digester.copy()
202 for serialized_dict in (header, parent, metadata, content):
202 for serialized_dict in (header, parent, metadata, content):
203 d.update(serialized_dict)
203 d.update(serialized_dict)
204 signature = d.hexdigest()
204 signature = d.hexdigest()
205
205
206 After the signature is the actual message, always in four frames of bytes.
206 After the signature is the actual message, always in four frames of bytes.
207 The four dictionaries that compose a message are serialized separately,
207 The four dictionaries that compose a message are serialized separately,
208 in the order of header, parent header, metadata, and content.
208 in the order of header, parent header, metadata, and content.
209 These can be serialized by any function that turns a dict into bytes.
209 These can be serialized by any function that turns a dict into bytes.
210 The default and most common serialization is JSON, but msgpack and pickle
210 The default and most common serialization is JSON, but msgpack and pickle
211 are common alternatives.
211 are common alternatives.
212
212
213 After the serialized dicts are zero to many raw data buffers,
213 After the serialized dicts are zero to many raw data buffers,
214 which can be used by message types that support binary data (mainly apply and data_pub).
214 which can be used by message types that support binary data (mainly apply and data_pub).
215
215
216
216
217 Python functional API
217 Python functional API
218 =====================
218 =====================
219
219
220 As messages are dicts, they map naturally to a ``func(**kw)`` call form. We
220 As messages are dicts, they map naturally to a ``func(**kw)`` call form. We
221 should develop, at a few key points, functional forms of all the requests that
221 should develop, at a few key points, functional forms of all the requests that
222 take arguments in this manner and automatically construct the necessary dict
222 take arguments in this manner and automatically construct the necessary dict
223 for sending.
223 for sending.
224
224
225 In addition, the Python implementation of the message specification extends
225 In addition, the Python implementation of the message specification extends
226 messages upon deserialization to the following form for convenience::
226 messages upon deserialization to the following form for convenience::
227
227
228 {
228 {
229 'header' : dict,
229 'header' : dict,
230 # The msg's unique identifier and type are always stored in the header,
230 # The msg's unique identifier and type are always stored in the header,
231 # but the Python implementation copies them to the top level.
231 # but the Python implementation copies them to the top level.
232 'msg_id' : uuid,
232 'msg_id' : uuid,
233 'msg_type' : str,
233 'msg_type' : str,
234 'parent_header' : dict,
234 'parent_header' : dict,
235 'content' : dict,
235 'content' : dict,
236 'metadata' : dict,
236 'metadata' : dict,
237 }
237 }
238
238
239 All messages sent to or received by any IPython process should have this
239 All messages sent to or received by any IPython process should have this
240 extended structure.
240 extended structure.
241
241
242
242
243 Messages on the shell ROUTER/DEALER sockets
243 Messages on the shell ROUTER/DEALER sockets
244 ===========================================
244 ===========================================
245
245
246 .. _execute:
246 .. _execute:
247
247
248 Execute
248 Execute
249 -------
249 -------
250
250
251 This message type is used by frontends to ask the kernel to execute code on
251 This message type is used by frontends to ask the kernel to execute code on
252 behalf of the user, in a namespace reserved to the user's variables (and thus
252 behalf of the user, in a namespace reserved to the user's variables (and thus
253 separate from the kernel's own internal code and variables).
253 separate from the kernel's own internal code and variables).
254
254
255 Message type: ``execute_request``::
255 Message type: ``execute_request``::
256
256
257 content = {
257 content = {
258 # Source code to be executed by the kernel, one or more lines.
258 # Source code to be executed by the kernel, one or more lines.
259 'code' : str,
259 'code' : str,
260
260
261 # A boolean flag which, if True, signals the kernel to execute
261 # A boolean flag which, if True, signals the kernel to execute
262 # this code as quietly as possible.
262 # this code as quietly as possible.
263 # silent=True forces store_history to be False,
263 # silent=True forces store_history to be False,
264 # and will *not*:
264 # and will *not*:
265 # - broadcast output on the IOPUB channel
265 # - broadcast output on the IOPUB channel
266 # - have an execute_result
266 # - have an execute_result
267 # The default is False.
267 # The default is False.
268 'silent' : bool,
268 'silent' : bool,
269
269
270 # A boolean flag which, if True, signals the kernel to populate history
270 # A boolean flag which, if True, signals the kernel to populate history
271 # The default is True if silent is False. If silent is True, store_history
271 # The default is True if silent is False. If silent is True, store_history
272 # is forced to be False.
272 # is forced to be False.
273 'store_history' : bool,
273 'store_history' : bool,
274
274
275 # A dict mapping names to expressions to be evaluated in the
275 # A dict mapping names to expressions to be evaluated in the
276 # user's dict. The rich display-data representation of each will be evaluated after execution.
276 # user's dict. The rich display-data representation of each will be evaluated after execution.
277 # See the display_data content for the structure of the representation data.
277 # See the display_data content for the structure of the representation data.
278 'user_expressions' : dict,
278 'user_expressions' : dict,
279
279
280 # Some frontends do not support stdin requests.
280 # Some frontends do not support stdin requests.
281 # If raw_input is called from code executed from such a frontend,
281 # If raw_input is called from code executed from such a frontend,
282 # a StdinNotImplementedError will be raised.
282 # a StdinNotImplementedError will be raised.
283 'allow_stdin' : True,
283 'allow_stdin' : True,
284
284
285 # A boolean flag, which, if True, does not abort the execution queue, if an exception is encountered.
285 # A boolean flag, which, if True, does not abort the execution queue, if an exception is encountered.
286 # This allows the queued execution of multiple execute_requests, even if they generate exceptions.
286 # This allows the queued execution of multiple execute_requests, even if they generate exceptions.
287 'stop_on_error' : False,
287 'stop_on_error' : False,
288 }
288 }
289
289
290 .. versionchanged:: 5.0
290 .. versionchanged:: 5.0
291
291
292 ``user_variables`` removed, because it is redundant with user_expressions.
292 ``user_variables`` removed, because it is redundant with user_expressions.
293
293
294 The ``code`` field contains a single string (possibly multiline) to be executed.
294 The ``code`` field contains a single string (possibly multiline) to be executed.
295
295
296 The ``user_expressions`` field deserves a detailed explanation. In the past, IPython had
296 The ``user_expressions`` field deserves a detailed explanation. In the past, IPython had
297 the notion of a prompt string that allowed arbitrary code to be evaluated, and
297 the notion of a prompt string that allowed arbitrary code to be evaluated, and
298 this was put to good use by many in creating prompts that displayed system
298 this was put to good use by many in creating prompts that displayed system
299 status, path information, and even more esoteric uses like remote instrument
299 status, path information, and even more esoteric uses like remote instrument
300 status acquired over the network. But now that IPython has a clean separation
300 status acquired over the network. But now that IPython has a clean separation
301 between the kernel and the clients, the kernel has no prompt knowledge; prompts
301 between the kernel and the clients, the kernel has no prompt knowledge; prompts
302 are a frontend feature, and it should be even possible for different
302 are a frontend feature, and it should be even possible for different
303 frontends to display different prompts while interacting with the same kernel.
303 frontends to display different prompts while interacting with the same kernel.
304 ``user_expressions`` can be used to retrieve this information.
304 ``user_expressions`` can be used to retrieve this information.
305
305
306 Any error in evaluating any expression in ``user_expressions`` will result in
306 Any error in evaluating any expression in ``user_expressions`` will result in
307 only that key containing a standard error message, of the form::
307 only that key containing a standard error message, of the form::
308
308
309 {
309 {
310 'status' : 'error',
310 'status' : 'error',
311 'ename' : 'NameError',
311 'ename' : 'NameError',
312 'evalue' : 'foo',
312 'evalue' : 'foo',
313 'traceback' : ...
313 'traceback' : ...
314 }
314 }
315
315
316 .. Note::
316 .. Note::
317
317
318 In order to obtain the current execution counter for the purposes of
318 In order to obtain the current execution counter for the purposes of
319 displaying input prompts, frontends may make an execution request with an
319 displaying input prompts, frontends may make an execution request with an
320 empty code string and ``silent=True``.
320 empty code string and ``silent=True``.
321
321
322 Upon completion of the execution request, the kernel *always* sends a reply,
322 Upon completion of the execution request, the kernel *always* sends a reply,
323 with a status code indicating what happened and additional data depending on
323 with a status code indicating what happened and additional data depending on
324 the outcome. See :ref:`below <execution_results>` for the possible return
324 the outcome. See :ref:`below <execution_results>` for the possible return
325 codes and associated data.
325 codes and associated data.
326
326
327 .. seealso::
327 .. seealso::
328
328
329 :ref:`execution_semantics`
329 :ref:`execution_semantics`
330
330
331 .. _execution_counter:
331 .. _execution_counter:
332
332
333 Execution counter (prompt number)
333 Execution counter (prompt number)
334 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
334 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
335
335
336 The kernel should have a single, monotonically increasing counter of all execution
336 The kernel should have a single, monotonically increasing counter of all execution
337 requests that are made with ``store_history=True``. This counter is used to populate
337 requests that are made with ``store_history=True``. This counter is used to populate
338 the ``In[n]`` and ``Out[n]`` prompts. The value of this counter will be returned as the
338 the ``In[n]`` and ``Out[n]`` prompts. The value of this counter will be returned as the
339 ``execution_count`` field of all ``execute_reply`` and ``execute_input`` messages.
339 ``execution_count`` field of all ``execute_reply`` and ``execute_input`` messages.
340
340
341 .. _execution_results:
341 .. _execution_results:
342
342
343 Execution results
343 Execution results
344 ~~~~~~~~~~~~~~~~~
344 ~~~~~~~~~~~~~~~~~
345
345
346 Message type: ``execute_reply``::
346 Message type: ``execute_reply``::
347
347
348 content = {
348 content = {
349 # One of: 'ok' OR 'error' OR 'abort'
349 # One of: 'ok' OR 'error' OR 'abort'
350 'status' : str,
350 'status' : str,
351
351
352 # The global kernel counter that increases by one with each request that
352 # The global kernel counter that increases by one with each request that
353 # stores history. This will typically be used by clients to display
353 # stores history. This will typically be used by clients to display
354 # prompt numbers to the user. If the request did not store history, this will
354 # prompt numbers to the user. If the request did not store history, this will
355 # be the current value of the counter in the kernel.
355 # be the current value of the counter in the kernel.
356 'execution_count' : int,
356 'execution_count' : int,
357 }
357 }
358
358
359 When status is 'ok', the following extra fields are present::
359 When status is 'ok', the following extra fields are present::
360
360
361 {
361 {
362 # 'payload' will be a list of payload dicts, and is optional.
362 # 'payload' will be a list of payload dicts, and is optional.
363 # payloads are considered deprecated.
363 # payloads are considered deprecated.
364 # The only requirement of each payload dict is that it have a 'source' key,
364 # The only requirement of each payload dict is that it have a 'source' key,
365 # which is a string classifying the payload (e.g. 'page').
365 # which is a string classifying the payload (e.g. 'page').
366
366
367 'payload' : list(dict),
367 'payload' : list(dict),
368
368
369 # Results for the user_expressions.
369 # Results for the user_expressions.
370 'user_expressions' : dict,
370 'user_expressions' : dict,
371 }
371 }
372
372
373 .. versionchanged:: 5.0
373 .. versionchanged:: 5.0
374
374
375 ``user_variables`` is removed, use user_expressions instead.
375 ``user_variables`` is removed, use user_expressions instead.
376
376
377 When status is 'error', the following extra fields are present::
377 When status is 'error', the following extra fields are present::
378
378
379 {
379 {
380 'ename' : str, # Exception name, as a string
380 'ename' : str, # Exception name, as a string
381 'evalue' : str, # Exception value, as a string
381 'evalue' : str, # Exception value, as a string
382
382
383 # The traceback will contain a list of frames, represented each as a
383 # The traceback will contain a list of frames, represented each as a
384 # string. For now we'll stick to the existing design of ultraTB, which
384 # string. For now we'll stick to the existing design of ultraTB, which
385 # controls exception level of detail statefully. But eventually we'll
385 # controls exception level of detail statefully. But eventually we'll
386 # want to grow into a model where more information is collected and
386 # want to grow into a model where more information is collected and
387 # packed into the traceback object, with clients deciding how little or
387 # packed into the traceback object, with clients deciding how little or
388 # how much of it to unpack. But for now, let's start with a simple list
388 # how much of it to unpack. But for now, let's start with a simple list
389 # of strings, since that requires only minimal changes to ultratb as
389 # of strings, since that requires only minimal changes to ultratb as
390 # written.
390 # written.
391 'traceback' : list,
391 'traceback' : list,
392 }
392 }
393
393
394
394
395 When status is 'abort', there are for now no additional data fields. This
395 When status is 'abort', there are for now no additional data fields. This
396 happens when the kernel was interrupted by a signal.
396 happens when the kernel was interrupted by a signal.
397
397
398 Payloads
398 Payloads
399 ********
399 ********
400
400
401 .. admonition:: Execution payloads
401 .. admonition:: Execution payloads
402
402
403 Payloads are considered deprecated, though their replacement is not yet implemented.
403 Payloads are considered deprecated, though their replacement is not yet implemented.
404
404
405 Payloads are a way to trigger frontend actions from the kernel. Current payloads:
405 Payloads are a way to trigger frontend actions from the kernel. Current payloads:
406
406
407 **page**: display data in a pager.
407 **page**: display data in a pager.
408
408
409 Pager output is used for introspection, or other displayed information that's not considered output.
409 Pager output is used for introspection, or other displayed information that's not considered output.
410 Pager payloads are generally displayed in a separate pane, that can be viewed alongside code,
410 Pager payloads are generally displayed in a separate pane, that can be viewed alongside code,
411 and are not included in notebook documents.
411 and are not included in notebook documents.
412
412
413 .. sourcecode:: python
413 .. sourcecode:: python
414
414
415 {
415 {
416 "source": "page",
416 "source": "page",
417 # mime-bundle of data to display in the pager.
417 # mime-bundle of data to display in the pager.
418 # Must include text/plain.
418 # Must include text/plain.
419 "data": mimebundle,
419 "data": mimebundle,
420 # line offset to start from
420 # line offset to start from
421 "start": int,
421 "start": int,
422 }
422 }
423
423
424 **set_next_input**: create a new output
424 **set_next_input**: create a new output
425
425
426 used to create new cells in the notebook,
426 used to create new cells in the notebook,
427 or set the next input in a console interface.
427 or set the next input in a console interface.
428 The main example being ``%load``.
428 The main example being ``%load``.
429
429
430 .. sourcecode:: python
430 .. sourcecode:: python
431
431
432 {
432 {
433 "source": "set_next_input",
433 "source": "set_next_input",
434 # the text contents of the cell to create
434 # the text contents of the cell to create
435 "text": "some cell content",
435 "text": "some cell content",
436 # If true, replace the current cell in document UIs instead of inserting
436 # If true, replace the current cell in document UIs instead of inserting
437 # a cell. Ignored in console UIs.
437 # a cell. Ignored in console UIs.
438 "replace": bool,
438 "replace": bool,
439 }
439 }
440
440
441 **edit**: open a file for editing.
441 **edit**: open a file for editing.
442
442
443 Triggered by `%edit`. Only the QtConsole currently supports edit payloads.
443 Triggered by `%edit`. Only the QtConsole currently supports edit payloads.
444
444
445 .. sourcecode:: python
445 .. sourcecode:: python
446
446
447 {
447 {
448 "source": "edit",
448 "source": "edit",
449 "filename": "/path/to/file.py", # the file to edit
449 "filename": "/path/to/file.py", # the file to edit
450 "line_number": int, # the line number to start with
450 "line_number": int, # the line number to start with
451 }
451 }
452
452
453 **ask_exit**: instruct the frontend to prompt the user for exit
453 **ask_exit**: instruct the frontend to prompt the user for exit
454
454
455 Allows the kernel to request exit, e.g. via ``%exit`` in IPython.
455 Allows the kernel to request exit, e.g. via ``%exit`` in IPython.
456 Only for console frontends.
456 Only for console frontends.
457
457
458 .. sourcecode:: python
458 .. sourcecode:: python
459
459
460 {
460 {
461 "source": "ask_exit",
461 "source": "ask_exit",
462 # whether the kernel should be left running, only closing the client
462 # whether the kernel should be left running, only closing the client
463 "keepkernel": bool,
463 "keepkernel": bool,
464 }
464 }
465
465
466
466
467 .. _msging_inspection:
467 .. _msging_inspection:
468
468
469 Introspection
469 Introspection
470 -------------
470 -------------
471
471
472 Code can be inspected to show useful information to the user.
472 Code can be inspected to show useful information to the user.
473 It is up to the Kernel to decide what information should be displayed, and its formatting.
473 It is up to the Kernel to decide what information should be displayed, and its formatting.
474
474
475 Message type: ``inspect_request``::
475 Message type: ``inspect_request``::
476
476
477 content = {
477 content = {
478 # The code context in which introspection is requested
478 # The code context in which introspection is requested
479 # this may be up to an entire multiline cell.
479 # this may be up to an entire multiline cell.
480 'code' : str,
480 'code' : str,
481
481
482 # The cursor position within 'code' (in unicode characters) where inspection is requested
482 # The cursor position within 'code' (in unicode characters) where inspection is requested
483 'cursor_pos' : int,
483 'cursor_pos' : int,
484
484
485 # The level of detail desired. In IPython, the default (0) is equivalent to typing
485 # The level of detail desired. In IPython, the default (0) is equivalent to typing
486 # 'x?' at the prompt, 1 is equivalent to 'x??'.
486 # 'x?' at the prompt, 1 is equivalent to 'x??'.
487 # The difference is up to kernels, but in IPython level 1 includes the source code
487 # The difference is up to kernels, but in IPython level 1 includes the source code
488 # if available.
488 # if available.
489 'detail_level' : 0 or 1,
489 'detail_level' : 0 or 1,
490 }
490 }
491
491
492 .. versionchanged:: 5.0
492 .. versionchanged:: 5.0
493
493
494 ``object_info_request`` renamed to ``inspect_request``.
494 ``object_info_request`` renamed to ``inspect_request``.
495
495
496 .. versionchanged:: 5.0
496 .. versionchanged:: 5.0
497
497
498 ``name`` key replaced with ``code`` and ``cursor_pos``,
498 ``name`` key replaced with ``code`` and ``cursor_pos``,
499 moving the lexing responsibility to the kernel.
499 moving the lexing responsibility to the kernel.
500
500
501 The reply is a mime-bundle, like a `display_data`_ message,
501 The reply is a mime-bundle, like a `display_data`_ message,
502 which should be a formatted representation of information about the context.
502 which should be a formatted representation of information about the context.
503 In the notebook, this is used to show tooltips over function calls, etc.
503 In the notebook, this is used to show tooltips over function calls, etc.
504
504
505 Message type: ``inspect_reply``::
505 Message type: ``inspect_reply``::
506
506
507 content = {
507 content = {
508 # 'ok' if the request succeeded or 'error', with error information as in all other replies.
508 # 'ok' if the request succeeded or 'error', with error information as in all other replies.
509 'status' : 'ok',
509 'status' : 'ok',
510
510
511 # data can be empty if nothing is found
511 # data can be empty if nothing is found
512 'data' : dict,
512 'data' : dict,
513 'metadata' : dict,
513 'metadata' : dict,
514 }
514 }
515
515
516 .. versionchanged:: 5.0
516 .. versionchanged:: 5.0
517
517
518 ``object_info_reply`` renamed to ``inspect_reply``.
518 ``object_info_reply`` renamed to ``inspect_reply``.
519
519
520 .. versionchanged:: 5.0
520 .. versionchanged:: 5.0
521
521
522 Reply is changed from structured data to a mime bundle, allowing formatting decisions to be made by the kernel.
522 Reply is changed from structured data to a mime bundle, allowing formatting decisions to be made by the kernel.
523
523
524 .. _msging_completion:
524 .. _msging_completion:
525
525
526 Completion
526 Completion
527 ----------
527 ----------
528
528
529 Message type: ``complete_request``::
529 Message type: ``complete_request``::
530
530
531 content = {
531 content = {
532 # The code context in which completion is requested
532 # The code context in which completion is requested
533 # this may be up to an entire multiline cell, such as
533 # this may be up to an entire multiline cell, such as
534 # 'foo = a.isal'
534 # 'foo = a.isal'
535 'code' : str,
535 'code' : str,
536
536
537 # The cursor position within 'code' (in unicode characters) where completion is requested
537 # The cursor position within 'code' (in unicode characters) where completion is requested
538 'cursor_pos' : int,
538 'cursor_pos' : int,
539 }
539 }
540
540
541 .. versionchanged:: 5.0
541 .. versionchanged:: 5.0
542
542
543 ``line``, ``block``, and ``text`` keys are removed in favor of a single ``code`` for context.
543 ``line``, ``block``, and ``text`` keys are removed in favor of a single ``code`` for context.
544 Lexing is up to the kernel.
544 Lexing is up to the kernel.
545
545
546
546
547 Message type: ``complete_reply``::
547 Message type: ``complete_reply``::
548
548
549 content = {
549 content = {
550 # The list of all matches to the completion request, such as
550 # The list of all matches to the completion request, such as
551 # ['a.isalnum', 'a.isalpha'] for the above example.
551 # ['a.isalnum', 'a.isalpha'] for the above example.
552 'matches' : list,
552 'matches' : list,
553
553
554 # The range of text that should be replaced by the above matches when a completion is accepted.
554 # The range of text that should be replaced by the above matches when a completion is accepted.
555 # typically cursor_end is the same as cursor_pos in the request.
555 # typically cursor_end is the same as cursor_pos in the request.
556 'cursor_start' : int,
556 'cursor_start' : int,
557 'cursor_end' : int,
557 'cursor_end' : int,
558
558
559 # Information that frontend plugins might use for extra display information about completions.
559 # Information that frontend plugins might use for extra display information about completions.
560 'metadata' : dict,
560 'metadata' : dict,
561
561
562 # status should be 'ok' unless an exception was raised during the request,
562 # status should be 'ok' unless an exception was raised during the request,
563 # in which case it should be 'error', along with the usual error message content
563 # in which case it should be 'error', along with the usual error message content
564 # in other messages.
564 # in other messages.
565 'status' : 'ok'
565 'status' : 'ok'
566 }
566 }
567
567
568 .. versionchanged:: 5.0
568 .. versionchanged:: 5.0
569
569
570 - ``matched_text`` is removed in favor of ``cursor_start`` and ``cursor_end``.
570 - ``matched_text`` is removed in favor of ``cursor_start`` and ``cursor_end``.
571 - ``metadata`` is added for extended information.
571 - ``metadata`` is added for extended information.
572
572
573 .. _msging_history:
573 .. _msging_history:
574
574
575 History
575 History
576 -------
576 -------
577
577
578 For clients to explicitly request history from a kernel. The kernel has all
578 For clients to explicitly request history from a kernel. The kernel has all
579 the actual execution history stored in a single location, so clients can
579 the actual execution history stored in a single location, so clients can
580 request it from the kernel when needed.
580 request it from the kernel when needed.
581
581
582 Message type: ``history_request``::
582 Message type: ``history_request``::
583
583
584 content = {
584 content = {
585
585
586 # If True, also return output history in the resulting dict.
586 # If True, also return output history in the resulting dict.
587 'output' : bool,
587 'output' : bool,
588
588
589 # If True, return the raw input history, else the transformed input.
589 # If True, return the raw input history, else the transformed input.
590 'raw' : bool,
590 'raw' : bool,
591
591
592 # So far, this can be 'range', 'tail' or 'search'.
592 # So far, this can be 'range', 'tail' or 'search'.
593 'hist_access_type' : str,
593 'hist_access_type' : str,
594
594
595 # If hist_access_type is 'range', get a range of input cells. session can
595 # If hist_access_type is 'range', get a range of input cells. session can
596 # be a positive session number, or a negative number to count back from
596 # be a positive session number, or a negative number to count back from
597 # the current session.
597 # the current session.
598 'session' : int,
598 'session' : int,
599 # start and stop are line numbers within that session.
599 # start and stop are line numbers within that session.
600 'start' : int,
600 'start' : int,
601 'stop' : int,
601 'stop' : int,
602
602
603 # If hist_access_type is 'tail' or 'search', get the last n cells.
603 # If hist_access_type is 'tail' or 'search', get the last n cells.
604 'n' : int,
604 'n' : int,
605
605
606 # If hist_access_type is 'search', get cells matching the specified glob
606 # If hist_access_type is 'search', get cells matching the specified glob
607 # pattern (with * and ? as wildcards).
607 # pattern (with * and ? as wildcards).
608 'pattern' : str,
608 'pattern' : str,
609
609
610 # If hist_access_type is 'search' and unique is true, do not
610 # If hist_access_type is 'search' and unique is true, do not
611 # include duplicated history. Default is false.
611 # include duplicated history. Default is false.
612 'unique' : bool,
612 'unique' : bool,
613
613
614 }
614 }
615
615
616 .. versionadded:: 4.0
616 .. versionadded:: 4.0
617 The key ``unique`` for ``history_request``.
617 The key ``unique`` for ``history_request``.
618
618
619 Message type: ``history_reply``::
619 Message type: ``history_reply``::
620
620
621 content = {
621 content = {
622 # A list of 3 tuples, either:
622 # A list of 3 tuples, either:
623 # (session, line_number, input) or
623 # (session, line_number, input) or
624 # (session, line_number, (input, output)),
624 # (session, line_number, (input, output)),
625 # depending on whether output was False or True, respectively.
625 # depending on whether output was False or True, respectively.
626 'history' : list,
626 'history' : list,
627 }
627 }
628
628
629 .. _msging_is_complete:
629 .. _msging_is_complete:
630
630
631 Code completeness
631 Code completeness
632 -----------------
632 -----------------
633
633
634 .. versionadded:: 5.0
634 .. versionadded:: 5.0
635
635
636 When the user enters a line in a console style interface, the console must
636 When the user enters a line in a console style interface, the console must
637 decide whether to immediately execute the current code, or whether to show a
637 decide whether to immediately execute the current code, or whether to show a
638 continuation prompt for further input. For instance, in Python ``a = 5`` would
638 continuation prompt for further input. For instance, in Python ``a = 5`` would
639 be executed immediately, while ``for i in range(5):`` would expect further input.
639 be executed immediately, while ``for i in range(5):`` would expect further input.
640
640
641 There are four possible replies:
641 There are four possible replies:
642
642
643 - *complete* code is ready to be executed
643 - *complete* code is ready to be executed
644 - *incomplete* code should prompt for another line
644 - *incomplete* code should prompt for another line
645 - *invalid* code will typically be sent for execution, so that the user sees the
645 - *invalid* code will typically be sent for execution, so that the user sees the
646 error soonest.
646 error soonest.
647 - *unknown* - if the kernel is not able to determine this. The frontend should
647 - *unknown* - if the kernel is not able to determine this. The frontend should
648 also handle the kernel not replying promptly. It may default to sending the
648 also handle the kernel not replying promptly. It may default to sending the
649 code for execution, or it may implement simple fallback heuristics for whether
649 code for execution, or it may implement simple fallback heuristics for whether
650 to execute the code (e.g. execute after a blank line).
650 to execute the code (e.g. execute after a blank line).
651
651
652 Frontends may have ways to override this, forcing the code to be sent for
652 Frontends may have ways to override this, forcing the code to be sent for
653 execution or forcing a continuation prompt.
653 execution or forcing a continuation prompt.
654
654
655 Message type: ``is_complete_request``::
655 Message type: ``is_complete_request``::
656
656
657 content = {
657 content = {
658 # The code entered so far as a multiline string
658 # The code entered so far as a multiline string
659 'code' : str,
659 'code' : str,
660 }
660 }
661
661
662 Message type: ``is_complete_reply``::
662 Message type: ``is_complete_reply``::
663
663
664 content = {
664 content = {
665 # One of 'complete', 'incomplete', 'invalid', 'unknown'
665 # One of 'complete', 'incomplete', 'invalid', 'unknown'
666 'status' : str,
666 'status' : str,
667
667
668 # If status is 'incomplete', indent should contain the characters to use
668 # If status is 'incomplete', indent should contain the characters to use
669 # to indent the next line. This is only a hint: frontends may ignore it
669 # to indent the next line. This is only a hint: frontends may ignore it
670 # and use their own autoindentation rules. For other statuses, this
670 # and use their own autoindentation rules. For other statuses, this
671 # field does not exist.
671 # field does not exist.
672 'indent': str,
672 'indent': str,
673 }
673 }
674
674
675 Connect
675 Connect
676 -------
676 -------
677
677
678 When a client connects to the request/reply socket of the kernel, it can issue
678 When a client connects to the request/reply socket of the kernel, it can issue
679 a connect request to get basic information about the kernel, such as the ports
679 a connect request to get basic information about the kernel, such as the ports
680 the other ZeroMQ sockets are listening on. This allows clients to only have
680 the other ZeroMQ sockets are listening on. This allows clients to only have
681 to know about a single port (the shell channel) to connect to a kernel.
681 to know about a single port (the shell channel) to connect to a kernel.
682
682
683 Message type: ``connect_request``::
683 Message type: ``connect_request``::
684
684
685 content = {
685 content = {
686 }
686 }
687
687
688 Message type: ``connect_reply``::
688 Message type: ``connect_reply``::
689
689
690 content = {
690 content = {
691 'shell_port' : int, # The port the shell ROUTER socket is listening on.
691 'shell_port' : int, # The port the shell ROUTER socket is listening on.
692 'iopub_port' : int, # The port the PUB socket is listening on.
692 'iopub_port' : int, # The port the PUB socket is listening on.
693 'stdin_port' : int, # The port the stdin ROUTER socket is listening on.
693 'stdin_port' : int, # The port the stdin ROUTER socket is listening on.
694 'hb_port' : int, # The port the heartbeat socket is listening on.
694 'hb_port' : int, # The port the heartbeat socket is listening on.
695 }
695 }
696
696
697 .. _msging_kernel_info:
697 .. _msging_kernel_info:
698
698
699 Kernel info
699 Kernel info
700 -----------
700 -----------
701
701
702 If a client needs to know information about the kernel, it can
702 If a client needs to know information about the kernel, it can
703 make a request of the kernel's information.
703 make a request of the kernel's information.
704 This message can be used to fetch core information of the
704 This message can be used to fetch core information of the
705 kernel, including language (e.g., Python), language version number and
705 kernel, including language (e.g., Python), language version number and
706 IPython version number, and the IPython message spec version number.
706 IPython version number, and the IPython message spec version number.
707
707
708 Message type: ``kernel_info_request``::
708 Message type: ``kernel_info_request``::
709
709
710 content = {
710 content = {
711 }
711 }
712
712
713 Message type: ``kernel_info_reply``::
713 Message type: ``kernel_info_reply``::
714
714
715 content = {
715 content = {
716 # Version of messaging protocol.
716 # Version of messaging protocol.
717 # The first integer indicates major version. It is incremented when
717 # The first integer indicates major version. It is incremented when
718 # there is any backward incompatible change.
718 # there is any backward incompatible change.
719 # The second integer indicates minor version. It is incremented when
719 # The second integer indicates minor version. It is incremented when
720 # there is any backward compatible change.
720 # there is any backward compatible change.
721 'protocol_version': 'X.Y.Z',
721 'protocol_version': 'X.Y.Z',
722
722
723 # The kernel implementation name
723 # The kernel implementation name
724 # (e.g. 'ipython' for the IPython kernel)
724 # (e.g. 'ipython' for the IPython kernel)
725 'implementation': str,
725 'implementation': str,
726
726
727 # Implementation version number.
727 # Implementation version number.
728 # The version number of the kernel's implementation
728 # The version number of the kernel's implementation
729 # (e.g. IPython.__version__ for the IPython kernel)
729 # (e.g. IPython.__version__ for the IPython kernel)
730 'implementation_version': 'X.Y.Z',
730 'implementation_version': 'X.Y.Z',
731
731
732 # Information about the language of code for the kernel
732 # Information about the language of code for the kernel
733 'language_info': {
733 'language_info': {
734 # Name of the programming language in which kernel is implemented.
734 # Name of the programming language in which kernel is implemented.
735 # Kernel included in IPython returns 'python'.
735 # Kernel included in IPython returns 'python'.
736 'name': str,
736 'name': str,
737
737
738 # Language version number.
738 # Language version number.
739 # It is Python version number (e.g., '2.7.3') for the kernel
739 # It is Python version number (e.g., '2.7.3') for the kernel
740 # included in IPython.
740 # included in IPython.
741 'version': 'X.Y.Z',
741 'version': 'X.Y.Z',
742
742
743 # mimetype for script files in this language
743 # mimetype for script files in this language
744 'mimetype': str,
744 'mimetype': str,
745
745
746 # Extension without the dot, e.g. 'py'
746 # Extension without the dot, e.g. 'py'
747 'file_extension': str,
747 'file_extension': str,
748
748
749 # Pygments lexer, for highlighting
749 # Pygments lexer, for highlighting
750 # Only needed if it differs from the top level 'language' field.
750 # Only needed if it differs from the top level 'language' field.
751 'pygments_lexer': str,
751 'pygments_lexer': str,
752
752
753 # Codemirror mode, for for highlighting in the notebook.
753 # Codemirror mode, for for highlighting in the notebook.
754 # Only needed if it differs from the top level 'language' field.
754 # Only needed if it differs from the top level 'language' field.
755 'codemirror_mode': str or dict,
755 'codemirror_mode': str or dict,
756
756
757 # Nbconvert exporter, if notebooks written with this kernel should
757 # Nbconvert exporter, if notebooks written with this kernel should
758 # be exported with something other than the general 'script'
758 # be exported with something other than the general 'script'
759 # exporter.
759 # exporter.
760 'nbconvert_exporter': str,
760 'nbconvert_exporter': str,
761 },
761 },
762
762
763 # A banner of information about the kernel,
763 # A banner of information about the kernel,
764 # which may be desplayed in console environments.
764 # which may be desplayed in console environments.
765 'banner' : str,
765 'banner' : str,
766
766
767 # Optional: A list of dictionaries, each with keys 'text' and 'url'.
767 # Optional: A list of dictionaries, each with keys 'text' and 'url'.
768 # These will be displayed in the help menu in the notebook UI.
768 # These will be displayed in the help menu in the notebook UI.
769 'help_links': [
769 'help_links': [
770 {'text': str, 'url': str}
770 {'text': str, 'url': str}
771 ],
771 ],
772 }
772 }
773
773
774 Refer to the lists of available `Pygments lexers <http://pygments.org/docs/lexers/>`_
774 Refer to the lists of available `Pygments lexers <http://pygments.org/docs/lexers/>`_
775 and `codemirror modes <http://codemirror.net/mode/index.html>`_ for those fields.
775 and `codemirror modes <http://codemirror.net/mode/index.html>`_ for those fields.
776
776
777 .. versionchanged:: 5.0
777 .. versionchanged:: 5.0
778
778
779 Versions changed from lists of integers to strings.
779 Versions changed from lists of integers to strings.
780
780
781 .. versionchanged:: 5.0
781 .. versionchanged:: 5.0
782
782
783 ``ipython_version`` is removed.
783 ``ipython_version`` is removed.
784
784
785 .. versionchanged:: 5.0
785 .. versionchanged:: 5.0
786
786
787 ``language_info``, ``implementation``, ``implementation_version``, ``banner``
787 ``language_info``, ``implementation``, ``implementation_version``, ``banner``
788 and ``help_links`` keys are added.
788 and ``help_links`` keys are added.
789
789
790 .. versionchanged:: 5.0
790 .. versionchanged:: 5.0
791
791
792 ``language_version`` moved to ``language_info.version``
792 ``language_version`` moved to ``language_info.version``
793
793
794 .. versionchanged:: 5.0
794 .. versionchanged:: 5.0
795
795
796 ``language`` moved to ``language_info.name``
796 ``language`` moved to ``language_info.name``
797
797
798 .. _msging_shutdown:
798 .. _msging_shutdown:
799
799
800 Kernel shutdown
800 Kernel shutdown
801 ---------------
801 ---------------
802
802
803 The clients can request the kernel to shut itself down; this is used in
803 The clients can request the kernel to shut itself down; this is used in
804 multiple cases:
804 multiple cases:
805
805
806 - when the user chooses to close the client application via a menu or window
806 - when the user chooses to close the client application via a menu or window
807 control.
807 control.
808 - when the user types 'exit' or 'quit' (or their uppercase magic equivalents).
808 - when the user types 'exit' or 'quit' (or their uppercase magic equivalents).
809 - when the user chooses a GUI method (like the 'Ctrl-C' shortcut in the
809 - when the user chooses a GUI method (like the 'Ctrl-C' shortcut in the
810 IPythonQt client) to force a kernel restart to get a clean kernel without
810 IPythonQt client) to force a kernel restart to get a clean kernel without
811 losing client-side state like history or inlined figures.
811 losing client-side state like history or inlined figures.
812
812
813 The client sends a shutdown request to the kernel, and once it receives the
813 The client sends a shutdown request to the kernel, and once it receives the
814 reply message (which is otherwise empty), it can assume that the kernel has
814 reply message (which is otherwise empty), it can assume that the kernel has
815 completed shutdown safely.
815 completed shutdown safely.
816
816
817 Upon their own shutdown, client applications will typically execute a last
817 Upon their own shutdown, client applications will typically execute a last
818 minute sanity check and forcefully terminate any kernel that is still alive, to
818 minute sanity check and forcefully terminate any kernel that is still alive, to
819 avoid leaving stray processes in the user's machine.
819 avoid leaving stray processes in the user's machine.
820
820
821 Message type: ``shutdown_request``::
821 Message type: ``shutdown_request``::
822
822
823 content = {
823 content = {
824 'restart' : bool # whether the shutdown is final, or precedes a restart
824 'restart' : bool # whether the shutdown is final, or precedes a restart
825 }
825 }
826
826
827 Message type: ``shutdown_reply``::
827 Message type: ``shutdown_reply``::
828
828
829 content = {
829 content = {
830 'restart' : bool # whether the shutdown is final, or precedes a restart
830 'restart' : bool # whether the shutdown is final, or precedes a restart
831 }
831 }
832
832
833 .. Note::
833 .. Note::
834
834
835 When the clients detect a dead kernel thanks to inactivity on the heartbeat
835 When the clients detect a dead kernel thanks to inactivity on the heartbeat
836 socket, they simply send a forceful process termination signal, since a dead
836 socket, they simply send a forceful process termination signal, since a dead
837 process is unlikely to respond in any useful way to messages.
837 process is unlikely to respond in any useful way to messages.
838
838
839
839
840 Messages on the PUB/SUB socket
840 Messages on the PUB/SUB socket
841 ==============================
841 ==============================
842
842
843 Streams (stdout, stderr, etc)
843 Streams (stdout, stderr, etc)
844 ------------------------------
844 ------------------------------
845
845
846 Message type: ``stream``::
846 Message type: ``stream``::
847
847
848 content = {
848 content = {
849 # The name of the stream is one of 'stdout', 'stderr'
849 # The name of the stream is one of 'stdout', 'stderr'
850 'name' : str,
850 'name' : str,
851
851
852 # The text is an arbitrary string to be written to that stream
852 # The text is an arbitrary string to be written to that stream
853 'text' : str,
853 'text' : str,
854 }
854 }
855
855
856 .. versionchanged:: 5.0
856 .. versionchanged:: 5.0
857
857
858 'data' key renamed to 'text' for conistency with the notebook format.
858 'data' key renamed to 'text' for conistency with the notebook format.
859
859
860 Display Data
860 Display Data
861 ------------
861 ------------
862
862
863 This type of message is used to bring back data that should be displayed (text,
863 This type of message is used to bring back data that should be displayed (text,
864 html, svg, etc.) in the frontends. This data is published to all frontends.
864 html, svg, etc.) in the frontends. This data is published to all frontends.
865 Each message can have multiple representations of the data; it is up to the
865 Each message can have multiple representations of the data; it is up to the
866 frontend to decide which to use and how. A single message should contain all
866 frontend to decide which to use and how. A single message should contain all
867 possible representations of the same information. Each representation should
867 possible representations of the same information. Each representation should
868 be a JSON'able data structure, and should be a valid MIME type.
868 be a JSON'able data structure, and should be a valid MIME type.
869
869
870 Some questions remain about this design:
870 Some questions remain about this design:
871
871
872 * Do we use this message type for execute_result/displayhook? Probably not, because
872 * Do we use this message type for execute_result/displayhook? Probably not, because
873 the displayhook also has to handle the Out prompt display. On the other hand
873 the displayhook also has to handle the Out prompt display. On the other hand
874 we could put that information into the metadata section.
874 we could put that information into the metadata section.
875
875
876 .. _display_data:
876 .. _display_data:
877
877
878 Message type: ``display_data``::
878 Message type: ``display_data``::
879
879
880 content = {
880 content = {
881
881
882 # Who create the data
882 # Who create the data
883 'source' : str,
883 'source' : str,
884
884
885 # The data dict contains key/value pairs, where the keys are MIME
885 # The data dict contains key/value pairs, where the keys are MIME
886 # types and the values are the raw data of the representation in that
886 # types and the values are the raw data of the representation in that
887 # format.
887 # format.
888 'data' : dict,
888 'data' : dict,
889
889
890 # Any metadata that describes the data
890 # Any metadata that describes the data
891 'metadata' : dict
891 'metadata' : dict
892 }
892 }
893
893
894
894
895 The ``metadata`` contains any metadata that describes the output.
895 The ``metadata`` contains any metadata that describes the output.
896 Global keys are assumed to apply to the output as a whole.
896 Global keys are assumed to apply to the output as a whole.
897 The ``metadata`` dict can also contain mime-type keys, which will be sub-dictionaries,
897 The ``metadata`` dict can also contain mime-type keys, which will be sub-dictionaries,
898 which are interpreted as applying only to output of that type.
898 which are interpreted as applying only to output of that type.
899 Third parties should put any data they write into a single dict
899 Third parties should put any data they write into a single dict
900 with a reasonably unique name to avoid conflicts.
900 with a reasonably unique name to avoid conflicts.
901
901
902 The only metadata keys currently defined in IPython are the width and height
902 The only metadata keys currently defined in IPython are the width and height
903 of images::
903 of images::
904
904
905 metadata = {
905 metadata = {
906 'image/png' : {
906 'image/png' : {
907 'width': 640,
907 'width': 640,
908 'height': 480
908 'height': 480
909 }
909 }
910 }
910 }
911
911
912
912
913 .. versionchanged:: 5.0
913 .. versionchanged:: 5.0
914
914
915 `application/json` data should be unpacked JSON data,
915 `application/json` data should be unpacked JSON data,
916 not double-serialized as a JSON string.
916 not double-serialized as a JSON string.
917
917
918
918
919 Raw Data Publication
919 Raw Data Publication
920 --------------------
920 --------------------
921
921
922 ``display_data`` lets you publish *representations* of data, such as images and html.
922 ``display_data`` lets you publish *representations* of data, such as images and html.
923 This ``data_pub`` message lets you publish *actual raw data*, sent via message buffers.
923 This ``data_pub`` message lets you publish *actual raw data*, sent via message buffers.
924
924
925 data_pub messages are constructed via the :func:`IPython.lib.datapub.publish_data` function:
925 data_pub messages are constructed via the :func:`IPython.lib.datapub.publish_data` function:
926
926
927 .. sourcecode:: python
927 .. sourcecode:: python
928
928
929 from IPython.kernel.zmq.datapub import publish_data
929 from IPython.kernel.zmq.datapub import publish_data
930 ns = dict(x=my_array)
930 ns = dict(x=my_array)
931 publish_data(ns)
931 publish_data(ns)
932
932
933
933
934 Message type: ``data_pub``::
934 Message type: ``data_pub``::
935
935
936 content = {
936 content = {
937 # the keys of the data dict, after it has been unserialized
937 # the keys of the data dict, after it has been unserialized
938 'keys' : ['a', 'b']
938 'keys' : ['a', 'b']
939 }
939 }
940 # the namespace dict will be serialized in the message buffers,
940 # the namespace dict will be serialized in the message buffers,
941 # which will have a length of at least one
941 # which will have a length of at least one
942 buffers = [b'pdict', ...]
942 buffers = [b'pdict', ...]
943
943
944
944
945 The interpretation of a sequence of data_pub messages for a given parent request should be
945 The interpretation of a sequence of data_pub messages for a given parent request should be
946 to update a single namespace with subsequent results.
946 to update a single namespace with subsequent results.
947
947
948 .. note::
948 .. note::
949
949
950 No frontends directly handle data_pub messages at this time.
950 No frontends directly handle data_pub messages at this time.
951 It is currently only used by the client/engines in :mod:`IPython.parallel`,
951 It is currently only used by the client/engines in :mod:`IPython.parallel`,
952 where engines may publish *data* to the Client,
952 where engines may publish *data* to the Client,
953 of which the Client can then publish *representations* via ``display_data``
953 of which the Client can then publish *representations* via ``display_data``
954 to various frontends.
954 to various frontends.
955
955
956 Code inputs
956 Code inputs
957 -----------
957 -----------
958
958
959 To let all frontends know what code is being executed at any given time, these
959 To let all frontends know what code is being executed at any given time, these
960 messages contain a re-broadcast of the ``code`` portion of an
960 messages contain a re-broadcast of the ``code`` portion of an
961 :ref:`execute_request <execute>`, along with the :ref:`execution_count
961 :ref:`execute_request <execute>`, along with the :ref:`execution_count
962 <execution_counter>`.
962 <execution_counter>`.
963
963
964 Message type: ``execute_input``::
964 Message type: ``execute_input``::
965
965
966 content = {
966 content = {
967 'code' : str, # Source code to be executed, one or more lines
967 'code' : str, # Source code to be executed, one or more lines
968
968
969 # The counter for this execution is also provided so that clients can
969 # The counter for this execution is also provided so that clients can
970 # display it, since IPython automatically creates variables called _iN
970 # display it, since IPython automatically creates variables called _iN
971 # (for input prompt In[N]).
971 # (for input prompt In[N]).
972 'execution_count' : int
972 'execution_count' : int
973 }
973 }
974
974
975 .. versionchanged:: 5.0
975 .. versionchanged:: 5.0
976
976
977 ``pyin`` is renamed to ``execute_input``.
977 ``pyin`` is renamed to ``execute_input``.
978
978
979
979
980 Execution results
980 Execution results
981 -----------------
981 -----------------
982
982
983 Results of an execution are published as an ``execute_result``.
983 Results of an execution are published as an ``execute_result``.
984 These are identical to `display_data`_ messages, with the addition of an ``execution_count`` key.
984 These are identical to `display_data`_ messages, with the addition of an ``execution_count`` key.
985
985
986 Results can have multiple simultaneous formats depending on its
986 Results can have multiple simultaneous formats depending on its
987 configuration. A plain text representation should always be provided
987 configuration. A plain text representation should always be provided
988 in the ``text/plain`` mime-type. Frontends are free to display any or all of these
988 in the ``text/plain`` mime-type. Frontends are free to display any or all of these
989 according to its capabilities.
989 according to its capabilities.
990 Frontends should ignore mime-types they do not understand. The data itself is
990 Frontends should ignore mime-types they do not understand. The data itself is
991 any JSON object and depends on the format. It is often, but not always a string.
991 any JSON object and depends on the format. It is often, but not always a string.
992
992
993 Message type: ``execute_result``::
993 Message type: ``execute_result``::
994
994
995 content = {
995 content = {
996
996
997 # The counter for this execution is also provided so that clients can
997 # The counter for this execution is also provided so that clients can
998 # display it, since IPython automatically creates variables called _N
998 # display it, since IPython automatically creates variables called _N
999 # (for prompt N).
999 # (for prompt N).
1000 'execution_count' : int,
1000 'execution_count' : int,
1001
1001
1002 # data and metadata are identical to a display_data message.
1002 # data and metadata are identical to a display_data message.
1003 # the object being displayed is that passed to the display hook,
1003 # the object being displayed is that passed to the display hook,
1004 # i.e. the *result* of the execution.
1004 # i.e. the *result* of the execution.
1005 'data' : dict,
1005 'data' : dict,
1006 'metadata' : dict,
1006 'metadata' : dict,
1007 }
1007 }
1008
1008
1009 Execution errors
1009 Execution errors
1010 ----------------
1010 ----------------
1011
1011
1012 When an error occurs during code execution
1012 When an error occurs during code execution
1013
1013
1014 Message type: ``error``::
1014 Message type: ``error``::
1015
1015
1016 content = {
1016 content = {
1017 # Similar content to the execute_reply messages for the 'error' case,
1017 # Similar content to the execute_reply messages for the 'error' case,
1018 # except the 'status' field is omitted.
1018 # except the 'status' field is omitted.
1019 }
1019 }
1020
1020
1021 .. versionchanged:: 5.0
1021 .. versionchanged:: 5.0
1022
1022
1023 ``pyerr`` renamed to ``error``
1023 ``pyerr`` renamed to ``error``
1024
1024
1025 Kernel status
1025 Kernel status
1026 -------------
1026 -------------
1027
1027
1028 This message type is used by frontends to monitor the status of the kernel.
1028 This message type is used by frontends to monitor the status of the kernel.
1029
1029
1030 Message type: ``status``::
1030 Message type: ``status``::
1031
1031
1032 content = {
1032 content = {
1033 # When the kernel starts to handle a message, it will enter the 'busy'
1033 # When the kernel starts to handle a message, it will enter the 'busy'
1034 # state and when it finishes, it will enter the 'idle' state.
1034 # state and when it finishes, it will enter the 'idle' state.
1035 # The kernel will publish state 'starting' exactly once at process startup.
1035 # The kernel will publish state 'starting' exactly once at process startup.
1036 execution_state : ('busy', 'idle', 'starting')
1036 execution_state : ('busy', 'idle', 'starting')
1037 }
1037 }
1038
1038
1039 .. versionchanged:: 5.0
1039 .. versionchanged:: 5.0
1040
1040
1041 Busy and idle messages should be sent before/after handling every message,
1041 Busy and idle messages should be sent before/after handling every message,
1042 not just execution.
1042 not just execution.
1043
1043
1044 .. note::
1045
1046 Extra status messages are added between the notebook webserver and websocket clients
1047 that are not sent by the kernel. These are:
1048
1049 - restarting (kernel has died, but will be automatically restarted)
1050 - dead (kernel has died, restarting has failed)
1051
1044 Clear output
1052 Clear output
1045 ------------
1053 ------------
1046
1054
1047 This message type is used to clear the output that is visible on the frontend.
1055 This message type is used to clear the output that is visible on the frontend.
1048
1056
1049 Message type: ``clear_output``::
1057 Message type: ``clear_output``::
1050
1058
1051 content = {
1059 content = {
1052
1060
1053 # Wait to clear the output until new output is available. Clears the
1061 # Wait to clear the output until new output is available. Clears the
1054 # existing output immediately before the new output is displayed.
1062 # existing output immediately before the new output is displayed.
1055 # Useful for creating simple animations with minimal flickering.
1063 # Useful for creating simple animations with minimal flickering.
1056 'wait' : bool,
1064 'wait' : bool,
1057 }
1065 }
1058
1066
1059 .. versionchanged:: 4.1
1067 .. versionchanged:: 4.1
1060
1068
1061 ``stdout``, ``stderr``, and ``display`` boolean keys for selective clearing are removed,
1069 ``stdout``, ``stderr``, and ``display`` boolean keys for selective clearing are removed,
1062 and ``wait`` is added.
1070 and ``wait`` is added.
1063 The selective clearing keys are ignored in v4 and the default behavior remains the same,
1071 The selective clearing keys are ignored in v4 and the default behavior remains the same,
1064 so v4 clear_output messages will be safely handled by a v4.1 frontend.
1072 so v4 clear_output messages will be safely handled by a v4.1 frontend.
1065
1073
1066
1074
1067 Messages on the stdin ROUTER/DEALER sockets
1075 Messages on the stdin ROUTER/DEALER sockets
1068 ===========================================
1076 ===========================================
1069
1077
1070 This is a socket where the request/reply pattern goes in the opposite direction:
1078 This is a socket where the request/reply pattern goes in the opposite direction:
1071 from the kernel to a *single* frontend, and its purpose is to allow
1079 from the kernel to a *single* frontend, and its purpose is to allow
1072 ``raw_input`` and similar operations that read from ``sys.stdin`` on the kernel
1080 ``raw_input`` and similar operations that read from ``sys.stdin`` on the kernel
1073 to be fulfilled by the client. The request should be made to the frontend that
1081 to be fulfilled by the client. The request should be made to the frontend that
1074 made the execution request that prompted ``raw_input`` to be called. For now we
1082 made the execution request that prompted ``raw_input`` to be called. For now we
1075 will keep these messages as simple as possible, since they only mean to convey
1083 will keep these messages as simple as possible, since they only mean to convey
1076 the ``raw_input(prompt)`` call.
1084 the ``raw_input(prompt)`` call.
1077
1085
1078 Message type: ``input_request``::
1086 Message type: ``input_request``::
1079
1087
1080 content = {
1088 content = {
1081 # the text to show at the prompt
1089 # the text to show at the prompt
1082 'prompt' : str,
1090 'prompt' : str,
1083 # Is the request for a password?
1091 # Is the request for a password?
1084 # If so, the frontend shouldn't echo input.
1092 # If so, the frontend shouldn't echo input.
1085 'password' : bool
1093 'password' : bool
1086 }
1094 }
1087
1095
1088 Message type: ``input_reply``::
1096 Message type: ``input_reply``::
1089
1097
1090 content = { 'value' : str }
1098 content = { 'value' : str }
1091
1099
1092
1100
1093 When ``password`` is True, the frontend should not echo the input as it is entered.
1101 When ``password`` is True, the frontend should not echo the input as it is entered.
1094
1102
1095 .. versionchanged:: 5.0
1103 .. versionchanged:: 5.0
1096
1104
1097 ``password`` key added.
1105 ``password`` key added.
1098
1106
1099 .. note::
1107 .. note::
1100
1108
1101 The stdin socket of the client is required to have the same zmq IDENTITY
1109 The stdin socket of the client is required to have the same zmq IDENTITY
1102 as the client's shell socket.
1110 as the client's shell socket.
1103 Because of this, the ``input_request`` must be sent with the same IDENTITY
1111 Because of this, the ``input_request`` must be sent with the same IDENTITY
1104 routing prefix as the ``execute_reply`` in order for the frontend to receive
1112 routing prefix as the ``execute_reply`` in order for the frontend to receive
1105 the message.
1113 the message.
1106
1114
1107 .. note::
1115 .. note::
1108
1116
1109 We do not explicitly try to forward the raw ``sys.stdin`` object, because in
1117 We do not explicitly try to forward the raw ``sys.stdin`` object, because in
1110 practice the kernel should behave like an interactive program. When a
1118 practice the kernel should behave like an interactive program. When a
1111 program is opened on the console, the keyboard effectively takes over the
1119 program is opened on the console, the keyboard effectively takes over the
1112 ``stdin`` file descriptor, and it can't be used for raw reading anymore.
1120 ``stdin`` file descriptor, and it can't be used for raw reading anymore.
1113 Since the IPython kernel effectively behaves like a console program (albeit
1121 Since the IPython kernel effectively behaves like a console program (albeit
1114 one whose "keyboard" is actually living in a separate process and
1122 one whose "keyboard" is actually living in a separate process and
1115 transported over the zmq connection), raw ``stdin`` isn't expected to be
1123 transported over the zmq connection), raw ``stdin`` isn't expected to be
1116 available.
1124 available.
1117
1125
1118 .. _kernel_heartbeat:
1126 .. _kernel_heartbeat:
1119
1127
1120 Heartbeat for kernels
1128 Heartbeat for kernels
1121 =====================
1129 =====================
1122
1130
1123 Clients send ping messages on a REQ socket, which are echoed right back
1131 Clients send ping messages on a REQ socket, which are echoed right back
1124 from the Kernel's REP socket. These are simple bytestrings, not full JSON messages described above.
1132 from the Kernel's REP socket. These are simple bytestrings, not full JSON messages described above.
1125
1133
1126
1134
1127 Custom Messages
1135 Custom Messages
1128 ===============
1136 ===============
1129
1137
1130 .. versionadded:: 4.1
1138 .. versionadded:: 4.1
1131
1139
1132 IPython 2.0 (msgspec v4.1) adds a messaging system for developers to add their own objects with Frontend
1140 IPython 2.0 (msgspec v4.1) adds a messaging system for developers to add their own objects with Frontend
1133 and Kernel-side components, and allow them to communicate with each other.
1141 and Kernel-side components, and allow them to communicate with each other.
1134 To do this, IPython adds a notion of a ``Comm``, which exists on both sides,
1142 To do this, IPython adds a notion of a ``Comm``, which exists on both sides,
1135 and can communicate in either direction.
1143 and can communicate in either direction.
1136
1144
1137 These messages are fully symmetrical - both the Kernel and the Frontend can send each message,
1145 These messages are fully symmetrical - both the Kernel and the Frontend can send each message,
1138 and no messages expect a reply.
1146 and no messages expect a reply.
1139 The Kernel listens for these messages on the Shell channel,
1147 The Kernel listens for these messages on the Shell channel,
1140 and the Frontend listens for them on the IOPub channel.
1148 and the Frontend listens for them on the IOPub channel.
1141
1149
1142 Opening a Comm
1150 Opening a Comm
1143 --------------
1151 --------------
1144
1152
1145 Opening a Comm produces a ``comm_open`` message, to be sent to the other side::
1153 Opening a Comm produces a ``comm_open`` message, to be sent to the other side::
1146
1154
1147 {
1155 {
1148 'comm_id' : 'u-u-i-d',
1156 'comm_id' : 'u-u-i-d',
1149 'target_name' : 'my_comm',
1157 'target_name' : 'my_comm',
1150 'data' : {}
1158 'data' : {}
1151 }
1159 }
1152
1160
1153 Every Comm has an ID and a target name.
1161 Every Comm has an ID and a target name.
1154 The code handling the message on the receiving side is responsible for maintaining a mapping
1162 The code handling the message on the receiving side is responsible for maintaining a mapping
1155 of target_name keys to constructors.
1163 of target_name keys to constructors.
1156 After a ``comm_open`` message has been sent,
1164 After a ``comm_open`` message has been sent,
1157 there should be a corresponding Comm instance on both sides.
1165 there should be a corresponding Comm instance on both sides.
1158 The ``data`` key is always a dict and can be any extra JSON information used in initialization of the comm.
1166 The ``data`` key is always a dict and can be any extra JSON information used in initialization of the comm.
1159
1167
1160 If the ``target_name`` key is not found on the receiving side,
1168 If the ``target_name`` key is not found on the receiving side,
1161 then it should immediately reply with a ``comm_close`` message to avoid an inconsistent state.
1169 then it should immediately reply with a ``comm_close`` message to avoid an inconsistent state.
1162
1170
1163 Comm Messages
1171 Comm Messages
1164 -------------
1172 -------------
1165
1173
1166 Comm messages are one-way communications to update comm state,
1174 Comm messages are one-way communications to update comm state,
1167 used for synchronizing widget state, or simply requesting actions of a comm's counterpart.
1175 used for synchronizing widget state, or simply requesting actions of a comm's counterpart.
1168
1176
1169 Essentially, each comm pair defines their own message specification implemented inside the ``data`` dict.
1177 Essentially, each comm pair defines their own message specification implemented inside the ``data`` dict.
1170
1178
1171 There are no expected replies (of course, one side can send another ``comm_msg`` in reply).
1179 There are no expected replies (of course, one side can send another ``comm_msg`` in reply).
1172
1180
1173 Message type: ``comm_msg``::
1181 Message type: ``comm_msg``::
1174
1182
1175 {
1183 {
1176 'comm_id' : 'u-u-i-d',
1184 'comm_id' : 'u-u-i-d',
1177 'data' : {}
1185 'data' : {}
1178 }
1186 }
1179
1187
1180 Tearing Down Comms
1188 Tearing Down Comms
1181 ------------------
1189 ------------------
1182
1190
1183 Since comms live on both sides, when a comm is destroyed the other side must be notified.
1191 Since comms live on both sides, when a comm is destroyed the other side must be notified.
1184 This is done with a ``comm_close`` message.
1192 This is done with a ``comm_close`` message.
1185
1193
1186 Message type: ``comm_close``::
1194 Message type: ``comm_close``::
1187
1195
1188 {
1196 {
1189 'comm_id' : 'u-u-i-d',
1197 'comm_id' : 'u-u-i-d',
1190 'data' : {}
1198 'data' : {}
1191 }
1199 }
1192
1200
1193 Output Side Effects
1201 Output Side Effects
1194 -------------------
1202 -------------------
1195
1203
1196 Since comm messages can execute arbitrary user code,
1204 Since comm messages can execute arbitrary user code,
1197 handlers should set the parent header and publish status busy / idle,
1205 handlers should set the parent header and publish status busy / idle,
1198 just like an execute request.
1206 just like an execute request.
1199
1207
1200
1208
1201 To Do
1209 To Do
1202 =====
1210 =====
1203
1211
1204 Missing things include:
1212 Missing things include:
1205
1213
1206 * Important: finish thinking through the payload concept and API.
1214 * Important: finish thinking through the payload concept and API.
1207
1215
1208 .. include:: ../links.txt
1216 .. include:: ../links.txt
General Comments 0
You need to be logged in to leave comments. Login now