Show More
@@ -1,484 +1,484 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2008-2011 The IPython Development Team |
|
2 | // Copyright (C) 2008-2011 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // Kernel |
|
9 | // Kernel | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | /** |
|
12 | /** | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | * @submodule Kernel |
|
15 | * @submodule Kernel | |
16 | */ |
|
16 | */ | |
17 |
|
17 | |||
18 | var IPython = (function (IPython) { |
|
18 | var IPython = (function (IPython) { | |
19 |
|
19 | |||
20 | var utils = IPython.utils; |
|
20 | var utils = IPython.utils; | |
21 |
|
21 | |||
22 | // Initialization and connection. |
|
22 | // Initialization and connection. | |
23 | /** |
|
23 | /** | |
24 | * A Kernel Class to communicate with the Python kernel |
|
24 | * A Kernel Class to communicate with the Python kernel | |
25 | * @Class Kernel |
|
25 | * @Class Kernel | |
26 | */ |
|
26 | */ | |
27 | var Kernel = function (base_url) { |
|
27 | var Kernel = function (base_url) { | |
28 | this.kernel_id = null; |
|
28 | this.kernel_id = null; | |
29 | this.shell_channel = null; |
|
29 | this.shell_channel = null; | |
30 | this.iopub_channel = null; |
|
30 | this.iopub_channel = null; | |
31 | this.stdin_channel = null; |
|
31 | this.stdin_channel = null; | |
32 | this.base_url = base_url; |
|
32 | this.base_url = base_url; | |
33 | this.running = false; |
|
33 | this.running = false; | |
34 | this.username = "username"; |
|
34 | this.username = "username"; | |
35 | this.session_id = utils.uuid(); |
|
35 | this.session_id = utils.uuid(); | |
36 | this._msg_callbacks = {}; |
|
36 | this._msg_callbacks = {}; | |
37 |
|
37 | |||
38 | if (typeof(WebSocket) !== 'undefined') { |
|
38 | if (typeof(WebSocket) !== 'undefined') { | |
39 | this.WebSocket = WebSocket; |
|
39 | this.WebSocket = WebSocket; | |
40 | } else if (typeof(MozWebSocket) !== 'undefined') { |
|
40 | } else if (typeof(MozWebSocket) !== 'undefined') { | |
41 | this.WebSocket = MozWebSocket; |
|
41 | this.WebSocket = MozWebSocket; | |
42 | } else { |
|
42 | } else { | |
43 | alert('Your browser does not have WebSocket support, please try Chrome, Safari or Firefox β₯ 6. Firefox 4 and 5 are also supported by you have to enable WebSockets in about:config.'); |
|
43 | alert('Your browser does not have WebSocket support, please try Chrome, Safari or Firefox β₯ 6. Firefox 4 and 5 are also supported by you have to enable WebSockets in about:config.'); | |
44 | }; |
|
44 | }; | |
45 | }; |
|
45 | }; | |
46 |
|
46 | |||
47 |
|
47 | |||
48 | Kernel.prototype._get_msg = function (msg_type, content) { |
|
48 | Kernel.prototype._get_msg = function (msg_type, content) { | |
49 | var msg = { |
|
49 | var msg = { | |
50 | header : { |
|
50 | header : { | |
51 | msg_id : utils.uuid(), |
|
51 | msg_id : utils.uuid(), | |
52 | username : this.username, |
|
52 | username : this.username, | |
53 | session : this.session_id, |
|
53 | session : this.session_id, | |
54 | msg_type : msg_type |
|
54 | msg_type : msg_type | |
55 | }, |
|
55 | }, | |
56 | metadata : {}, |
|
56 | metadata : {}, | |
57 | content : content, |
|
57 | content : content, | |
58 | parent_header : {} |
|
58 | parent_header : {} | |
59 | }; |
|
59 | }; | |
60 | return msg; |
|
60 | return msg; | |
61 | }; |
|
61 | }; | |
62 |
|
62 | |||
63 | /** |
|
63 | /** | |
64 | * Start the Python kernel |
|
64 | * Start the Python kernel | |
65 | * @method start |
|
65 | * @method start | |
66 | */ |
|
66 | */ | |
67 | Kernel.prototype.start = function (notebook_id) { |
|
67 | Kernel.prototype.start = function (notebook_id) { | |
68 | var that = this; |
|
68 | var that = this; | |
69 | if (!this.running) { |
|
69 | if (!this.running) { | |
70 | var qs = $.param({notebook:notebook_id}); |
|
70 | var qs = $.param({notebook:notebook_id}); | |
71 | var url = this.base_url + '?' + qs; |
|
71 | var url = this.base_url + '?' + qs; | |
72 | $.post(url, |
|
72 | $.post(url, | |
73 | $.proxy(that._kernel_started,that), |
|
73 | $.proxy(that._kernel_started,that), | |
74 | 'json' |
|
74 | 'json' | |
75 | ); |
|
75 | ); | |
76 | }; |
|
76 | }; | |
77 | }; |
|
77 | }; | |
78 |
|
78 | |||
79 | /** |
|
79 | /** | |
80 | * Restart the python kernel. |
|
80 | * Restart the python kernel. | |
81 | * |
|
81 | * | |
82 | * Emit a 'status_restarting.Kernel' event with |
|
82 | * Emit a 'status_restarting.Kernel' event with | |
83 | * the current object as parameter |
|
83 | * the current object as parameter | |
84 | * |
|
84 | * | |
85 | * @method restart |
|
85 | * @method restart | |
86 | */ |
|
86 | */ | |
87 | Kernel.prototype.restart = function () { |
|
87 | Kernel.prototype.restart = function () { | |
88 | $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this}); |
|
88 | $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this}); | |
89 | var that = this; |
|
89 | var that = this; | |
90 | if (this.running) { |
|
90 | if (this.running) { | |
91 | this.stop_channels(); |
|
91 | this.stop_channels(); | |
92 | var url = this.kernel_url + "/restart"; |
|
92 | var url = this.kernel_url + "/restart"; | |
93 | $.post(url, |
|
93 | $.post(url, | |
94 | $.proxy(that._kernel_started, that), |
|
94 | $.proxy(that._kernel_started, that), | |
95 | 'json' |
|
95 | 'json' | |
96 | ); |
|
96 | ); | |
97 | }; |
|
97 | }; | |
98 | }; |
|
98 | }; | |
99 |
|
99 | |||
100 |
|
100 | |||
101 | Kernel.prototype._kernel_started = function (json) { |
|
101 | Kernel.prototype._kernel_started = function (json) { | |
102 | console.log("Kernel started: ", json.kernel_id); |
|
102 | console.log("Kernel started: ", json.kernel_id); | |
103 | this.running = true; |
|
103 | this.running = true; | |
104 | this.kernel_id = json.kernel_id; |
|
104 | this.kernel_id = json.kernel_id; | |
105 | this.ws_url = json.ws_url; |
|
105 | this.ws_url = json.ws_url; | |
106 | this.kernel_url = this.base_url + "/" + this.kernel_id; |
|
106 | this.kernel_url = this.base_url + "/" + this.kernel_id; | |
107 | this.start_channels(); |
|
107 | this.start_channels(); | |
108 | $([IPython.events]).trigger('status_started.Kernel', {kernel: this}); |
|
108 | $([IPython.events]).trigger('status_started.Kernel', {kernel: this}); | |
109 | }; |
|
109 | }; | |
110 |
|
110 | |||
111 |
|
111 | |||
112 | Kernel.prototype._websocket_closed = function(ws_url, early) { |
|
112 | Kernel.prototype._websocket_closed = function(ws_url, early) { | |
113 | this.stop_channels(); |
|
113 | this.stop_channels(); | |
114 | $([IPython.events]).trigger('websocket_closed.Kernel', |
|
114 | $([IPython.events]).trigger('websocket_closed.Kernel', | |
115 | {ws_url: ws_url, kernel: this, early: early} |
|
115 | {ws_url: ws_url, kernel: this, early: early} | |
116 | ); |
|
116 | ); | |
117 | }; |
|
117 | }; | |
118 |
|
118 | |||
119 | /** |
|
119 | /** | |
120 | * Start the `shell`and `iopub` channels. |
|
120 | * Start the `shell`and `iopub` channels. | |
121 | * Will stop and restart them if they already exist. |
|
121 | * Will stop and restart them if they already exist. | |
122 | * |
|
122 | * | |
123 | * @method start_channels |
|
123 | * @method start_channels | |
124 | */ |
|
124 | */ | |
125 | Kernel.prototype.start_channels = function () { |
|
125 | Kernel.prototype.start_channels = function () { | |
126 | var that = this; |
|
126 | var that = this; | |
127 | this.stop_channels(); |
|
127 | this.stop_channels(); | |
128 | var ws_url = this.ws_url + this.kernel_url; |
|
128 | var ws_url = this.ws_url + this.kernel_url; | |
129 | console.log("Starting WebSockets:", ws_url); |
|
129 | console.log("Starting WebSockets:", ws_url); | |
130 | this.shell_channel = new this.WebSocket(ws_url + "/shell"); |
|
130 | this.shell_channel = new this.WebSocket(ws_url + "/shell"); | |
131 | this.stdin_channel = new this.WebSocket(ws_url + "/stdin"); |
|
131 | this.stdin_channel = new this.WebSocket(ws_url + "/stdin"); | |
132 | this.iopub_channel = new this.WebSocket(ws_url + "/iopub"); |
|
132 | this.iopub_channel = new this.WebSocket(ws_url + "/iopub"); | |
133 | send_cookie = function(){ |
|
133 | send_cookie = function(){ | |
134 | // send the session id so the Session object Python-side |
|
134 | // send the session id so the Session object Python-side | |
135 | // has the same identity |
|
135 | // has the same identity | |
136 | this.send(that.session_id + ':' + document.cookie); |
|
136 | this.send(that.session_id + ':' + document.cookie); | |
137 | }; |
|
137 | }; | |
138 | var already_called_onclose = false; // only alert once |
|
138 | var already_called_onclose = false; // only alert once | |
139 | var ws_closed_early = function(evt){ |
|
139 | var ws_closed_early = function(evt){ | |
140 | if (already_called_onclose){ |
|
140 | if (already_called_onclose){ | |
141 | return; |
|
141 | return; | |
142 | } |
|
142 | } | |
143 | already_called_onclose = true; |
|
143 | already_called_onclose = true; | |
144 | if ( ! evt.wasClean ){ |
|
144 | if ( ! evt.wasClean ){ | |
145 | that._websocket_closed(ws_url, true); |
|
145 | that._websocket_closed(ws_url, true); | |
146 | } |
|
146 | } | |
147 | }; |
|
147 | }; | |
148 | var ws_closed_late = function(evt){ |
|
148 | var ws_closed_late = function(evt){ | |
149 | if (already_called_onclose){ |
|
149 | if (already_called_onclose){ | |
150 | return; |
|
150 | return; | |
151 | } |
|
151 | } | |
152 | already_called_onclose = true; |
|
152 | already_called_onclose = true; | |
153 | if ( ! evt.wasClean ){ |
|
153 | if ( ! evt.wasClean ){ | |
154 | that._websocket_closed(ws_url, false); |
|
154 | that._websocket_closed(ws_url, false); | |
155 | } |
|
155 | } | |
156 | }; |
|
156 | }; | |
157 | var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel]; |
|
157 | var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel]; | |
158 | for (var i=0; i < channels.length; i++) { |
|
158 | for (var i=0; i < channels.length; i++) { | |
159 | channels[i].onopen = send_cookie; |
|
159 | channels[i].onopen = send_cookie; | |
160 | channels[i].onclose = ws_closed_early; |
|
160 | channels[i].onclose = ws_closed_early; | |
161 | } |
|
161 | } | |
162 | // switch from early-close to late-close message after 1s |
|
162 | // switch from early-close to late-close message after 1s | |
163 | setTimeout(function() { |
|
163 | setTimeout(function() { | |
164 | for (var i=0; i < channels.length; i++) { |
|
164 | for (var i=0; i < channels.length; i++) { | |
165 | if (channels[i] !== null) { |
|
165 | if (channels[i] !== null) { | |
166 | channels[i].onclose = ws_closed_late; |
|
166 | channels[i].onclose = ws_closed_late; | |
167 | } |
|
167 | } | |
168 | } |
|
168 | } | |
169 | }, 1000); |
|
169 | }, 1000); | |
170 | this.shell_channel.onmessage = $.proxy(this._handle_shell_reply, this); |
|
170 | this.shell_channel.onmessage = $.proxy(this._handle_shell_reply, this); | |
171 | this.iopub_channel.onmessage = $.proxy(this._handle_iopub_reply, this); |
|
171 | this.iopub_channel.onmessage = $.proxy(this._handle_iopub_reply, this); | |
172 | this.stdin_channel.onmessage = $.proxy(this._handle_input_request, this); |
|
172 | this.stdin_channel.onmessage = $.proxy(this._handle_input_request, this); | |
173 |
|
173 | |||
174 | $([IPython.events]).on('send_input_reply.Kernel', function(evt, data) { |
|
174 | $([IPython.events]).on('send_input_reply.Kernel', function(evt, data) { | |
175 | that.send_input_reply(data); |
|
175 | that.send_input_reply(data); | |
176 | }); |
|
176 | }); | |
177 | }; |
|
177 | }; | |
178 |
|
178 | |||
179 | /** |
|
179 | /** | |
180 | * Start the `shell`and `iopub` channels. |
|
180 | * Start the `shell`and `iopub` channels. | |
181 | * @method stop_channels |
|
181 | * @method stop_channels | |
182 | */ |
|
182 | */ | |
183 | Kernel.prototype.stop_channels = function () { |
|
183 | Kernel.prototype.stop_channels = function () { | |
184 | var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel]; |
|
184 | var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel]; | |
185 | for (var i=0; i < channels.length; i++) { |
|
185 | for (var i=0; i < channels.length; i++) { | |
186 | if ( channels[i] !== null ) { |
|
186 | if ( channels[i] !== null ) { | |
187 | channels[i].onclose = function (evt) {}; |
|
187 | channels[i].onclose = function (evt) {}; | |
188 | channels[i].close(); |
|
188 | channels[i].close(); | |
189 | } |
|
189 | } | |
190 | }; |
|
190 | }; | |
191 | this.shell_channel = this.iopub_channel = this.stdin_channel = null; |
|
191 | this.shell_channel = this.iopub_channel = this.stdin_channel = null; | |
192 | }; |
|
192 | }; | |
193 |
|
193 | |||
194 | // Main public methods. |
|
194 | // Main public methods. | |
195 |
|
195 | |||
196 | /** |
|
196 | /** | |
197 | * Get info on object asynchronoulsy |
|
197 | * Get info on object asynchronoulsy | |
198 | * |
|
198 | * | |
199 | * @async |
|
199 | * @async | |
200 | * @param objname {string} |
|
200 | * @param objname {string} | |
201 | * @param callback {dict} |
|
201 | * @param callback {dict} | |
202 | * @method object_info_request |
|
202 | * @method object_info_request | |
203 | * |
|
203 | * | |
204 | * @example |
|
204 | * @example | |
205 | * |
|
205 | * | |
206 | * When calling this method pass a callbacks structure of the form: |
|
206 | * When calling this method pass a callbacks structure of the form: | |
207 | * |
|
207 | * | |
208 | * callbacks = { |
|
208 | * callbacks = { | |
209 | * 'object_info_reply': object_info_reply_callback |
|
209 | * 'object_info_reply': object_info_reply_callback | |
210 | * } |
|
210 | * } | |
211 | * |
|
211 | * | |
212 | * The `object_info_reply_callback` will be passed the content object of the |
|
212 | * The `object_info_reply_callback` will be passed the content object of the | |
213 | * |
|
213 | * | |
214 | * `object_into_reply` message documented in |
|
214 | * `object_into_reply` message documented in | |
215 | * [IPython dev documentation](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information) |
|
215 | * [IPython dev documentation](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information) | |
216 | */ |
|
216 | */ | |
217 | Kernel.prototype.object_info_request = function (objname, callbacks) { |
|
217 | Kernel.prototype.object_info_request = function (objname, callbacks) { | |
218 | if(typeof(objname)!=null && objname!=null) |
|
218 | if(typeof(objname)!=null && objname!=null) | |
219 | { |
|
219 | { | |
220 | var content = { |
|
220 | var content = { | |
221 | oname : objname.toString(), |
|
221 | oname : objname.toString(), | |
222 | }; |
|
222 | }; | |
223 | var msg = this._get_msg("object_info_request", content); |
|
223 | var msg = this._get_msg("object_info_request", content); | |
224 | this.shell_channel.send(JSON.stringify(msg)); |
|
224 | this.shell_channel.send(JSON.stringify(msg)); | |
225 | this.set_callbacks_for_msg(msg.header.msg_id, callbacks); |
|
225 | this.set_callbacks_for_msg(msg.header.msg_id, callbacks); | |
226 | return msg.header.msg_id; |
|
226 | return msg.header.msg_id; | |
227 | } |
|
227 | } | |
228 | return; |
|
228 | return; | |
229 | } |
|
229 | } | |
230 |
|
230 | |||
231 | /** |
|
231 | /** | |
232 | * Execute given code into kernel, and pass result to callback. |
|
232 | * Execute given code into kernel, and pass result to callback. | |
233 | * |
|
233 | * | |
234 | * TODO: document input_request in callbacks |
|
234 | * TODO: document input_request in callbacks | |
235 | * |
|
235 | * | |
236 | * @async |
|
236 | * @async | |
237 | * @method execute |
|
237 | * @method execute | |
238 | * @param {string} code |
|
238 | * @param {string} code | |
239 |
* @param [callbacks] {Object} With the option |
|
239 | * @param [callbacks] {Object} With the optional following keys | |
240 | * @param callbacks.'execute_reply' {function} |
|
240 | * @param callbacks.'execute_reply' {function} | |
241 | * @param callbacks.'output' {function} |
|
241 | * @param callbacks.'output' {function} | |
242 | * @param callbacks.'clear_output' {function} |
|
242 | * @param callbacks.'clear_output' {function} | |
243 | * @param callbacks.'set_next_input' {function} |
|
243 | * @param callbacks.'set_next_input' {function} | |
244 | * @param {object} [options] |
|
244 | * @param {object} [options] | |
245 | * @param [options.silent=false] {Boolean} |
|
245 | * @param [options.silent=false] {Boolean} | |
246 | * @param [options.user_expressions=empty_dict] {Dict} |
|
246 | * @param [options.user_expressions=empty_dict] {Dict} | |
247 | * @param [options.user_variables=empty_list] {List od Strings} |
|
247 | * @param [options.user_variables=empty_list] {List od Strings} | |
248 | * @param [options.allow_stdin=false] {Boolean} true|false |
|
248 | * @param [options.allow_stdin=false] {Boolean} true|false | |
249 | * |
|
249 | * | |
250 | * @example |
|
250 | * @example | |
251 | * |
|
251 | * | |
252 | * The options object should contain the options for the execute call. Its default |
|
252 | * The options object should contain the options for the execute call. Its default | |
253 | * values are: |
|
253 | * values are: | |
254 | * |
|
254 | * | |
255 | * options = { |
|
255 | * options = { | |
256 | * silent : true, |
|
256 | * silent : true, | |
257 | * user_variables : [], |
|
257 | * user_variables : [], | |
258 | * user_expressions : {}, |
|
258 | * user_expressions : {}, | |
259 | * allow_stdin : false |
|
259 | * allow_stdin : false | |
260 | * } |
|
260 | * } | |
261 | * |
|
261 | * | |
262 | * When calling this method pass a callbacks structure of the form: |
|
262 | * When calling this method pass a callbacks structure of the form: | |
263 | * |
|
263 | * | |
264 | * callbacks = { |
|
264 | * callbacks = { | |
265 | * 'execute_reply': execute_reply_callback, |
|
265 | * 'execute_reply': execute_reply_callback, | |
266 | * 'output': output_callback, |
|
266 | * 'output': output_callback, | |
267 | * 'clear_output': clear_output_callback, |
|
267 | * 'clear_output': clear_output_callback, | |
268 | * 'set_next_input': set_next_input_callback |
|
268 | * 'set_next_input': set_next_input_callback | |
269 | * } |
|
269 | * } | |
270 | * |
|
270 | * | |
271 | * The `execute_reply_callback` will be passed the content and metadata |
|
271 | * The `execute_reply_callback` will be passed the content and metadata | |
272 | * objects of the `execute_reply` message documented |
|
272 | * objects of the `execute_reply` message documented | |
273 | * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#execute) |
|
273 | * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#execute) | |
274 | * |
|
274 | * | |
275 | * The `output_callback` will be passed `msg_type` ('stream','display_data','pyout','pyerr') |
|
275 | * The `output_callback` will be passed `msg_type` ('stream','display_data','pyout','pyerr') | |
276 | * of the output and the content and metadata objects of the PUB/SUB channel that contains the |
|
276 | * of the output and the content and metadata objects of the PUB/SUB channel that contains the | |
277 | * output: |
|
277 | * output: | |
278 | * |
|
278 | * | |
279 | * http://ipython.org/ipython-doc/dev/development/messaging.html#messages-on-the-pub-sub-socket |
|
279 | * http://ipython.org/ipython-doc/dev/development/messaging.html#messages-on-the-pub-sub-socket | |
280 | * |
|
280 | * | |
281 | * The `clear_output_callback` will be passed a content object that contains |
|
281 | * The `clear_output_callback` will be passed a content object that contains | |
282 | * stdout, stderr and other fields that are booleans, as well as the metadata object. |
|
282 | * stdout, stderr and other fields that are booleans, as well as the metadata object. | |
283 | * |
|
283 | * | |
284 | * The `set_next_input_callback` will be passed the text that should become the next |
|
284 | * The `set_next_input_callback` will be passed the text that should become the next | |
285 | * input cell. |
|
285 | * input cell. | |
286 | */ |
|
286 | */ | |
287 | Kernel.prototype.execute = function (code, callbacks, options) { |
|
287 | Kernel.prototype.execute = function (code, callbacks, options) { | |
288 |
|
288 | |||
289 | var content = { |
|
289 | var content = { | |
290 | code : code, |
|
290 | code : code, | |
291 | silent : true, |
|
291 | silent : true, | |
292 | user_variables : [], |
|
292 | user_variables : [], | |
293 | user_expressions : {}, |
|
293 | user_expressions : {}, | |
294 | allow_stdin : false |
|
294 | allow_stdin : false | |
295 | }; |
|
295 | }; | |
296 | callbacks = callbacks || {}; |
|
296 | callbacks = callbacks || {}; | |
297 | if (callbacks.input_request !== undefined) { |
|
297 | if (callbacks.input_request !== undefined) { | |
298 | content.allow_stdin = true; |
|
298 | content.allow_stdin = true; | |
299 | } |
|
299 | } | |
300 | $.extend(true, content, options) |
|
300 | $.extend(true, content, options) | |
301 | $([IPython.events]).trigger('execution_request.Kernel', {kernel: this, content:content}); |
|
301 | $([IPython.events]).trigger('execution_request.Kernel', {kernel: this, content:content}); | |
302 | var msg = this._get_msg("execute_request", content); |
|
302 | var msg = this._get_msg("execute_request", content); | |
303 | this.shell_channel.send(JSON.stringify(msg)); |
|
303 | this.shell_channel.send(JSON.stringify(msg)); | |
304 | this.set_callbacks_for_msg(msg.header.msg_id, callbacks); |
|
304 | this.set_callbacks_for_msg(msg.header.msg_id, callbacks); | |
305 | return msg.header.msg_id; |
|
305 | return msg.header.msg_id; | |
306 | }; |
|
306 | }; | |
307 |
|
307 | |||
308 | /** |
|
308 | /** | |
309 | * When calling this method pass a callbacks structure of the form: |
|
309 | * When calling this method pass a callbacks structure of the form: | |
310 | * |
|
310 | * | |
311 | * callbacks = { |
|
311 | * callbacks = { | |
312 | * 'complete_reply': complete_reply_callback |
|
312 | * 'complete_reply': complete_reply_callback | |
313 | * } |
|
313 | * } | |
314 | * |
|
314 | * | |
315 | * The `complete_reply_callback` will be passed the content object of the |
|
315 | * The `complete_reply_callback` will be passed the content object of the | |
316 | * `complete_reply` message documented |
|
316 | * `complete_reply` message documented | |
317 | * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete) |
|
317 | * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete) | |
318 | * |
|
318 | * | |
319 | * @method complete |
|
319 | * @method complete | |
320 | * @param line {integer} |
|
320 | * @param line {integer} | |
321 | * @param cursor_pos {integer} |
|
321 | * @param cursor_pos {integer} | |
322 | * @param {dict} callbacks |
|
322 | * @param {dict} callbacks | |
323 | * @param callbacks.complete_reply {function} `complete_reply_callback` |
|
323 | * @param callbacks.complete_reply {function} `complete_reply_callback` | |
324 | * |
|
324 | * | |
325 | */ |
|
325 | */ | |
326 | Kernel.prototype.complete = function (line, cursor_pos, callbacks) { |
|
326 | Kernel.prototype.complete = function (line, cursor_pos, callbacks) { | |
327 | callbacks = callbacks || {}; |
|
327 | callbacks = callbacks || {}; | |
328 | var content = { |
|
328 | var content = { | |
329 | text : '', |
|
329 | text : '', | |
330 | line : line, |
|
330 | line : line, | |
331 | cursor_pos : cursor_pos |
|
331 | cursor_pos : cursor_pos | |
332 | }; |
|
332 | }; | |
333 | var msg = this._get_msg("complete_request", content); |
|
333 | var msg = this._get_msg("complete_request", content); | |
334 | this.shell_channel.send(JSON.stringify(msg)); |
|
334 | this.shell_channel.send(JSON.stringify(msg)); | |
335 | this.set_callbacks_for_msg(msg.header.msg_id, callbacks); |
|
335 | this.set_callbacks_for_msg(msg.header.msg_id, callbacks); | |
336 | return msg.header.msg_id; |
|
336 | return msg.header.msg_id; | |
337 | }; |
|
337 | }; | |
338 |
|
338 | |||
339 |
|
339 | |||
340 | Kernel.prototype.interrupt = function () { |
|
340 | Kernel.prototype.interrupt = function () { | |
341 | if (this.running) { |
|
341 | if (this.running) { | |
342 | $([IPython.events]).trigger('status_interrupting.Kernel', {kernel: this}); |
|
342 | $([IPython.events]).trigger('status_interrupting.Kernel', {kernel: this}); | |
343 | $.post(this.kernel_url + "/interrupt"); |
|
343 | $.post(this.kernel_url + "/interrupt"); | |
344 | }; |
|
344 | }; | |
345 | }; |
|
345 | }; | |
346 |
|
346 | |||
347 |
|
347 | |||
348 | Kernel.prototype.kill = function () { |
|
348 | Kernel.prototype.kill = function () { | |
349 | if (this.running) { |
|
349 | if (this.running) { | |
350 | this.running = false; |
|
350 | this.running = false; | |
351 | var settings = { |
|
351 | var settings = { | |
352 | cache : false, |
|
352 | cache : false, | |
353 | type : "DELETE" |
|
353 | type : "DELETE" | |
354 | }; |
|
354 | }; | |
355 | $.ajax(this.kernel_url, settings); |
|
355 | $.ajax(this.kernel_url, settings); | |
356 | }; |
|
356 | }; | |
357 | }; |
|
357 | }; | |
358 |
|
358 | |||
359 | Kernel.prototype.send_input_reply = function (input) { |
|
359 | Kernel.prototype.send_input_reply = function (input) { | |
360 | var content = { |
|
360 | var content = { | |
361 | value : input, |
|
361 | value : input, | |
362 | }; |
|
362 | }; | |
363 | $([IPython.events]).trigger('input_reply.Kernel', {kernel: this, content:content}); |
|
363 | $([IPython.events]).trigger('input_reply.Kernel', {kernel: this, content:content}); | |
364 | var msg = this._get_msg("input_reply", content); |
|
364 | var msg = this._get_msg("input_reply", content); | |
365 | this.stdin_channel.send(JSON.stringify(msg)); |
|
365 | this.stdin_channel.send(JSON.stringify(msg)); | |
366 | return msg.header.msg_id; |
|
366 | return msg.header.msg_id; | |
367 | }; |
|
367 | }; | |
368 |
|
368 | |||
369 |
|
369 | |||
370 | // Reply handlers |
|
370 | // Reply handlers | |
371 |
|
371 | |||
372 | Kernel.prototype.get_callbacks_for_msg = function (msg_id) { |
|
372 | Kernel.prototype.get_callbacks_for_msg = function (msg_id) { | |
373 | var callbacks = this._msg_callbacks[msg_id]; |
|
373 | var callbacks = this._msg_callbacks[msg_id]; | |
374 | return callbacks; |
|
374 | return callbacks; | |
375 | }; |
|
375 | }; | |
376 |
|
376 | |||
377 |
|
377 | |||
378 | Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) { |
|
378 | Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) { | |
379 | this._msg_callbacks[msg_id] = callbacks || {}; |
|
379 | this._msg_callbacks[msg_id] = callbacks || {}; | |
380 | } |
|
380 | } | |
381 |
|
381 | |||
382 |
|
382 | |||
383 | Kernel.prototype._handle_shell_reply = function (e) { |
|
383 | Kernel.prototype._handle_shell_reply = function (e) { | |
384 | var reply = $.parseJSON(e.data); |
|
384 | var reply = $.parseJSON(e.data); | |
385 | $([IPython.events]).trigger('shell_reply.Kernel', {kernel: this, reply:reply}); |
|
385 | $([IPython.events]).trigger('shell_reply.Kernel', {kernel: this, reply:reply}); | |
386 | var header = reply.header; |
|
386 | var header = reply.header; | |
387 | var content = reply.content; |
|
387 | var content = reply.content; | |
388 | var metadata = reply.metadata; |
|
388 | var metadata = reply.metadata; | |
389 | var msg_type = header.msg_type; |
|
389 | var msg_type = header.msg_type; | |
390 | var callbacks = this.get_callbacks_for_msg(reply.parent_header.msg_id); |
|
390 | var callbacks = this.get_callbacks_for_msg(reply.parent_header.msg_id); | |
391 | if (callbacks !== undefined) { |
|
391 | if (callbacks !== undefined) { | |
392 | var cb = callbacks[msg_type]; |
|
392 | var cb = callbacks[msg_type]; | |
393 | if (cb !== undefined) { |
|
393 | if (cb !== undefined) { | |
394 | cb(content, metadata); |
|
394 | cb(content, metadata); | |
395 | } |
|
395 | } | |
396 | }; |
|
396 | }; | |
397 |
|
397 | |||
398 | if (content.payload !== undefined) { |
|
398 | if (content.payload !== undefined) { | |
399 | var payload = content.payload || []; |
|
399 | var payload = content.payload || []; | |
400 | this._handle_payload(callbacks, payload); |
|
400 | this._handle_payload(callbacks, payload); | |
401 | } |
|
401 | } | |
402 | }; |
|
402 | }; | |
403 |
|
403 | |||
404 |
|
404 | |||
405 | Kernel.prototype._handle_payload = function (callbacks, payload) { |
|
405 | Kernel.prototype._handle_payload = function (callbacks, payload) { | |
406 | var l = payload.length; |
|
406 | var l = payload.length; | |
407 | // Payloads are handled by triggering events because we don't want the Kernel |
|
407 | // Payloads are handled by triggering events because we don't want the Kernel | |
408 | // to depend on the Notebook or Pager classes. |
|
408 | // to depend on the Notebook or Pager classes. | |
409 | for (var i=0; i<l; i++) { |
|
409 | for (var i=0; i<l; i++) { | |
410 | if (payload[i].source === 'IPython.kernel.zmq.page.page') { |
|
410 | if (payload[i].source === 'IPython.kernel.zmq.page.page') { | |
411 | var data = {'text':payload[i].text} |
|
411 | var data = {'text':payload[i].text} | |
412 | $([IPython.events]).trigger('open_with_text.Pager', data); |
|
412 | $([IPython.events]).trigger('open_with_text.Pager', data); | |
413 | } else if (payload[i].source === 'IPython.kernel.zmq.zmqshell.ZMQInteractiveShell.set_next_input') { |
|
413 | } else if (payload[i].source === 'IPython.kernel.zmq.zmqshell.ZMQInteractiveShell.set_next_input') { | |
414 | if (callbacks.set_next_input !== undefined) { |
|
414 | if (callbacks.set_next_input !== undefined) { | |
415 | callbacks.set_next_input(payload[i].text) |
|
415 | callbacks.set_next_input(payload[i].text) | |
416 | } |
|
416 | } | |
417 | } |
|
417 | } | |
418 | }; |
|
418 | }; | |
419 | }; |
|
419 | }; | |
420 |
|
420 | |||
421 |
|
421 | |||
422 | Kernel.prototype._handle_iopub_reply = function (e) { |
|
422 | Kernel.prototype._handle_iopub_reply = function (e) { | |
423 | var reply = $.parseJSON(e.data); |
|
423 | var reply = $.parseJSON(e.data); | |
424 | var content = reply.content; |
|
424 | var content = reply.content; | |
425 | var msg_type = reply.header.msg_type; |
|
425 | var msg_type = reply.header.msg_type; | |
426 | var metadata = reply.metadata; |
|
426 | var metadata = reply.metadata; | |
427 | var callbacks = this.get_callbacks_for_msg(reply.parent_header.msg_id); |
|
427 | var callbacks = this.get_callbacks_for_msg(reply.parent_header.msg_id); | |
428 | if (msg_type !== 'status' && callbacks === undefined) { |
|
428 | if (msg_type !== 'status' && callbacks === undefined) { | |
429 | // Message not from one of this notebook's cells and there are no |
|
429 | // Message not from one of this notebook's cells and there are no | |
430 | // callbacks to handle it. |
|
430 | // callbacks to handle it. | |
431 | return; |
|
431 | return; | |
432 | } |
|
432 | } | |
433 | var output_types = ['stream','display_data','pyout','pyerr']; |
|
433 | var output_types = ['stream','display_data','pyout','pyerr']; | |
434 | if (output_types.indexOf(msg_type) >= 0) { |
|
434 | if (output_types.indexOf(msg_type) >= 0) { | |
435 | var cb = callbacks['output']; |
|
435 | var cb = callbacks['output']; | |
436 | if (cb !== undefined) { |
|
436 | if (cb !== undefined) { | |
437 | cb(msg_type, content, metadata); |
|
437 | cb(msg_type, content, metadata); | |
438 | } |
|
438 | } | |
439 | } else if (msg_type === 'status') { |
|
439 | } else if (msg_type === 'status') { | |
440 | if (content.execution_state === 'busy') { |
|
440 | if (content.execution_state === 'busy') { | |
441 | $([IPython.events]).trigger('status_busy.Kernel', {kernel: this}); |
|
441 | $([IPython.events]).trigger('status_busy.Kernel', {kernel: this}); | |
442 | } else if (content.execution_state === 'idle') { |
|
442 | } else if (content.execution_state === 'idle') { | |
443 | $([IPython.events]).trigger('status_idle.Kernel', {kernel: this}); |
|
443 | $([IPython.events]).trigger('status_idle.Kernel', {kernel: this}); | |
444 | } else if (content.execution_state === 'restarting') { |
|
444 | } else if (content.execution_state === 'restarting') { | |
445 | $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this}); |
|
445 | $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this}); | |
446 | } else if (content.execution_state === 'dead') { |
|
446 | } else if (content.execution_state === 'dead') { | |
447 | this.stop_channels(); |
|
447 | this.stop_channels(); | |
448 | $([IPython.events]).trigger('status_dead.Kernel', {kernel: this}); |
|
448 | $([IPython.events]).trigger('status_dead.Kernel', {kernel: this}); | |
449 | }; |
|
449 | }; | |
450 | } else if (msg_type === 'clear_output') { |
|
450 | } else if (msg_type === 'clear_output') { | |
451 | var cb = callbacks['clear_output']; |
|
451 | var cb = callbacks['clear_output']; | |
452 | if (cb !== undefined) { |
|
452 | if (cb !== undefined) { | |
453 | cb(content, metadata); |
|
453 | cb(content, metadata); | |
454 | } |
|
454 | } | |
455 | }; |
|
455 | }; | |
456 | }; |
|
456 | }; | |
457 |
|
457 | |||
458 |
|
458 | |||
459 | Kernel.prototype._handle_input_request = function (e) { |
|
459 | Kernel.prototype._handle_input_request = function (e) { | |
460 | var request = $.parseJSON(e.data); |
|
460 | var request = $.parseJSON(e.data); | |
461 | var header = request.header; |
|
461 | var header = request.header; | |
462 | var content = request.content; |
|
462 | var content = request.content; | |
463 | var metadata = request.metadata; |
|
463 | var metadata = request.metadata; | |
464 | var msg_type = header.msg_type; |
|
464 | var msg_type = header.msg_type; | |
465 | if (msg_type !== 'input_request') { |
|
465 | if (msg_type !== 'input_request') { | |
466 | console.log("Invalid input request!", request); |
|
466 | console.log("Invalid input request!", request); | |
467 | return; |
|
467 | return; | |
468 | } |
|
468 | } | |
469 | var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id); |
|
469 | var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id); | |
470 | if (callbacks !== undefined) { |
|
470 | if (callbacks !== undefined) { | |
471 | var cb = callbacks[msg_type]; |
|
471 | var cb = callbacks[msg_type]; | |
472 | if (cb !== undefined) { |
|
472 | if (cb !== undefined) { | |
473 | cb(content, metadata); |
|
473 | cb(content, metadata); | |
474 | } |
|
474 | } | |
475 | }; |
|
475 | }; | |
476 | }; |
|
476 | }; | |
477 |
|
477 | |||
478 |
|
478 | |||
479 | IPython.Kernel = Kernel; |
|
479 | IPython.Kernel = Kernel; | |
480 |
|
480 | |||
481 | return IPython; |
|
481 | return IPython; | |
482 |
|
482 | |||
483 | }(IPython)); |
|
483 | }(IPython)); | |
484 |
|
484 |
General Comments 0
You need to be logged in to leave comments.
Login now