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