Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50650 views
1
// Copyright (c) IPython Development Team.
2
// Distributed under the terms of the Modified BSD License.
3
4
define([
5
'base/js/namespace',
6
'jquery',
7
'base/js/utils',
8
'services/kernels/kernel',
9
], function(IPython, $, utils, kernel) {
10
"use strict";
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_path: the path (not including name) to the notebook
19
* - kernel_name: the type of kernel (e.g. python3)
20
* - base_url: the root url of the notebook server
21
* - ws_url: the url to access websockets
22
* - notebook: Notebook object
23
*
24
* @class Session
25
* @param {Object} options
26
*/
27
var Session = function (options) {
28
this.id = null;
29
this.notebook_model = {
30
path: options.notebook_path
31
};
32
this.kernel_model = {
33
id: null,
34
name: options.kernel_name
35
};
36
37
this.base_url = options.base_url;
38
this.ws_url = options.ws_url;
39
this.session_service_url = utils.url_join_encode(this.base_url, 'api/sessions');
40
this.session_url = null;
41
42
this.notebook = options.notebook;
43
this.kernel = null;
44
this.events = options.notebook.events;
45
46
this.bind_events();
47
};
48
49
Session.prototype.bind_events = function () {
50
var that = this;
51
var record_status = function (evt, info) {
52
console.log('Session: ' + evt.type + ' (' + info.session.id + ')');
53
};
54
55
this.events.on('kernel_created.Session', record_status);
56
this.events.on('kernel_dead.Session', record_status);
57
this.events.on('kernel_killed.Session', record_status);
58
59
// if the kernel dies, then also remove the session
60
this.events.on('kernel_dead.Kernel', function () {
61
that.delete();
62
});
63
};
64
65
66
// Public REST api functions
67
68
/**
69
* GET /api/sessions
70
*
71
* Get a list of the current sessions.
72
*
73
* @function list
74
* @param {function} [success] - function executed on ajax success
75
* @param {function} [error] - functon executed on ajax error
76
*/
77
Session.prototype.list = function (success, error) {
78
$.ajax(this.session_service_url, {
79
processData: false,
80
cache: false,
81
type: "GET",
82
dataType: "json",
83
success: success,
84
error: this._on_error(error)
85
});
86
};
87
88
/**
89
* POST /api/sessions
90
*
91
* Start a new session. This function can only executed once.
92
*
93
* @function start
94
* @param {function} [success] - function executed on ajax success
95
* @param {function} [error] - functon executed on ajax error
96
*/
97
Session.prototype.start = function (success, error) {
98
var that = this;
99
var on_success = function (data, status, xhr) {
100
if (that.kernel) {
101
that.kernel.name = that.kernel_model.name;
102
} else {
103
var kernel_service_url = utils.url_path_join(that.base_url, "api/kernels");
104
that.kernel = new kernel.Kernel(kernel_service_url, that.ws_url, that.notebook, that.kernel_model.name);
105
}
106
that.events.trigger('kernel_created.Session', {session: that, kernel: that.kernel});
107
that.kernel._kernel_created(data.kernel);
108
if (success) {
109
success(data, status, xhr);
110
}
111
};
112
var on_error = function (xhr, status, err) {
113
that.events.trigger('kernel_dead.Session', {session: that, xhr: xhr, status: status, error: err});
114
if (error) {
115
error(xhr, status, err);
116
}
117
};
118
119
$.ajax(this.session_service_url, {
120
processData: false,
121
cache: false,
122
type: "POST",
123
data: JSON.stringify(this._get_model()),
124
dataType: "json",
125
success: this._on_success(on_success),
126
error: this._on_error(on_error)
127
});
128
};
129
130
/**
131
* GET /api/sessions/[:session_id]
132
*
133
* Get information about a session.
134
*
135
* @function get_info
136
* @param {function} [success] - function executed on ajax success
137
* @param {function} [error] - functon executed on ajax error
138
*/
139
Session.prototype.get_info = function (success, error) {
140
$.ajax(this.session_url, {
141
processData: false,
142
cache: false,
143
type: "GET",
144
dataType: "json",
145
success: this._on_success(success),
146
error: this._on_error(error)
147
});
148
};
149
150
/**
151
* PATCH /api/sessions/[:session_id]
152
*
153
* Rename or move a notebook. If the given name or path are
154
* undefined, then they will not be changed.
155
*
156
* @function rename_notebook
157
* @param {string} [path] - new notebook path
158
* @param {function} [success] - function executed on ajax success
159
* @param {function} [error] - functon executed on ajax error
160
*/
161
Session.prototype.rename_notebook = function (path, success, error) {
162
if (path !== undefined) {
163
this.notebook_model.path = path;
164
}
165
166
$.ajax(this.session_url, {
167
processData: false,
168
cache: false,
169
type: "PATCH",
170
data: JSON.stringify(this._get_model()),
171
dataType: "json",
172
success: this._on_success(success),
173
error: this._on_error(error)
174
});
175
};
176
177
/**
178
* DELETE /api/sessions/[:session_id]
179
*
180
* Kill the kernel and shutdown the session.
181
*
182
* @function delete
183
* @param {function} [success] - function executed on ajax success
184
* @param {function} [error] - functon executed on ajax error
185
*/
186
Session.prototype.delete = function (success, error) {
187
if (this.kernel) {
188
this.events.trigger('kernel_killed.Session', {session: this, kernel: this.kernel});
189
this.kernel._kernel_dead();
190
}
191
192
$.ajax(this.session_url, {
193
processData: false,
194
cache: false,
195
type: "DELETE",
196
dataType: "json",
197
success: this._on_success(success),
198
error: this._on_error(error)
199
});
200
};
201
202
/**
203
* Restart the session by deleting it and the starting it
204
* fresh. If options are given, they can include any of the
205
* following:
206
*
207
* - notebook_path - the path to the notebook
208
* - kernel_name - the name (type) of the kernel
209
*
210
* @function restart
211
* @param {Object} [options] - options for the new kernel
212
* @param {function} [success] - function executed on ajax success
213
* @param {function} [error] - functon executed on ajax error
214
*/
215
Session.prototype.restart = function (options, success, error) {
216
var that = this;
217
var start = function () {
218
if (options && options.notebook_path) {
219
that.notebook_model.path = options.notebook_path;
220
}
221
if (options && options.kernel_name) {
222
that.kernel_model.name = options.kernel_name;
223
}
224
that.kernel_model.id = null;
225
that.start(success, error);
226
};
227
this.delete(start, start);
228
};
229
230
// Helper functions
231
232
/**
233
* Get the data model for the session, which includes the notebook path
234
* and kernel (name and id).
235
*
236
* @function _get_model
237
* @returns {Object} - the data model
238
*/
239
Session.prototype._get_model = function () {
240
return {
241
notebook: this.notebook_model,
242
kernel: this.kernel_model
243
};
244
};
245
246
/**
247
* Update the data model from the given JSON object, which should
248
* have attributes of `id`, `notebook`, and/or `kernel`. If
249
* provided, the notebook data must include name and path, and the
250
* kernel data must include name and id.
251
*
252
* @function _update_model
253
* @param {Object} data - updated data model
254
*/
255
Session.prototype._update_model = function (data) {
256
if (data && data.id) {
257
this.id = data.id;
258
this.session_url = utils.url_join_encode(this.session_service_url, this.id);
259
}
260
if (data && data.notebook) {
261
this.notebook_model.path = data.notebook.path;
262
}
263
if (data && data.kernel) {
264
this.kernel_model.name = data.kernel.name;
265
this.kernel_model.id = data.kernel.id;
266
}
267
};
268
269
/**
270
* Handle a successful AJAX request by updating the session data
271
* model with the response, and then optionally calling a provided
272
* callback.
273
*
274
* @function _on_success
275
* @param {function} success - callback
276
*/
277
Session.prototype._on_success = function (success) {
278
var that = this;
279
return function (data, status, xhr) {
280
that._update_model(data);
281
if (success) {
282
success(data, status, xhr);
283
}
284
};
285
};
286
287
/**
288
* Handle a failed AJAX request by logging the error message, and
289
* then optionally calling a provided callback.
290
*
291
* @function _on_error
292
* @param {function} error - callback
293
*/
294
Session.prototype._on_error = function (error) {
295
return function (xhr, status, err) {
296
utils.log_ajax_error(xhr, status, err);
297
if (error) {
298
error(xhr, status, err);
299
}
300
};
301
};
302
303
/**
304
* Error type indicating that the session is already starting.
305
*/
306
var SessionAlreadyStarting = function (message) {
307
this.name = "SessionAlreadyStarting";
308
this.message = (message || "");
309
};
310
SessionAlreadyStarting.prototype = Error.prototype;
311
312
// For backwards compatability.
313
IPython.Session = Session;
314
315
return {
316
Session: Session,
317
SessionAlreadyStarting: SessionAlreadyStarting
318
};
319
});
320
321