Show More
@@ -1,488 +1,490 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 | var ws_url = json.ws_url; |
|
105 | var ws_url = json.ws_url; | |
106 | if (ws_url.match(/wss?:\/\//) == null) { |
|
106 | if (ws_url.match(/wss?:\/\//) == null) { | |
107 | ws_url = "ws" + location.origin.substr(4) + ws_url; |
|
107 | // trailing 's' in https will become wss for secure web sockets | |
|
108 | prot = location.protocol.replace('http', 'ws') + "//"; | |||
|
109 | ws_url = prot + location.host + ws_url; | |||
108 | }; |
|
110 | }; | |
109 | this.ws_url = ws_url; |
|
111 | this.ws_url = ws_url; | |
110 | this.kernel_url = this.base_url + "/" + this.kernel_id; |
|
112 | this.kernel_url = this.base_url + "/" + this.kernel_id; | |
111 | this.start_channels(); |
|
113 | this.start_channels(); | |
112 | $([IPython.events]).trigger('status_started.Kernel', {kernel: this}); |
|
114 | $([IPython.events]).trigger('status_started.Kernel', {kernel: this}); | |
113 | }; |
|
115 | }; | |
114 |
|
116 | |||
115 |
|
117 | |||
116 | Kernel.prototype._websocket_closed = function(ws_url, early) { |
|
118 | Kernel.prototype._websocket_closed = function(ws_url, early) { | |
117 | this.stop_channels(); |
|
119 | this.stop_channels(); | |
118 | $([IPython.events]).trigger('websocket_closed.Kernel', |
|
120 | $([IPython.events]).trigger('websocket_closed.Kernel', | |
119 | {ws_url: ws_url, kernel: this, early: early} |
|
121 | {ws_url: ws_url, kernel: this, early: early} | |
120 | ); |
|
122 | ); | |
121 | }; |
|
123 | }; | |
122 |
|
124 | |||
123 | /** |
|
125 | /** | |
124 | * Start the `shell`and `iopub` channels. |
|
126 | * Start the `shell`and `iopub` channels. | |
125 | * Will stop and restart them if they already exist. |
|
127 | * Will stop and restart them if they already exist. | |
126 | * |
|
128 | * | |
127 | * @method start_channels |
|
129 | * @method start_channels | |
128 | */ |
|
130 | */ | |
129 | Kernel.prototype.start_channels = function () { |
|
131 | Kernel.prototype.start_channels = function () { | |
130 | var that = this; |
|
132 | var that = this; | |
131 | this.stop_channels(); |
|
133 | this.stop_channels(); | |
132 | var ws_url = this.ws_url + this.kernel_url; |
|
134 | var ws_url = this.ws_url + this.kernel_url; | |
133 | console.log("Starting WebSockets:", ws_url); |
|
135 | console.log("Starting WebSockets:", ws_url); | |
134 | this.shell_channel = new this.WebSocket(ws_url + "/shell"); |
|
136 | this.shell_channel = new this.WebSocket(ws_url + "/shell"); | |
135 | this.stdin_channel = new this.WebSocket(ws_url + "/stdin"); |
|
137 | this.stdin_channel = new this.WebSocket(ws_url + "/stdin"); | |
136 | this.iopub_channel = new this.WebSocket(ws_url + "/iopub"); |
|
138 | this.iopub_channel = new this.WebSocket(ws_url + "/iopub"); | |
137 | send_cookie = function(){ |
|
139 | send_cookie = function(){ | |
138 | // send the session id so the Session object Python-side |
|
140 | // send the session id so the Session object Python-side | |
139 | // has the same identity |
|
141 | // has the same identity | |
140 | this.send(that.session_id + ':' + document.cookie); |
|
142 | this.send(that.session_id + ':' + document.cookie); | |
141 | }; |
|
143 | }; | |
142 | var already_called_onclose = false; // only alert once |
|
144 | var already_called_onclose = false; // only alert once | |
143 | var ws_closed_early = function(evt){ |
|
145 | var ws_closed_early = function(evt){ | |
144 | if (already_called_onclose){ |
|
146 | if (already_called_onclose){ | |
145 | return; |
|
147 | return; | |
146 | } |
|
148 | } | |
147 | already_called_onclose = true; |
|
149 | already_called_onclose = true; | |
148 | if ( ! evt.wasClean ){ |
|
150 | if ( ! evt.wasClean ){ | |
149 | that._websocket_closed(ws_url, true); |
|
151 | that._websocket_closed(ws_url, true); | |
150 | } |
|
152 | } | |
151 | }; |
|
153 | }; | |
152 | var ws_closed_late = function(evt){ |
|
154 | var ws_closed_late = function(evt){ | |
153 | if (already_called_onclose){ |
|
155 | if (already_called_onclose){ | |
154 | return; |
|
156 | return; | |
155 | } |
|
157 | } | |
156 | already_called_onclose = true; |
|
158 | already_called_onclose = true; | |
157 | if ( ! evt.wasClean ){ |
|
159 | if ( ! evt.wasClean ){ | |
158 | that._websocket_closed(ws_url, false); |
|
160 | that._websocket_closed(ws_url, false); | |
159 | } |
|
161 | } | |
160 | }; |
|
162 | }; | |
161 | var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel]; |
|
163 | var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel]; | |
162 | for (var i=0; i < channels.length; i++) { |
|
164 | for (var i=0; i < channels.length; i++) { | |
163 | channels[i].onopen = send_cookie; |
|
165 | channels[i].onopen = send_cookie; | |
164 | channels[i].onclose = ws_closed_early; |
|
166 | channels[i].onclose = ws_closed_early; | |
165 | } |
|
167 | } | |
166 | // switch from early-close to late-close message after 1s |
|
168 | // switch from early-close to late-close message after 1s | |
167 | setTimeout(function() { |
|
169 | setTimeout(function() { | |
168 | for (var i=0; i < channels.length; i++) { |
|
170 | for (var i=0; i < channels.length; i++) { | |
169 | if (channels[i] !== null) { |
|
171 | if (channels[i] !== null) { | |
170 | channels[i].onclose = ws_closed_late; |
|
172 | channels[i].onclose = ws_closed_late; | |
171 | } |
|
173 | } | |
172 | } |
|
174 | } | |
173 | }, 1000); |
|
175 | }, 1000); | |
174 | this.shell_channel.onmessage = $.proxy(this._handle_shell_reply, this); |
|
176 | this.shell_channel.onmessage = $.proxy(this._handle_shell_reply, this); | |
175 | this.iopub_channel.onmessage = $.proxy(this._handle_iopub_reply, this); |
|
177 | this.iopub_channel.onmessage = $.proxy(this._handle_iopub_reply, this); | |
176 | this.stdin_channel.onmessage = $.proxy(this._handle_input_request, this); |
|
178 | this.stdin_channel.onmessage = $.proxy(this._handle_input_request, this); | |
177 |
|
179 | |||
178 | $([IPython.events]).on('send_input_reply.Kernel', function(evt, data) { |
|
180 | $([IPython.events]).on('send_input_reply.Kernel', function(evt, data) { | |
179 | that.send_input_reply(data); |
|
181 | that.send_input_reply(data); | |
180 | }); |
|
182 | }); | |
181 | }; |
|
183 | }; | |
182 |
|
184 | |||
183 | /** |
|
185 | /** | |
184 | * Start the `shell`and `iopub` channels. |
|
186 | * Start the `shell`and `iopub` channels. | |
185 | * @method stop_channels |
|
187 | * @method stop_channels | |
186 | */ |
|
188 | */ | |
187 | Kernel.prototype.stop_channels = function () { |
|
189 | Kernel.prototype.stop_channels = function () { | |
188 | var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel]; |
|
190 | var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel]; | |
189 | for (var i=0; i < channels.length; i++) { |
|
191 | for (var i=0; i < channels.length; i++) { | |
190 | if ( channels[i] !== null ) { |
|
192 | if ( channels[i] !== null ) { | |
191 | channels[i].onclose = function (evt) {}; |
|
193 | channels[i].onclose = function (evt) {}; | |
192 | channels[i].close(); |
|
194 | channels[i].close(); | |
193 | } |
|
195 | } | |
194 | }; |
|
196 | }; | |
195 | this.shell_channel = this.iopub_channel = this.stdin_channel = null; |
|
197 | this.shell_channel = this.iopub_channel = this.stdin_channel = null; | |
196 | }; |
|
198 | }; | |
197 |
|
199 | |||
198 | // Main public methods. |
|
200 | // Main public methods. | |
199 |
|
201 | |||
200 | /** |
|
202 | /** | |
201 | * Get info on object asynchronoulsy |
|
203 | * Get info on object asynchronoulsy | |
202 | * |
|
204 | * | |
203 | * @async |
|
205 | * @async | |
204 | * @param objname {string} |
|
206 | * @param objname {string} | |
205 | * @param callback {dict} |
|
207 | * @param callback {dict} | |
206 | * @method object_info_request |
|
208 | * @method object_info_request | |
207 | * |
|
209 | * | |
208 | * @example |
|
210 | * @example | |
209 | * |
|
211 | * | |
210 | * When calling this method pass a callbacks structure of the form: |
|
212 | * When calling this method pass a callbacks structure of the form: | |
211 | * |
|
213 | * | |
212 | * callbacks = { |
|
214 | * callbacks = { | |
213 | * 'object_info_reply': object_info_reply_callback |
|
215 | * 'object_info_reply': object_info_reply_callback | |
214 | * } |
|
216 | * } | |
215 | * |
|
217 | * | |
216 | * The `object_info_reply_callback` will be passed the content object of the |
|
218 | * The `object_info_reply_callback` will be passed the content object of the | |
217 | * |
|
219 | * | |
218 | * `object_into_reply` message documented in |
|
220 | * `object_into_reply` message documented in | |
219 | * [IPython dev documentation](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information) |
|
221 | * [IPython dev documentation](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information) | |
220 | */ |
|
222 | */ | |
221 | Kernel.prototype.object_info_request = function (objname, callbacks) { |
|
223 | Kernel.prototype.object_info_request = function (objname, callbacks) { | |
222 | if(typeof(objname)!=null && objname!=null) |
|
224 | if(typeof(objname)!=null && objname!=null) | |
223 | { |
|
225 | { | |
224 | var content = { |
|
226 | var content = { | |
225 | oname : objname.toString(), |
|
227 | oname : objname.toString(), | |
226 | }; |
|
228 | }; | |
227 | var msg = this._get_msg("object_info_request", content); |
|
229 | var msg = this._get_msg("object_info_request", content); | |
228 | this.shell_channel.send(JSON.stringify(msg)); |
|
230 | this.shell_channel.send(JSON.stringify(msg)); | |
229 | this.set_callbacks_for_msg(msg.header.msg_id, callbacks); |
|
231 | this.set_callbacks_for_msg(msg.header.msg_id, callbacks); | |
230 | return msg.header.msg_id; |
|
232 | return msg.header.msg_id; | |
231 | } |
|
233 | } | |
232 | return; |
|
234 | return; | |
233 | } |
|
235 | } | |
234 |
|
236 | |||
235 | /** |
|
237 | /** | |
236 | * Execute given code into kernel, and pass result to callback. |
|
238 | * Execute given code into kernel, and pass result to callback. | |
237 | * |
|
239 | * | |
238 | * TODO: document input_request in callbacks |
|
240 | * TODO: document input_request in callbacks | |
239 | * |
|
241 | * | |
240 | * @async |
|
242 | * @async | |
241 | * @method execute |
|
243 | * @method execute | |
242 | * @param {string} code |
|
244 | * @param {string} code | |
243 | * @param [callbacks] {Object} With the optional following keys |
|
245 | * @param [callbacks] {Object} With the optional following keys | |
244 | * @param callbacks.'execute_reply' {function} |
|
246 | * @param callbacks.'execute_reply' {function} | |
245 | * @param callbacks.'output' {function} |
|
247 | * @param callbacks.'output' {function} | |
246 | * @param callbacks.'clear_output' {function} |
|
248 | * @param callbacks.'clear_output' {function} | |
247 | * @param callbacks.'set_next_input' {function} |
|
249 | * @param callbacks.'set_next_input' {function} | |
248 | * @param {object} [options] |
|
250 | * @param {object} [options] | |
249 | * @param [options.silent=false] {Boolean} |
|
251 | * @param [options.silent=false] {Boolean} | |
250 | * @param [options.user_expressions=empty_dict] {Dict} |
|
252 | * @param [options.user_expressions=empty_dict] {Dict} | |
251 | * @param [options.user_variables=empty_list] {List od Strings} |
|
253 | * @param [options.user_variables=empty_list] {List od Strings} | |
252 | * @param [options.allow_stdin=false] {Boolean} true|false |
|
254 | * @param [options.allow_stdin=false] {Boolean} true|false | |
253 | * |
|
255 | * | |
254 | * @example |
|
256 | * @example | |
255 | * |
|
257 | * | |
256 | * The options object should contain the options for the execute call. Its default |
|
258 | * The options object should contain the options for the execute call. Its default | |
257 | * values are: |
|
259 | * values are: | |
258 | * |
|
260 | * | |
259 | * options = { |
|
261 | * options = { | |
260 | * silent : true, |
|
262 | * silent : true, | |
261 | * user_variables : [], |
|
263 | * user_variables : [], | |
262 | * user_expressions : {}, |
|
264 | * user_expressions : {}, | |
263 | * allow_stdin : false |
|
265 | * allow_stdin : false | |
264 | * } |
|
266 | * } | |
265 | * |
|
267 | * | |
266 | * When calling this method pass a callbacks structure of the form: |
|
268 | * When calling this method pass a callbacks structure of the form: | |
267 | * |
|
269 | * | |
268 | * callbacks = { |
|
270 | * callbacks = { | |
269 | * 'execute_reply': execute_reply_callback, |
|
271 | * 'execute_reply': execute_reply_callback, | |
270 | * 'output': output_callback, |
|
272 | * 'output': output_callback, | |
271 | * 'clear_output': clear_output_callback, |
|
273 | * 'clear_output': clear_output_callback, | |
272 | * 'set_next_input': set_next_input_callback |
|
274 | * 'set_next_input': set_next_input_callback | |
273 | * } |
|
275 | * } | |
274 | * |
|
276 | * | |
275 | * The `execute_reply_callback` will be passed the content and metadata |
|
277 | * The `execute_reply_callback` will be passed the content and metadata | |
276 | * objects of the `execute_reply` message documented |
|
278 | * objects of the `execute_reply` message documented | |
277 | * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#execute) |
|
279 | * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#execute) | |
278 | * |
|
280 | * | |
279 | * The `output_callback` will be passed `msg_type` ('stream','display_data','pyout','pyerr') |
|
281 | * The `output_callback` will be passed `msg_type` ('stream','display_data','pyout','pyerr') | |
280 | * of the output and the content and metadata objects of the PUB/SUB channel that contains the |
|
282 | * of the output and the content and metadata objects of the PUB/SUB channel that contains the | |
281 | * output: |
|
283 | * output: | |
282 | * |
|
284 | * | |
283 | * http://ipython.org/ipython-doc/dev/development/messaging.html#messages-on-the-pub-sub-socket |
|
285 | * http://ipython.org/ipython-doc/dev/development/messaging.html#messages-on-the-pub-sub-socket | |
284 | * |
|
286 | * | |
285 | * The `clear_output_callback` will be passed a content object that contains |
|
287 | * The `clear_output_callback` will be passed a content object that contains | |
286 | * stdout, stderr and other fields that are booleans, as well as the metadata object. |
|
288 | * stdout, stderr and other fields that are booleans, as well as the metadata object. | |
287 | * |
|
289 | * | |
288 | * The `set_next_input_callback` will be passed the text that should become the next |
|
290 | * The `set_next_input_callback` will be passed the text that should become the next | |
289 | * input cell. |
|
291 | * input cell. | |
290 | */ |
|
292 | */ | |
291 | Kernel.prototype.execute = function (code, callbacks, options) { |
|
293 | Kernel.prototype.execute = function (code, callbacks, options) { | |
292 |
|
294 | |||
293 | var content = { |
|
295 | var content = { | |
294 | code : code, |
|
296 | code : code, | |
295 | silent : true, |
|
297 | silent : true, | |
296 | user_variables : [], |
|
298 | user_variables : [], | |
297 | user_expressions : {}, |
|
299 | user_expressions : {}, | |
298 | allow_stdin : false |
|
300 | allow_stdin : false | |
299 | }; |
|
301 | }; | |
300 | callbacks = callbacks || {}; |
|
302 | callbacks = callbacks || {}; | |
301 | if (callbacks.input_request !== undefined) { |
|
303 | if (callbacks.input_request !== undefined) { | |
302 | content.allow_stdin = true; |
|
304 | content.allow_stdin = true; | |
303 | } |
|
305 | } | |
304 | $.extend(true, content, options) |
|
306 | $.extend(true, content, options) | |
305 | $([IPython.events]).trigger('execution_request.Kernel', {kernel: this, content:content}); |
|
307 | $([IPython.events]).trigger('execution_request.Kernel', {kernel: this, content:content}); | |
306 | var msg = this._get_msg("execute_request", content); |
|
308 | var msg = this._get_msg("execute_request", content); | |
307 | this.shell_channel.send(JSON.stringify(msg)); |
|
309 | this.shell_channel.send(JSON.stringify(msg)); | |
308 | this.set_callbacks_for_msg(msg.header.msg_id, callbacks); |
|
310 | this.set_callbacks_for_msg(msg.header.msg_id, callbacks); | |
309 | return msg.header.msg_id; |
|
311 | return msg.header.msg_id; | |
310 | }; |
|
312 | }; | |
311 |
|
313 | |||
312 | /** |
|
314 | /** | |
313 | * When calling this method pass a callbacks structure of the form: |
|
315 | * When calling this method pass a callbacks structure of the form: | |
314 | * |
|
316 | * | |
315 | * callbacks = { |
|
317 | * callbacks = { | |
316 | * 'complete_reply': complete_reply_callback |
|
318 | * 'complete_reply': complete_reply_callback | |
317 | * } |
|
319 | * } | |
318 | * |
|
320 | * | |
319 | * The `complete_reply_callback` will be passed the content object of the |
|
321 | * The `complete_reply_callback` will be passed the content object of the | |
320 | * `complete_reply` message documented |
|
322 | * `complete_reply` message documented | |
321 | * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete) |
|
323 | * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete) | |
322 | * |
|
324 | * | |
323 | * @method complete |
|
325 | * @method complete | |
324 | * @param line {integer} |
|
326 | * @param line {integer} | |
325 | * @param cursor_pos {integer} |
|
327 | * @param cursor_pos {integer} | |
326 | * @param {dict} callbacks |
|
328 | * @param {dict} callbacks | |
327 | * @param callbacks.complete_reply {function} `complete_reply_callback` |
|
329 | * @param callbacks.complete_reply {function} `complete_reply_callback` | |
328 | * |
|
330 | * | |
329 | */ |
|
331 | */ | |
330 | Kernel.prototype.complete = function (line, cursor_pos, callbacks) { |
|
332 | Kernel.prototype.complete = function (line, cursor_pos, callbacks) { | |
331 | callbacks = callbacks || {}; |
|
333 | callbacks = callbacks || {}; | |
332 | var content = { |
|
334 | var content = { | |
333 | text : '', |
|
335 | text : '', | |
334 | line : line, |
|
336 | line : line, | |
335 | cursor_pos : cursor_pos |
|
337 | cursor_pos : cursor_pos | |
336 | }; |
|
338 | }; | |
337 | var msg = this._get_msg("complete_request", content); |
|
339 | var msg = this._get_msg("complete_request", content); | |
338 | this.shell_channel.send(JSON.stringify(msg)); |
|
340 | this.shell_channel.send(JSON.stringify(msg)); | |
339 | this.set_callbacks_for_msg(msg.header.msg_id, callbacks); |
|
341 | this.set_callbacks_for_msg(msg.header.msg_id, callbacks); | |
340 | return msg.header.msg_id; |
|
342 | return msg.header.msg_id; | |
341 | }; |
|
343 | }; | |
342 |
|
344 | |||
343 |
|
345 | |||
344 | Kernel.prototype.interrupt = function () { |
|
346 | Kernel.prototype.interrupt = function () { | |
345 | if (this.running) { |
|
347 | if (this.running) { | |
346 | $([IPython.events]).trigger('status_interrupting.Kernel', {kernel: this}); |
|
348 | $([IPython.events]).trigger('status_interrupting.Kernel', {kernel: this}); | |
347 | $.post(this.kernel_url + "/interrupt"); |
|
349 | $.post(this.kernel_url + "/interrupt"); | |
348 | }; |
|
350 | }; | |
349 | }; |
|
351 | }; | |
350 |
|
352 | |||
351 |
|
353 | |||
352 | Kernel.prototype.kill = function () { |
|
354 | Kernel.prototype.kill = function () { | |
353 | if (this.running) { |
|
355 | if (this.running) { | |
354 | this.running = false; |
|
356 | this.running = false; | |
355 | var settings = { |
|
357 | var settings = { | |
356 | cache : false, |
|
358 | cache : false, | |
357 | type : "DELETE" |
|
359 | type : "DELETE" | |
358 | }; |
|
360 | }; | |
359 | $.ajax(this.kernel_url, settings); |
|
361 | $.ajax(this.kernel_url, settings); | |
360 | }; |
|
362 | }; | |
361 | }; |
|
363 | }; | |
362 |
|
364 | |||
363 | Kernel.prototype.send_input_reply = function (input) { |
|
365 | Kernel.prototype.send_input_reply = function (input) { | |
364 | var content = { |
|
366 | var content = { | |
365 | value : input, |
|
367 | value : input, | |
366 | }; |
|
368 | }; | |
367 | $([IPython.events]).trigger('input_reply.Kernel', {kernel: this, content:content}); |
|
369 | $([IPython.events]).trigger('input_reply.Kernel', {kernel: this, content:content}); | |
368 | var msg = this._get_msg("input_reply", content); |
|
370 | var msg = this._get_msg("input_reply", content); | |
369 | this.stdin_channel.send(JSON.stringify(msg)); |
|
371 | this.stdin_channel.send(JSON.stringify(msg)); | |
370 | return msg.header.msg_id; |
|
372 | return msg.header.msg_id; | |
371 | }; |
|
373 | }; | |
372 |
|
374 | |||
373 |
|
375 | |||
374 | // Reply handlers |
|
376 | // Reply handlers | |
375 |
|
377 | |||
376 | Kernel.prototype.get_callbacks_for_msg = function (msg_id) { |
|
378 | Kernel.prototype.get_callbacks_for_msg = function (msg_id) { | |
377 | var callbacks = this._msg_callbacks[msg_id]; |
|
379 | var callbacks = this._msg_callbacks[msg_id]; | |
378 | return callbacks; |
|
380 | return callbacks; | |
379 | }; |
|
381 | }; | |
380 |
|
382 | |||
381 |
|
383 | |||
382 | Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) { |
|
384 | Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) { | |
383 | this._msg_callbacks[msg_id] = callbacks || {}; |
|
385 | this._msg_callbacks[msg_id] = callbacks || {}; | |
384 | } |
|
386 | } | |
385 |
|
387 | |||
386 |
|
388 | |||
387 | Kernel.prototype._handle_shell_reply = function (e) { |
|
389 | Kernel.prototype._handle_shell_reply = function (e) { | |
388 | var reply = $.parseJSON(e.data); |
|
390 | var reply = $.parseJSON(e.data); | |
389 | $([IPython.events]).trigger('shell_reply.Kernel', {kernel: this, reply:reply}); |
|
391 | $([IPython.events]).trigger('shell_reply.Kernel', {kernel: this, reply:reply}); | |
390 | var header = reply.header; |
|
392 | var header = reply.header; | |
391 | var content = reply.content; |
|
393 | var content = reply.content; | |
392 | var metadata = reply.metadata; |
|
394 | var metadata = reply.metadata; | |
393 | var msg_type = header.msg_type; |
|
395 | var msg_type = header.msg_type; | |
394 | var callbacks = this.get_callbacks_for_msg(reply.parent_header.msg_id); |
|
396 | var callbacks = this.get_callbacks_for_msg(reply.parent_header.msg_id); | |
395 | if (callbacks !== undefined) { |
|
397 | if (callbacks !== undefined) { | |
396 | var cb = callbacks[msg_type]; |
|
398 | var cb = callbacks[msg_type]; | |
397 | if (cb !== undefined) { |
|
399 | if (cb !== undefined) { | |
398 | cb(content, metadata); |
|
400 | cb(content, metadata); | |
399 | } |
|
401 | } | |
400 | }; |
|
402 | }; | |
401 |
|
403 | |||
402 | if (content.payload !== undefined) { |
|
404 | if (content.payload !== undefined) { | |
403 | var payload = content.payload || []; |
|
405 | var payload = content.payload || []; | |
404 | this._handle_payload(callbacks, payload); |
|
406 | this._handle_payload(callbacks, payload); | |
405 | } |
|
407 | } | |
406 | }; |
|
408 | }; | |
407 |
|
409 | |||
408 |
|
410 | |||
409 | Kernel.prototype._handle_payload = function (callbacks, payload) { |
|
411 | Kernel.prototype._handle_payload = function (callbacks, payload) { | |
410 | var l = payload.length; |
|
412 | var l = payload.length; | |
411 | // Payloads are handled by triggering events because we don't want the Kernel |
|
413 | // Payloads are handled by triggering events because we don't want the Kernel | |
412 | // to depend on the Notebook or Pager classes. |
|
414 | // to depend on the Notebook or Pager classes. | |
413 | for (var i=0; i<l; i++) { |
|
415 | for (var i=0; i<l; i++) { | |
414 | if (payload[i].source === 'IPython.kernel.zmq.page.page') { |
|
416 | if (payload[i].source === 'IPython.kernel.zmq.page.page') { | |
415 | var data = {'text':payload[i].text} |
|
417 | var data = {'text':payload[i].text} | |
416 | $([IPython.events]).trigger('open_with_text.Pager', data); |
|
418 | $([IPython.events]).trigger('open_with_text.Pager', data); | |
417 | } else if (payload[i].source === 'IPython.kernel.zmq.zmqshell.ZMQInteractiveShell.set_next_input') { |
|
419 | } else if (payload[i].source === 'IPython.kernel.zmq.zmqshell.ZMQInteractiveShell.set_next_input') { | |
418 | if (callbacks.set_next_input !== undefined) { |
|
420 | if (callbacks.set_next_input !== undefined) { | |
419 | callbacks.set_next_input(payload[i].text) |
|
421 | callbacks.set_next_input(payload[i].text) | |
420 | } |
|
422 | } | |
421 | } |
|
423 | } | |
422 | }; |
|
424 | }; | |
423 | }; |
|
425 | }; | |
424 |
|
426 | |||
425 |
|
427 | |||
426 | Kernel.prototype._handle_iopub_reply = function (e) { |
|
428 | Kernel.prototype._handle_iopub_reply = function (e) { | |
427 | var reply = $.parseJSON(e.data); |
|
429 | var reply = $.parseJSON(e.data); | |
428 | var content = reply.content; |
|
430 | var content = reply.content; | |
429 | var msg_type = reply.header.msg_type; |
|
431 | var msg_type = reply.header.msg_type; | |
430 | var metadata = reply.metadata; |
|
432 | var metadata = reply.metadata; | |
431 | var callbacks = this.get_callbacks_for_msg(reply.parent_header.msg_id); |
|
433 | var callbacks = this.get_callbacks_for_msg(reply.parent_header.msg_id); | |
432 | if (msg_type !== 'status' && callbacks === undefined) { |
|
434 | if (msg_type !== 'status' && callbacks === undefined) { | |
433 | // Message not from one of this notebook's cells and there are no |
|
435 | // Message not from one of this notebook's cells and there are no | |
434 | // callbacks to handle it. |
|
436 | // callbacks to handle it. | |
435 | return; |
|
437 | return; | |
436 | } |
|
438 | } | |
437 | var output_types = ['stream','display_data','pyout','pyerr']; |
|
439 | var output_types = ['stream','display_data','pyout','pyerr']; | |
438 | if (output_types.indexOf(msg_type) >= 0) { |
|
440 | if (output_types.indexOf(msg_type) >= 0) { | |
439 | var cb = callbacks['output']; |
|
441 | var cb = callbacks['output']; | |
440 | if (cb !== undefined) { |
|
442 | if (cb !== undefined) { | |
441 | cb(msg_type, content, metadata); |
|
443 | cb(msg_type, content, metadata); | |
442 | } |
|
444 | } | |
443 | } else if (msg_type === 'status') { |
|
445 | } else if (msg_type === 'status') { | |
444 | if (content.execution_state === 'busy') { |
|
446 | if (content.execution_state === 'busy') { | |
445 | $([IPython.events]).trigger('status_busy.Kernel', {kernel: this}); |
|
447 | $([IPython.events]).trigger('status_busy.Kernel', {kernel: this}); | |
446 | } else if (content.execution_state === 'idle') { |
|
448 | } else if (content.execution_state === 'idle') { | |
447 | $([IPython.events]).trigger('status_idle.Kernel', {kernel: this}); |
|
449 | $([IPython.events]).trigger('status_idle.Kernel', {kernel: this}); | |
448 | } else if (content.execution_state === 'restarting') { |
|
450 | } else if (content.execution_state === 'restarting') { | |
449 | $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this}); |
|
451 | $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this}); | |
450 | } else if (content.execution_state === 'dead') { |
|
452 | } else if (content.execution_state === 'dead') { | |
451 | this.stop_channels(); |
|
453 | this.stop_channels(); | |
452 | $([IPython.events]).trigger('status_dead.Kernel', {kernel: this}); |
|
454 | $([IPython.events]).trigger('status_dead.Kernel', {kernel: this}); | |
453 | }; |
|
455 | }; | |
454 | } else if (msg_type === 'clear_output') { |
|
456 | } else if (msg_type === 'clear_output') { | |
455 | var cb = callbacks['clear_output']; |
|
457 | var cb = callbacks['clear_output']; | |
456 | if (cb !== undefined) { |
|
458 | if (cb !== undefined) { | |
457 | cb(content, metadata); |
|
459 | cb(content, metadata); | |
458 | } |
|
460 | } | |
459 | }; |
|
461 | }; | |
460 | }; |
|
462 | }; | |
461 |
|
463 | |||
462 |
|
464 | |||
463 | Kernel.prototype._handle_input_request = function (e) { |
|
465 | Kernel.prototype._handle_input_request = function (e) { | |
464 | var request = $.parseJSON(e.data); |
|
466 | var request = $.parseJSON(e.data); | |
465 | var header = request.header; |
|
467 | var header = request.header; | |
466 | var content = request.content; |
|
468 | var content = request.content; | |
467 | var metadata = request.metadata; |
|
469 | var metadata = request.metadata; | |
468 | var msg_type = header.msg_type; |
|
470 | var msg_type = header.msg_type; | |
469 | if (msg_type !== 'input_request') { |
|
471 | if (msg_type !== 'input_request') { | |
470 | console.log("Invalid input request!", request); |
|
472 | console.log("Invalid input request!", request); | |
471 | return; |
|
473 | return; | |
472 | } |
|
474 | } | |
473 | var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id); |
|
475 | var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id); | |
474 | if (callbacks !== undefined) { |
|
476 | if (callbacks !== undefined) { | |
475 | var cb = callbacks[msg_type]; |
|
477 | var cb = callbacks[msg_type]; | |
476 | if (cb !== undefined) { |
|
478 | if (cb !== undefined) { | |
477 | cb(content, metadata); |
|
479 | cb(content, metadata); | |
478 | } |
|
480 | } | |
479 | }; |
|
481 | }; | |
480 | }; |
|
482 | }; | |
481 |
|
483 | |||
482 |
|
484 | |||
483 | IPython.Kernel = Kernel; |
|
485 | IPython.Kernel = Kernel; | |
484 |
|
486 | |||
485 | return IPython; |
|
487 | return IPython; | |
486 |
|
488 | |||
487 | }(IPython)); |
|
489 | }(IPython)); | |
488 |
|
490 |
General Comments 0
You need to be logged in to leave comments.
Login now