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