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