Show More
@@ -1,167 +1,167 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.base_url = "/kernels"; |
|
18 | this.base_url = "/kernels"; | |
19 | this.kernel_url = null; |
|
19 | this.kernel_url = null; | |
20 | this.shell_channel = null; |
|
20 | this.shell_channel = null; | |
21 | this.iopub_channel = null; |
|
21 | this.iopub_channel = null; | |
22 | this.running = false; |
|
22 | this.running = false; | |
23 |
|
23 | |||
24 | this.username = "username"; |
|
24 | this.username = "username"; | |
25 | this.session_id = utils.uuid(); |
|
25 | this.session_id = utils.uuid(); | |
26 |
|
26 | |||
27 | if (typeof(WebSocket) !== 'undefined') { |
|
27 | if (typeof(WebSocket) !== 'undefined') { | |
28 | this.WebSocket = WebSocket |
|
28 | this.WebSocket = WebSocket | |
29 | } else if (typeof(MozWebSocket) !== 'undefined') { |
|
29 | } else if (typeof(MozWebSocket) !== 'undefined') { | |
30 | this.WebSocket = MozWebSocket |
|
30 | this.WebSocket = MozWebSocket | |
31 | } else { |
|
31 | } else { | |
32 | 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.'); |
|
32 | 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.'); | |
33 | }; |
|
33 | }; | |
34 | }; |
|
34 | }; | |
35 |
|
35 | |||
36 |
|
36 | |||
37 | Kernel.prototype.get_msg = function (msg_type, content) { |
|
37 | Kernel.prototype.get_msg = function (msg_type, content) { | |
38 | var msg = { |
|
38 | var msg = { | |
39 | header : { |
|
39 | header : { | |
40 | msg_id : utils.uuid(), |
|
40 | msg_id : utils.uuid(), | |
41 | username : this.username, |
|
41 | username : this.username, | |
42 | session : this.session_id, |
|
42 | session : this.session_id, | |
43 | msg_type : msg_type |
|
43 | msg_type : msg_type | |
44 | }, |
|
44 | }, | |
45 | content : content, |
|
45 | content : content, | |
46 | parent_header : {} |
|
46 | parent_header : {} | |
47 | }; |
|
47 | }; | |
48 | return msg; |
|
48 | return msg; | |
49 | } |
|
49 | } | |
50 |
|
50 | |||
51 | Kernel.prototype.start = function (notebook_id, callback) { |
|
51 | Kernel.prototype.start = function (notebook_id, callback) { | |
52 | var that = this; |
|
52 | var that = this; | |
53 | if (!this.running) { |
|
53 | if (!this.running) { | |
54 | var qs = $.param({notebook:notebook_id}); |
|
54 | var qs = $.param({notebook:notebook_id}); | |
55 | $.post(this.base_url + '?' + qs, |
|
55 | $.post(this.base_url + '?' + qs, | |
56 | function (kernel_id) { |
|
56 | function (kernel_id) { | |
57 | that._handle_start_kernel(kernel_id, callback); |
|
57 | that._handle_start_kernel(kernel_id, callback); | |
58 | }, |
|
58 | }, | |
59 | 'json' |
|
59 | 'json' | |
60 | ); |
|
60 | ); | |
61 | }; |
|
61 | }; | |
62 | }; |
|
62 | }; | |
63 |
|
63 | |||
64 |
|
64 | |||
65 | Kernel.prototype.restart = function (callback) { |
|
65 | Kernel.prototype.restart = function (callback) { | |
66 | IPython.kernel_status_widget.status_restarting(); |
|
66 | IPython.kernel_status_widget.status_restarting(); | |
67 | var url = this.kernel_url + "/restart"; |
|
67 | var url = this.kernel_url + "/restart"; | |
68 | var that = this; |
|
68 | var that = this; | |
69 | if (this.running) { |
|
69 | if (this.running) { | |
70 | this.stop_channels(); |
|
70 | this.stop_channels(); | |
71 | $.post(url, |
|
71 | $.post(url, | |
72 | function (kernel_id) { |
|
72 | function (kernel_id) { | |
73 | that._handle_start_kernel(kernel_id, callback); |
|
73 | that._handle_start_kernel(kernel_id, callback); | |
74 | }, |
|
74 | }, | |
75 | 'json' |
|
75 | 'json' | |
76 | ); |
|
76 | ); | |
77 | }; |
|
77 | }; | |
78 | }; |
|
78 | }; | |
79 |
|
79 | |||
80 |
|
80 | |||
81 | Kernel.prototype._handle_start_kernel = function (json, callback) { |
|
81 | Kernel.prototype._handle_start_kernel = function (json, callback) { | |
82 | this.running = true; |
|
82 | this.running = true; | |
83 | this.kernel_id = json.kernel_id; |
|
83 | this.kernel_id = json.kernel_id; | |
84 | this.ws_url = json.ws_url; |
|
84 | this.ws_url = json.ws_url; | |
85 | this.kernel_url = this.base_url + "/" + this.kernel_id; |
|
85 | this.kernel_url = this.base_url + "/" + this.kernel_id; | |
86 | this.start_channels(); |
|
86 | this.start_channels(); | |
87 | callback(); |
|
87 | callback(); | |
88 | IPython.kernel_status_widget.status_idle(); |
|
88 | IPython.kernel_status_widget.status_idle(); | |
89 | }; |
|
89 | }; | |
90 |
|
90 | |||
91 |
|
91 | |||
92 | Kernel.prototype.start_channels = function () { |
|
92 | Kernel.prototype.start_channels = function () { | |
93 | this.stop_channels(); |
|
93 | this.stop_channels(); | |
94 | var ws_url = this.ws_url + this.kernel_url; |
|
94 | var ws_url = this.ws_url + this.kernel_url; | |
95 | console.log("Starting WS:", ws_url); |
|
95 | console.log("Starting WS:", ws_url); | |
96 | this.shell_channel = new this.WebSocket(ws_url + "/shell"); |
|
96 | this.shell_channel = new this.WebSocket(ws_url + "/shell"); | |
97 | this.iopub_channel = new this.WebSocket(ws_url + "/iopub"); |
|
97 | this.iopub_channel = new this.WebSocket(ws_url + "/iopub"); | |
98 | send_cookie = function(){ |
|
98 | send_cookie = function(){ | |
99 | this.send(document.cookie); |
|
99 | this.send(document.cookie); | |
100 | console.log(this); |
|
100 | console.log(this); | |
101 | } |
|
101 | } | |
102 | this.shell_channel.onopen = send_cookie; |
|
102 | this.shell_channel.onopen = send_cookie; | |
103 | this.iopub_channel.onopen = send_cookie; |
|
103 | this.iopub_channel.onopen = send_cookie; | |
104 | }; |
|
104 | }; | |
105 |
|
105 | |||
106 |
|
106 | |||
107 | Kernel.prototype.stop_channels = function () { |
|
107 | Kernel.prototype.stop_channels = function () { | |
108 | if (this.shell_channel !== null) { |
|
108 | if (this.shell_channel !== null) { | |
109 | this.shell_channel.close(); |
|
109 | this.shell_channel.close(); | |
110 | this.shell_channel = null; |
|
110 | this.shell_channel = null; | |
111 | }; |
|
111 | }; | |
112 | if (this.iopub_channel !== null) { |
|
112 | if (this.iopub_channel !== null) { | |
113 | this.iopub_channel.close(); |
|
113 | this.iopub_channel.close(); | |
114 | this.iopub_channel = null; |
|
114 | this.iopub_channel = null; | |
115 | }; |
|
115 | }; | |
116 | }; |
|
116 | }; | |
117 |
|
117 | |||
118 | Kernel.prototype.execute = function (code) { |
|
118 | Kernel.prototype.execute = function (code) { | |
119 | var content = { |
|
119 | var content = { | |
120 | code : code, |
|
120 | code : code, | |
121 | silent : false, |
|
121 | silent : false, | |
122 | user_variables : [], |
|
122 | user_variables : [], | |
123 | user_expressions : {} |
|
123 | user_expressions : {}, | |
124 | allow_stdin : false, |
|
124 | allow_stdin : false, | |
125 | }; |
|
125 | }; | |
126 | var msg = this.get_msg("execute_request", content); |
|
126 | var msg = this.get_msg("execute_request", content); | |
127 | this.shell_channel.send(JSON.stringify(msg)); |
|
127 | this.shell_channel.send(JSON.stringify(msg)); | |
128 | return msg.header.msg_id; |
|
128 | return msg.header.msg_id; | |
129 | } |
|
129 | } | |
130 |
|
130 | |||
131 |
|
131 | |||
132 | Kernel.prototype.complete = function (line, cursor_pos) { |
|
132 | Kernel.prototype.complete = function (line, cursor_pos) { | |
133 | var content = { |
|
133 | var content = { | |
134 | text : '', |
|
134 | text : '', | |
135 | line : line, |
|
135 | line : line, | |
136 | cursor_pos : cursor_pos |
|
136 | cursor_pos : cursor_pos | |
137 | }; |
|
137 | }; | |
138 | var msg = this.get_msg("complete_request", content); |
|
138 | var msg = this.get_msg("complete_request", content); | |
139 | this.shell_channel.send(JSON.stringify(msg)); |
|
139 | this.shell_channel.send(JSON.stringify(msg)); | |
140 | return msg.header.msg_id; |
|
140 | return msg.header.msg_id; | |
141 | } |
|
141 | } | |
142 |
|
142 | |||
143 |
|
143 | |||
144 | Kernel.prototype.interrupt = function () { |
|
144 | Kernel.prototype.interrupt = function () { | |
145 | if (this.running) { |
|
145 | if (this.running) { | |
146 | $.post(this.kernel_url + "/interrupt"); |
|
146 | $.post(this.kernel_url + "/interrupt"); | |
147 | }; |
|
147 | }; | |
148 | }; |
|
148 | }; | |
149 |
|
149 | |||
150 |
|
150 | |||
151 | Kernel.prototype.kill = function () { |
|
151 | Kernel.prototype.kill = function () { | |
152 | if (this.running) { |
|
152 | if (this.running) { | |
153 | this.running = false; |
|
153 | this.running = false; | |
154 | var settings = { |
|
154 | var settings = { | |
155 | cache : false, |
|
155 | cache : false, | |
156 | type : "DELETE", |
|
156 | type : "DELETE", | |
157 | }; |
|
157 | }; | |
158 | $.ajax(this.kernel_url, settings); |
|
158 | $.ajax(this.kernel_url, settings); | |
159 | }; |
|
159 | }; | |
160 | }; |
|
160 | }; | |
161 |
|
161 | |||
162 | IPython.Kernel = Kernel; |
|
162 | IPython.Kernel = Kernel; | |
163 |
|
163 | |||
164 | return IPython; |
|
164 | return IPython; | |
165 |
|
165 | |||
166 | }(IPython)); |
|
166 | }(IPython)); | |
167 |
|
167 |
General Comments 0
You need to be logged in to leave comments.
Login now