Show More
@@ -9,6 +9,22 b' define([' | |||||
9 | ], function(IPython, $, utils, kernel) { |
|
9 | ], function(IPython, $, utils, kernel) { | |
10 | "use strict"; |
|
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 | var Session = function (options) { |
|
28 | var Session = function (options) { | |
13 | this.id = null; |
|
29 | this.id = null; | |
14 | this.notebook_model = { |
|
30 | this.notebook_model = { | |
@@ -27,11 +43,19 b' define([' | |||||
27 |
|
43 | |||
28 | this.notebook = options.notebook; |
|
44 | this.notebook = options.notebook; | |
29 | this.kernel = null; |
|
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 | * GET /api/sessions |
|
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 | Session.prototype.list = function (success, error) { |
|
60 | Session.prototype.list = function (success, error) { | |
37 | $.ajax(this.session_service_url, { |
|
61 | $.ajax(this.session_service_url, { | |
@@ -46,6 +70,12 b' define([' | |||||
46 |
|
70 | |||
47 | /** |
|
71 | /** | |
48 | * POST /api/sessions |
|
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 | Session.prototype.start = function (success, error) { |
|
80 | Session.prototype.start = function (success, error) { | |
51 | if (this.kernel !== null) { |
|
81 | if (this.kernel !== null) { | |
@@ -83,6 +113,12 b' define([' | |||||
83 |
|
113 | |||
84 | /** |
|
114 | /** | |
85 | * GET /api/sessions/[:session_id] |
|
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 | Session.prototype.get_info = function (success, error) { |
|
123 | Session.prototype.get_info = function (success, error) { | |
88 | $.ajax(this.session_url, { |
|
124 | $.ajax(this.session_url, { | |
@@ -97,20 +133,24 b' define([' | |||||
97 |
|
133 | |||
98 | /** |
|
134 | /** | |
99 | * PATCH /api/sessions/[:session_id] |
|
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. |
|
146 | Session.prototype.rename_notebook = function (name, path, success, error) { | |
102 |
if ( |
|
147 | if (name !== undefined) { | |
103 |
this.notebook_model.name = |
|
148 | this.notebook_model.name = name; | |
104 | } |
|
|||
105 | if (notebook_path !== undefined) { |
|
|||
106 | this.notebook_model.path = notebook_path; |
|
|||
107 | } |
|
149 | } | |
108 |
if ( |
|
150 | if (path !== undefined) { | |
109 |
this. |
|
151 | this.notebook_model.path = path; | |
110 | } |
|
152 | } | |
111 |
|
153 | |||
112 | console.log(JSON.stringify(this._get_model())); |
|
|||
113 |
|
||||
114 | $.ajax(this.session_url, { |
|
154 | $.ajax(this.session_url, { | |
115 | processData: false, |
|
155 | processData: false, | |
116 | cache: false, |
|
156 | cache: false, | |
@@ -122,12 +162,14 b' define([' | |||||
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 | * DELETE /api/sessions/[:session_id] |
|
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 | Session.prototype.delete = function (success, error) { |
|
174 | Session.prototype.delete = function (success, error) { | |
133 | if (this.kernel) { |
|
175 | if (this.kernel) { | |
@@ -143,7 +185,16 b' define([' | |||||
143 | error: this._on_error(error) |
|
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 | Session.prototype._get_model = function () { |
|
198 | Session.prototype._get_model = function () { | |
148 | return { |
|
199 | return { | |
149 | notebook: this.notebook_model, |
|
200 | notebook: this.notebook_model, | |
@@ -151,6 +202,15 b' define([' | |||||
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 | Session.prototype._update_model = function (data) { |
|
214 | Session.prototype._update_model = function (data) { | |
155 | if (data && data.id) { |
|
215 | if (data && data.id) { | |
156 | this.id = data.id; |
|
216 | this.id = data.id; | |
@@ -166,6 +226,14 b' define([' | |||||
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 | Session.prototype._on_success = function (success) { |
|
237 | Session.prototype._on_success = function (success) { | |
170 | var that = this; |
|
238 | var that = this; | |
171 | return function (data, status, xhr) { |
|
239 | return function (data, status, xhr) { | |
@@ -176,6 +244,13 b' define([' | |||||
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 | Session.prototype._on_error = function (error) { |
|
254 | Session.prototype._on_error = function (error) { | |
180 | return function (xhr, status, err) { |
|
255 | return function (xhr, status, err) { | |
181 | utils.log_ajax_error(xhr, status, err); |
|
256 | utils.log_ajax_error(xhr, status, err); | |
@@ -185,12 +260,13 b' define([' | |||||
185 | }; |
|
260 | }; | |
186 | }; |
|
261 | }; | |
187 |
|
262 | |||
188 |
|
263 | /** | ||
|
264 | * Error type indicating that the session is already starting. | |||
|
265 | */ | |||
189 | var SessionAlreadyStarting = function (message) { |
|
266 | var SessionAlreadyStarting = function (message) { | |
190 | this.name = "SessionAlreadyStarting"; |
|
267 | this.name = "SessionAlreadyStarting"; | |
191 | this.message = (message || ""); |
|
268 | this.message = (message || ""); | |
192 | }; |
|
269 | }; | |
193 |
|
||||
194 | SessionAlreadyStarting.prototype = Error.prototype; |
|
270 | SessionAlreadyStarting.prototype = Error.prototype; | |
195 |
|
271 | |||
196 | // For backwards compatability. |
|
272 | // For backwards compatability. |
General Comments 0
You need to be logged in to leave comments.
Login now