Show More
@@ -1,165 +1,199 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 | var IPython = (function (IPython) { |
|
12 | var IPython = (function (IPython) { | |
13 |
|
13 | |||
14 | var utils = IPython.utils; |
|
14 | var utils = IPython.utils; | |
15 |
|
15 | |||
16 | var Kernel = function () { |
|
16 | var Kernel = function () { | |
17 | this.kernel_id = null; |
|
17 | this.kernel_id = null; | |
18 | this.shell_channel = null; |
|
18 | this.shell_channel = null; | |
19 | this.iopub_channel = null; |
|
19 | this.iopub_channel = null; | |
20 | this.base_url = $('body').data('baseKernelUrl') + "kernels"; |
|
20 | this.base_url = $('body').data('baseKernelUrl') + "kernels"; | |
21 | this.running = false; |
|
21 | this.running = false; | |
22 | this.username = "username"; |
|
22 | this.username = "username"; | |
23 | this.session_id = utils.uuid(); |
|
23 | this.session_id = utils.uuid(); | |
24 |
|
24 | |||
25 | if (typeof(WebSocket) !== 'undefined') { |
|
25 | if (typeof(WebSocket) !== 'undefined') { | |
26 | this.WebSocket = WebSocket |
|
26 | this.WebSocket = WebSocket | |
27 | } else if (typeof(MozWebSocket) !== 'undefined') { |
|
27 | } else if (typeof(MozWebSocket) !== 'undefined') { | |
28 | this.WebSocket = MozWebSocket |
|
28 | this.WebSocket = MozWebSocket | |
29 | } else { |
|
29 | } else { | |
30 | 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.'); |
|
30 | 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.'); | |
31 | }; |
|
31 | }; | |
32 | }; |
|
32 | }; | |
33 |
|
33 | |||
34 |
|
34 | |||
35 | Kernel.prototype.get_msg = function (msg_type, content) { |
|
35 | Kernel.prototype.get_msg = function (msg_type, content) { | |
36 | var msg = { |
|
36 | var msg = { | |
37 | header : { |
|
37 | header : { | |
38 | msg_id : utils.uuid(), |
|
38 | msg_id : utils.uuid(), | |
39 | username : this.username, |
|
39 | username : this.username, | |
40 | session : this.session_id, |
|
40 | session : this.session_id, | |
41 | msg_type : msg_type |
|
41 | msg_type : msg_type | |
42 | }, |
|
42 | }, | |
43 | content : content, |
|
43 | content : content, | |
44 | parent_header : {} |
|
44 | parent_header : {} | |
45 | }; |
|
45 | }; | |
46 | return msg; |
|
46 | return msg; | |
47 | } |
|
47 | } | |
48 |
|
48 | |||
49 | Kernel.prototype.start = function (notebook_id, callback) { |
|
49 | Kernel.prototype.start = function (notebook_id, callback) { | |
50 | var that = this; |
|
50 | var that = this; | |
51 | if (!this.running) { |
|
51 | if (!this.running) { | |
52 | var qs = $.param({notebook:notebook_id}); |
|
52 | var qs = $.param({notebook:notebook_id}); | |
53 | var url = this.base_url + '?' + qs |
|
53 | var url = this.base_url + '?' + qs | |
54 | $.post(url, |
|
54 | $.post(url, | |
55 | function (kernel_id) { |
|
55 | function (kernel_id) { | |
56 | that._handle_start_kernel(kernel_id, callback); |
|
56 | that._handle_start_kernel(kernel_id, callback); | |
57 | }, |
|
57 | }, | |
58 | 'json' |
|
58 | 'json' | |
59 | ); |
|
59 | ); | |
60 | }; |
|
60 | }; | |
61 | }; |
|
61 | }; | |
62 |
|
62 | |||
63 |
|
63 | |||
64 | Kernel.prototype.restart = function (callback) { |
|
64 | Kernel.prototype.restart = function (callback) { | |
65 | IPython.kernel_status_widget.status_restarting(); |
|
65 | IPython.kernel_status_widget.status_restarting(); | |
66 | var url = this.kernel_url + "/restart"; |
|
66 | var url = this.kernel_url + "/restart"; | |
67 | var that = this; |
|
67 | var that = this; | |
68 | if (this.running) { |
|
68 | if (this.running) { | |
69 | this.stop_channels(); |
|
69 | this.stop_channels(); | |
70 | $.post(url, |
|
70 | $.post(url, | |
71 | function (kernel_id) { |
|
71 | function (kernel_id) { | |
72 | that._handle_start_kernel(kernel_id, callback); |
|
72 | that._handle_start_kernel(kernel_id, callback); | |
73 | }, |
|
73 | }, | |
74 | 'json' |
|
74 | 'json' | |
75 | ); |
|
75 | ); | |
76 | }; |
|
76 | }; | |
77 | }; |
|
77 | }; | |
78 |
|
78 | |||
79 |
|
79 | |||
80 | Kernel.prototype._handle_start_kernel = function (json, callback) { |
|
80 | Kernel.prototype._handle_start_kernel = function (json, callback) { | |
81 | this.running = true; |
|
81 | this.running = true; | |
82 | this.kernel_id = json.kernel_id; |
|
82 | this.kernel_id = json.kernel_id; | |
83 | this.ws_url = json.ws_url; |
|
83 | this.ws_url = json.ws_url; | |
84 | this.kernel_url = this.base_url + "/" + this.kernel_id; |
|
84 | this.kernel_url = this.base_url + "/" + this.kernel_id; | |
85 | this.start_channels(); |
|
85 | this.start_channels(); | |
86 | callback(); |
|
86 | callback(); | |
87 | IPython.kernel_status_widget.status_idle(); |
|
87 | IPython.kernel_status_widget.status_idle(); | |
88 | }; |
|
88 | }; | |
89 |
|
89 | |||
90 |
|
90 | |||
91 | Kernel.prototype.start_channels = function () { |
|
91 | Kernel.prototype.start_channels = function () { | |
|
92 | var that = this; | |||
92 | this.stop_channels(); |
|
93 | this.stop_channels(); | |
93 | var ws_url = this.ws_url + this.kernel_url; |
|
94 | var ws_url = this.ws_url + this.kernel_url; | |
94 | console.log("Starting WS:", ws_url); |
|
95 | console.log("Starting WS:", ws_url); | |
95 | this.shell_channel = new this.WebSocket(ws_url + "/shell"); |
|
96 | this.shell_channel = new this.WebSocket(ws_url + "/shell"); | |
96 | this.iopub_channel = new this.WebSocket(ws_url + "/iopub"); |
|
97 | this.iopub_channel = new this.WebSocket(ws_url + "/iopub"); | |
97 | send_cookie = function(){ |
|
98 | send_cookie = function(){ | |
98 | this.send(document.cookie); |
|
99 | this.send(document.cookie); | |
99 | } |
|
100 | } | |
|
101 | var already_called_onclose = false; // only alert once | |||
|
102 | ws_closed_early = function(evt){ | |||
|
103 | if (already_called_onclose){ | |||
|
104 | return; | |||
|
105 | } | |||
|
106 | already_called_onclose = true; | |||
|
107 | if ( ! evt.wasClean ){ | |||
|
108 | alert("Websocket connection to " + ws_url + " could not be established." + | |||
|
109 | " You will not be able to run code." + | |||
|
110 | " Your browser may not be compatible with the websocket version in the server," + | |||
|
111 | " or if the url does not look right, there could be an error in the" + | |||
|
112 | " server's configuration."); | |||
|
113 | } | |||
|
114 | } | |||
|
115 | ws_closed_late = function(evt){ | |||
|
116 | if (already_called_onclose){ | |||
|
117 | return; | |||
|
118 | } | |||
|
119 | already_called_onclose = true; | |||
|
120 | if ( ! evt.wasClean ){ | |||
|
121 | alert("Websocket connection has closed unexpectedly." + | |||
|
122 | " The kernel will no longer be responsive."); | |||
|
123 | } | |||
|
124 | } | |||
100 | this.shell_channel.onopen = send_cookie; |
|
125 | this.shell_channel.onopen = send_cookie; | |
|
126 | this.shell_channel.onclose = ws_closed_early; | |||
101 | this.iopub_channel.onopen = send_cookie; |
|
127 | this.iopub_channel.onopen = send_cookie; | |
|
128 | this.iopub_channel.onclose = ws_closed_early; | |||
|
129 | // switch from early-close to late-close message after 1s | |||
|
130 | setTimeout(function(){ | |||
|
131 | that.shell_channel.onclose = ws_closed_late; | |||
|
132 | that.iopub_channel.onclose = ws_closed_late; | |||
|
133 | }, 1000); | |||
102 | }; |
|
134 | }; | |
103 |
|
135 | |||
104 |
|
136 | |||
105 | Kernel.prototype.stop_channels = function () { |
|
137 | Kernel.prototype.stop_channels = function () { | |
106 | if (this.shell_channel !== null) { |
|
138 | if (this.shell_channel !== null) { | |
|
139 | this.shell_channel.onclose = function (evt) {null}; | |||
107 | this.shell_channel.close(); |
|
140 | this.shell_channel.close(); | |
108 | this.shell_channel = null; |
|
141 | this.shell_channel = null; | |
109 | }; |
|
142 | }; | |
110 | if (this.iopub_channel !== null) { |
|
143 | if (this.iopub_channel !== null) { | |
|
144 | this.iopub_channel.onclose = function (evt) {null}; | |||
111 | this.iopub_channel.close(); |
|
145 | this.iopub_channel.close(); | |
112 | this.iopub_channel = null; |
|
146 | this.iopub_channel = null; | |
113 | }; |
|
147 | }; | |
114 | }; |
|
148 | }; | |
115 |
|
149 | |||
116 | Kernel.prototype.execute = function (code) { |
|
150 | Kernel.prototype.execute = function (code) { | |
117 | var content = { |
|
151 | var content = { | |
118 | code : code, |
|
152 | code : code, | |
119 | silent : false, |
|
153 | silent : false, | |
120 | user_variables : [], |
|
154 | user_variables : [], | |
121 | user_expressions : {}, |
|
155 | user_expressions : {}, | |
122 | allow_stdin : false, |
|
156 | allow_stdin : false, | |
123 | }; |
|
157 | }; | |
124 | var msg = this.get_msg("execute_request", content); |
|
158 | var msg = this.get_msg("execute_request", content); | |
125 | this.shell_channel.send(JSON.stringify(msg)); |
|
159 | this.shell_channel.send(JSON.stringify(msg)); | |
126 | return msg.header.msg_id; |
|
160 | return msg.header.msg_id; | |
127 | } |
|
161 | } | |
128 |
|
162 | |||
129 |
|
163 | |||
130 | Kernel.prototype.complete = function (line, cursor_pos) { |
|
164 | Kernel.prototype.complete = function (line, cursor_pos) { | |
131 | var content = { |
|
165 | var content = { | |
132 | text : '', |
|
166 | text : '', | |
133 | line : line, |
|
167 | line : line, | |
134 | cursor_pos : cursor_pos |
|
168 | cursor_pos : cursor_pos | |
135 | }; |
|
169 | }; | |
136 | var msg = this.get_msg("complete_request", content); |
|
170 | var msg = this.get_msg("complete_request", content); | |
137 | this.shell_channel.send(JSON.stringify(msg)); |
|
171 | this.shell_channel.send(JSON.stringify(msg)); | |
138 | return msg.header.msg_id; |
|
172 | return msg.header.msg_id; | |
139 | } |
|
173 | } | |
140 |
|
174 | |||
141 |
|
175 | |||
142 | Kernel.prototype.interrupt = function () { |
|
176 | Kernel.prototype.interrupt = function () { | |
143 | if (this.running) { |
|
177 | if (this.running) { | |
144 | $.post(this.kernel_url + "/interrupt"); |
|
178 | $.post(this.kernel_url + "/interrupt"); | |
145 | }; |
|
179 | }; | |
146 | }; |
|
180 | }; | |
147 |
|
181 | |||
148 |
|
182 | |||
149 | Kernel.prototype.kill = function () { |
|
183 | Kernel.prototype.kill = function () { | |
150 | if (this.running) { |
|
184 | if (this.running) { | |
151 | this.running = false; |
|
185 | this.running = false; | |
152 | var settings = { |
|
186 | var settings = { | |
153 | cache : false, |
|
187 | cache : false, | |
154 | type : "DELETE", |
|
188 | type : "DELETE", | |
155 | }; |
|
189 | }; | |
156 | $.ajax(this.kernel_url, settings); |
|
190 | $.ajax(this.kernel_url, settings); | |
157 | }; |
|
191 | }; | |
158 | }; |
|
192 | }; | |
159 |
|
193 | |||
160 | IPython.Kernel = Kernel; |
|
194 | IPython.Kernel = Kernel; | |
161 |
|
195 | |||
162 | return IPython; |
|
196 | return IPython; | |
163 |
|
197 | |||
164 | }(IPython)); |
|
198 | }(IPython)); | |
165 |
|
199 |
General Comments 0
You need to be logged in to leave comments.
Login now