Show More
@@ -1,203 +1,279 | |||
|
1 | 1 | // Copyright (c) IPython Development Team. |
|
2 | 2 | // Distributed under the terms of the Modified BSD License. |
|
3 | 3 | |
|
4 | 4 | define([ |
|
5 | 5 | 'base/js/namespace', |
|
6 | 6 | 'jquery', |
|
7 | 7 | 'base/js/utils', |
|
8 | 8 | 'services/kernels/js/kernel', |
|
9 | 9 | ], function(IPython, $, utils, kernel) { |
|
10 | 10 | "use strict"; |
|
11 | 11 | |
|
12 | /** | |
|
13 | * Session object for accessing the session REST api. The session | |
|
14 | * should be used to start kernels and then shut them down -- for | |
|
15 | * all other operations, the kernel object should be used. | |
|
16 | * | |
|
17 | * Options should include: | |
|
18 | * - notebook_name: the notebook name | |
|
19 | * - notebook_path: the path (not including name) to the notebook | |
|
20 | * - kernel_name: the type of kernel (e.g. python3) | |
|
21 | * - base_url: the root url of the notebook server | |
|
22 | * - ws_url: the url to access websockets | |
|
23 | * - notebook: Notebook object | |
|
24 | * | |
|
25 | * @class Session | |
|
26 | * @param {Object} options | |
|
27 | */ | |
|
12 | 28 | var Session = function (options) { |
|
13 | 29 | this.id = null; |
|
14 | 30 | this.notebook_model = { |
|
15 | 31 | name: options.notebook_name, |
|
16 | 32 | path: options.notebook_path |
|
17 | 33 | }; |
|
18 | 34 | this.kernel_model = { |
|
19 | 35 | id: null, |
|
20 | 36 | name: options.kernel_name |
|
21 | 37 | }; |
|
22 | 38 | |
|
23 | 39 | this.base_url = options.base_url; |
|
24 | 40 | this.ws_url = options.ws_url; |
|
25 | 41 | this.session_service_url = utils.url_join_encode(this.base_url, 'api/sessions'); |
|
26 | 42 | this.session_url = null; |
|
27 | 43 | |
|
28 | 44 | this.notebook = options.notebook; |
|
29 | 45 | this.kernel = null; |
|
30 | this.events = options.events; | |
|
46 | this.events = options.notebook.events; | |
|
31 | 47 | }; |
|
32 | 48 | |
|
49 | // Public REST api functions | |
|
50 | ||
|
33 | 51 | /** |
|
34 | 52 | * GET /api/sessions |
|
53 | * | |
|
54 | * Get a list of the current sessions. | |
|
55 | * | |
|
56 | * @function list | |
|
57 | * @param {function} [success] - function executed on ajax success | |
|
58 | * @param {function} [error] - functon executed on ajax error | |
|
35 | 59 | */ |
|
36 | 60 | Session.prototype.list = function (success, error) { |
|
37 | 61 | $.ajax(this.session_service_url, { |
|
38 | 62 | processData: false, |
|
39 | 63 | cache: false, |
|
40 | 64 | type: "GET", |
|
41 | 65 | dataType: "json", |
|
42 | 66 | success: success, |
|
43 | 67 | error: this._on_error(error) |
|
44 | 68 | }); |
|
45 | 69 | }; |
|
46 | 70 | |
|
47 | 71 | /** |
|
48 | 72 | * POST /api/sessions |
|
73 | * | |
|
74 | * Start a new session. This function can only executed once. | |
|
75 | * | |
|
76 | * @function start | |
|
77 | * @param {function} [success] - function executed on ajax success | |
|
78 | * @param {function} [error] - functon executed on ajax error | |
|
49 | 79 | */ |
|
50 | 80 | Session.prototype.start = function (success, error) { |
|
51 | 81 | if (this.kernel !== null) { |
|
52 | 82 | throw new Error("session has already been started"); |
|
53 | 83 | }; |
|
54 | 84 | |
|
55 | 85 | var that = this; |
|
56 | 86 | var on_success = function (data, status, xhr) { |
|
57 | 87 | var kernel_service_url = utils.url_path_join(that.base_url, "api/kernels"); |
|
58 | 88 | that.kernel = new kernel.Kernel( |
|
59 | 89 | kernel_service_url, that.ws_url, that.notebook, |
|
60 | 90 | that.kernel_model.id, that.kernel_model.name); |
|
61 | 91 | that.kernel._kernel_started(data.kernel); |
|
62 | 92 | if (success) { |
|
63 | 93 | success(data, status, xhr); |
|
64 | 94 | } |
|
65 | 95 | }; |
|
66 | 96 | var on_error = function (xhr, status, err) { |
|
67 | 97 | that.events.trigger('no_kernel.Kernel'); |
|
68 | 98 | if (error) { |
|
69 | 99 | error(xhr, status, err); |
|
70 | 100 | } |
|
71 | 101 | }; |
|
72 | 102 | |
|
73 | 103 | $.ajax(this.session_service_url, { |
|
74 | 104 | processData: false, |
|
75 | 105 | cache: false, |
|
76 | 106 | type: "POST", |
|
77 | 107 | data: JSON.stringify(this._get_model()), |
|
78 | 108 | dataType: "json", |
|
79 | 109 | success: this._on_success(on_success), |
|
80 | 110 | error: this._on_error(on_error) |
|
81 | 111 | }); |
|
82 | 112 | }; |
|
83 | 113 | |
|
84 | 114 | /** |
|
85 | 115 | * GET /api/sessions/[:session_id] |
|
116 | * | |
|
117 | * Get information about a session. | |
|
118 | * | |
|
119 | * @function get_info | |
|
120 | * @param {function} [success] - function executed on ajax success | |
|
121 | * @param {function} [error] - functon executed on ajax error | |
|
86 | 122 | */ |
|
87 | 123 | Session.prototype.get_info = function (success, error) { |
|
88 | 124 | $.ajax(this.session_url, { |
|
89 | 125 | processData: false, |
|
90 | 126 | cache: false, |
|
91 | 127 | type: "GET", |
|
92 | 128 | dataType: "json", |
|
93 | 129 | success: this._on_success(success), |
|
94 | 130 | error: this._on_error(error) |
|
95 | 131 | }); |
|
96 | 132 | }; |
|
97 | 133 | |
|
98 | 134 | /** |
|
99 | 135 | * PATCH /api/sessions/[:session_id] |
|
136 | * | |
|
137 | * Rename or move a notebook. If the given name or path are | |
|
138 | * undefined, then they will not be changed. | |
|
139 | * | |
|
140 | * @function rename_notebook | |
|
141 | * @param {string} [name] - new notebook name | |
|
142 | * @param {string} [path] - new path to notebook | |
|
143 | * @param {function} [success] - function executed on ajax success | |
|
144 | * @param {function} [error] - functon executed on ajax error | |
|
100 | 145 | */ |
|
101 |
Session.prototype. |
|
|
102 |
if ( |
|
|
103 |
this.notebook_model.name = |
|
|
104 | } | |
|
105 | if (notebook_path !== undefined) { | |
|
106 | this.notebook_model.path = notebook_path; | |
|
146 | Session.prototype.rename_notebook = function (name, path, success, error) { | |
|
147 | if (name !== undefined) { | |
|
148 | this.notebook_model.name = name; | |
|
107 | 149 | } |
|
108 |
if ( |
|
|
109 |
this. |
|
|
150 | if (path !== undefined) { | |
|
151 | this.notebook_model.path = path; | |
|
110 | 152 | } |
|
111 | 153 | |
|
112 | console.log(JSON.stringify(this._get_model())); | |
|
113 | ||
|
114 | 154 | $.ajax(this.session_url, { |
|
115 | 155 | processData: false, |
|
116 | 156 | cache: false, |
|
117 | 157 | type: "PATCH", |
|
118 | 158 | data: JSON.stringify(this._get_model()), |
|
119 | 159 | dataType: "json", |
|
120 | 160 | success: this._on_success(success), |
|
121 | 161 | error: this._on_error(error) |
|
122 | 162 | }); |
|
123 | 163 | }; |
|
124 | 164 | |
|
125 | Session.prototype.rename_notebook = function (name, path, success, error) { | |
|
126 | this.change(name, path, undefined, success, error); | |
|
127 | }; | |
|
128 | ||
|
129 | 165 | /** |
|
130 | 166 | * DELETE /api/sessions/[:session_id] |
|
167 | * | |
|
168 | * Kill the kernel and shutdown the session. | |
|
169 | * | |
|
170 | * @function delete | |
|
171 | * @param {function} [success] - function executed on ajax success | |
|
172 | * @param {function} [error] - functon executed on ajax error | |
|
131 | 173 | */ |
|
132 | 174 | Session.prototype.delete = function (success, error) { |
|
133 | 175 | if (this.kernel) { |
|
134 | 176 | this.kernel._kernel_dead(); |
|
135 | 177 | } |
|
136 | 178 | |
|
137 | 179 | $.ajax(this.session_url, { |
|
138 | 180 | processData: false, |
|
139 | 181 | cache: false, |
|
140 | 182 | type: "DELETE", |
|
141 | 183 | dataType: "json", |
|
142 | 184 | success: this._on_success(success), |
|
143 | 185 | error: this._on_error(error) |
|
144 | 186 | }); |
|
145 | 187 | }; |
|
146 | ||
|
188 | ||
|
189 | // Helper functions | |
|
190 | ||
|
191 | /** | |
|
192 | * Get the data model for the session, which includes the notebook | |
|
193 | * (name and path) and kernel (name and id). | |
|
194 | * | |
|
195 | * @function _get_model | |
|
196 | * @returns {Object} - the data model | |
|
197 | */ | |
|
147 | 198 | Session.prototype._get_model = function () { |
|
148 | 199 | return { |
|
149 | 200 | notebook: this.notebook_model, |
|
150 | 201 | kernel: this.kernel_model |
|
151 | 202 | }; |
|
152 | 203 | }; |
|
153 | 204 | |
|
205 | /** | |
|
206 | * Update the data model from the given JSON object, which should | |
|
207 | * have attributes of `id`, `notebook`, and/or `kernel`. If | |
|
208 | * provided, the notebook data must include name and path, and the | |
|
209 | * kernel data must include name and id. | |
|
210 | * | |
|
211 | * @function _update_model | |
|
212 | * @param {Object} data - updated data model | |
|
213 | */ | |
|
154 | 214 | Session.prototype._update_model = function (data) { |
|
155 | 215 | if (data && data.id) { |
|
156 | 216 | this.id = data.id; |
|
157 | 217 | this.session_url = utils.url_join_encode(this.session_service_url, this.id); |
|
158 | 218 | } |
|
159 | 219 | if (data && data.notebook) { |
|
160 | 220 | this.notebook_model.name = data.notebook.name; |
|
161 | 221 | this.notebook_model.path = data.notebook.path; |
|
162 | 222 | } |
|
163 | 223 | if (data && data.kernel) { |
|
164 | 224 | this.kernel_model.name = data.kernel.name; |
|
165 | 225 | this.kernel_model.id = data.kernel.id; |
|
166 | 226 | } |
|
167 | 227 | }; |
|
168 | 228 | |
|
229 | /** | |
|
230 | * Handle a successful AJAX request by updating the session data | |
|
231 | * model with the response, and then optionally calling a provided | |
|
232 | * callback. | |
|
233 | * | |
|
234 | * @function _on_success | |
|
235 | * @param {function} success - callback | |
|
236 | */ | |
|
169 | 237 | Session.prototype._on_success = function (success) { |
|
170 | 238 | var that = this; |
|
171 | 239 | return function (data, status, xhr) { |
|
172 | 240 | that._update_model(data); |
|
173 | 241 | if (success) { |
|
174 | 242 | success(data, status, xhr); |
|
175 | 243 | } |
|
176 | 244 | }; |
|
177 | 245 | }; |
|
178 | 246 | |
|
247 | /** | |
|
248 | * Handle a failed AJAX request by logging the error message, and | |
|
249 | * then optionally calling a provided callback. | |
|
250 | * | |
|
251 | * @function _on_error | |
|
252 | * @param {function} error - callback | |
|
253 | */ | |
|
179 | 254 | Session.prototype._on_error = function (error) { |
|
180 | 255 | return function (xhr, status, err) { |
|
181 | 256 | utils.log_ajax_error(xhr, status, err); |
|
182 | 257 | if (error) { |
|
183 | 258 | error(xhr, status, err); |
|
184 | 259 | } |
|
185 | 260 | }; |
|
186 | 261 | }; |
|
187 | 262 | |
|
188 | ||
|
263 | /** | |
|
264 | * Error type indicating that the session is already starting. | |
|
265 | */ | |
|
189 | 266 | var SessionAlreadyStarting = function (message) { |
|
190 | 267 | this.name = "SessionAlreadyStarting"; |
|
191 | 268 | this.message = (message || ""); |
|
192 | 269 | }; |
|
193 | ||
|
194 | 270 | SessionAlreadyStarting.prototype = Error.prototype; |
|
195 | 271 | |
|
196 | 272 | // For backwards compatability. |
|
197 | 273 | IPython.Session = Session; |
|
198 | 274 | |
|
199 | 275 | return { |
|
200 | 276 | Session: Session, |
|
201 | 277 | SessionAlreadyStarting: SessionAlreadyStarting |
|
202 | 278 | }; |
|
203 | 279 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now