session.js
203 lines
| 5.7 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r17198 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
Zachary Sailer
|
r12987 | |||
Jonathan Frederic
|
r17198 | define([ | ||
'base/js/namespace', | ||||
Jonathan Frederic
|
r17200 | 'jquery', | ||
Jonathan Frederic
|
r17198 | 'base/js/utils', | ||
'services/kernels/js/kernel', | ||||
Jonathan Frederic
|
r17202 | ], function(IPython, $, utils, kernel) { | ||
MinRK
|
r13103 | "use strict"; | ||
Jonathan Frederic
|
r17198 | |||
Jessica B. Hamrick
|
r18198 | var Session = function (options) { | ||
Zachary Sailer
|
r13060 | this.id = null; | ||
Jessica B. Hamrick
|
r18200 | this.notebook_model = { | ||
Jessica B. Hamrick
|
r18198 | name: options.notebook_name, | ||
path: options.notebook_path | ||||
}; | ||||
Jessica B. Hamrick
|
r18200 | this.kernel_model = { | ||
id: null, | ||||
Jessica B. Hamrick
|
r18198 | name: options.kernel_name | ||
}; | ||||
Jonathan Frederic
|
r17212 | this.base_url = options.base_url; | ||
MinRK
|
r17308 | this.ws_url = options.ws_url; | ||
Jessica B. Hamrick
|
r18206 | this.session_service_url = utils.url_join_encode(this.base_url, 'api/sessions'); | ||
this.session_url = null; | ||||
Jessica B. Hamrick
|
r18198 | |||
Jessica B. Hamrick
|
r18200 | this.notebook = options.notebook; | ||
this.kernel = null; | ||||
Jessica B. Hamrick
|
r18198 | this.events = options.events; | ||
Zachary Sailer
|
r12987 | }; | ||
Jessica B. Hamrick
|
r18198 | |||
/** | ||||
* GET /api/sessions | ||||
*/ | ||||
Session.prototype.list = function (success, error) { | ||||
Jessica B. Hamrick
|
r18206 | $.ajax(this.session_service_url, { | ||
Jessica B. Hamrick
|
r18198 | processData: false, | ||
cache: false, | ||||
type: "GET", | ||||
dataType: "json", | ||||
success: success, | ||||
error: this._on_error(error) | ||||
}); | ||||
}; | ||||
/** | ||||
* POST /api/sessions | ||||
*/ | ||||
MinRK
|
r17649 | Session.prototype.start = function (success, error) { | ||
Jessica B. Hamrick
|
r18211 | if (this.kernel !== null) { | ||
throw new Error("session has already been started"); | ||||
}; | ||||
MinRK
|
r13103 | var that = this; | ||
Jessica B. Hamrick
|
r18198 | var on_success = function (data, status, xhr) { | ||
var kernel_service_url = utils.url_path_join(that.base_url, "api/kernels"); | ||||
Jessica B. Hamrick
|
r18200 | that.kernel = new kernel.Kernel( | ||
kernel_service_url, that.ws_url, that.notebook, | ||||
that.kernel_model.id, that.kernel_model.name); | ||||
Jessica B. Hamrick
|
r18198 | that.kernel._kernel_started(data.kernel); | ||
if (success) { | ||||
success(data, status, xhr); | ||||
MinRK
|
r13103 | } | ||
}; | ||||
Jessica B. Hamrick
|
r18198 | var on_error = function (xhr, status, err) { | ||
that.events.trigger('status_dead.Kernel'); | ||||
if (error) { | ||||
error(xhr, status, err); | ||||
MinRK
|
r17676 | } | ||
Zachary Sailer
|
r13057 | }; | ||
Jessica B. Hamrick
|
r18198 | |||
Jessica B. Hamrick
|
r18206 | $.ajax(this.session_service_url, { | ||
Jessica B. Hamrick
|
r18198 | processData: false, | ||
cache: false, | ||||
type: "POST", | ||||
data: JSON.stringify(this._get_model()), | ||||
dataType: "json", | ||||
success: this._on_success(on_success), | ||||
error: this._on_error(on_error) | ||||
}); | ||||
Zachary Sailer
|
r12987 | }; | ||
Jessica B. Hamrick
|
r18198 | |||
/** | ||||
* GET /api/sessions/[:session_id] | ||||
*/ | ||||
Session.prototype.get_info = function (success, error) { | ||||
Jessica B. Hamrick
|
r18206 | $.ajax(this.session_url, { | ||
Jessica B. Hamrick
|
r18198 | processData: false, | ||
cache: false, | ||||
type: "GET", | ||||
dataType: "json", | ||||
success: this._on_success(success), | ||||
error: this._on_error(error) | ||||
}); | ||||
MinRK
|
r13103 | }; | ||
Jessica B. Hamrick
|
r18198 | |||
/** | ||||
* PATCH /api/sessions/[:session_id] | ||||
*/ | ||||
Session.prototype.change = function (notebook_name, notebook_path, kernel_name, success, error) { | ||||
Jessica B. Hamrick
|
r18211 | if (notebook_name !== undefined) { | ||
this.notebook_model.name = notebook_name; | ||||
} | ||||
if (notebook_path !== undefined) { | ||||
this.notebook_model.path = notebook_path; | ||||
} | ||||
if (kernel_name !== undefined) { | ||||
this.kernel_model.name = kernel_name; | ||||
} | ||||
console.log(JSON.stringify(this._get_model())); | ||||
Jessica B. Hamrick
|
r18198 | |||
Jessica B. Hamrick
|
r18206 | $.ajax(this.session_url, { | ||
Jessica B. Hamrick
|
r18198 | processData: false, | ||
cache: false, | ||||
type: "PATCH", | ||||
data: JSON.stringify(this._get_model()), | ||||
Jessica B. Hamrick
|
r18211 | dataType: "json", | ||
Jessica B. Hamrick
|
r18198 | success: this._on_success(success), | ||
error: this._on_error(error) | ||||
}); | ||||
}; | ||||
Session.prototype.rename_notebook = function (name, path, success, error) { | ||||
Jessica B. Hamrick
|
r18211 | this.change(name, path, undefined, success, error); | ||
Jessica B. Hamrick
|
r18198 | }; | ||
/** | ||||
* DELETE /api/sessions/[:session_id] | ||||
*/ | ||||
Jessica B. Hamrick
|
r18200 | Session.prototype.delete = function (success, error) { | ||
Jessica B. Hamrick
|
r18206 | if (this.kernel) { | ||
this.kernel._kernel_dead(); | ||||
} | ||||
Jessica B. Hamrick
|
r18198 | |||
Jessica B. Hamrick
|
r18206 | $.ajax(this.session_url, { | ||
Jessica B. Hamrick
|
r18198 | processData: false, | ||
cache: false, | ||||
type: "DELETE", | ||||
dataType: "json", | ||||
Jessica B. Hamrick
|
r18206 | success: this._on_success(success), | ||
Jessica B. Hamrick
|
r18198 | error: this._on_error(error) | ||
}); | ||||
Zachary Sailer
|
r12997 | }; | ||
Jessica B. Hamrick
|
r18198 | Session.prototype._get_model = function () { | ||
return { | ||||
Jessica B. Hamrick
|
r18200 | notebook: this.notebook_model, | ||
kernel: this.kernel_model | ||||
Jessica B. Hamrick
|
r18198 | }; | ||
Zachary Sailer
|
r12987 | }; | ||
MinRK
|
r17676 | |||
Jessica B. Hamrick
|
r18198 | Session.prototype._update_model = function (data) { | ||
Jessica B. Hamrick
|
r18200 | if (data && data.id) { | ||
this.id = data.id; | ||||
Jessica B. Hamrick
|
r18206 | this.session_url = utils.url_join_encode(this.session_service_url, this.id); | ||
Jessica B. Hamrick
|
r18200 | } | ||
if (data && data.notebook) { | ||||
this.notebook_model.name = data.notebook.name; | ||||
this.notebook_model.path = data.notebook.path; | ||||
Jessica B. Hamrick
|
r18198 | } | ||
Jessica B. Hamrick
|
r18200 | if (data && data.kernel) { | ||
this.kernel_model.name = data.kernel.name; | ||||
this.kernel_model.id = data.kernel.id; | ||||
Jessica B. Hamrick
|
r18198 | } | ||
Zachary Sailer
|
r12987 | }; | ||
Jessica B. Hamrick
|
r18198 | |||
Session.prototype._on_success = function (success) { | ||||
var that = this; | ||||
return function (data, status, xhr) { | ||||
that._update_model(data); | ||||
if (success) { | ||||
success(data, status, xhr); | ||||
} | ||||
}; | ||||
Zachary Sailer
|
r12995 | }; | ||
Jessica B. Hamrick
|
r18198 | Session.prototype._on_error = function (error) { | ||
return function (xhr, status, err) { | ||||
utils.log_ajax_error(xhr, status, err); | ||||
if (error) { | ||||
error(xhr, status, err); | ||||
} | ||||
}; | ||||
Zachary Sailer
|
r12995 | }; | ||
Jessica B. Hamrick
|
r18198 | |||
MinRK
|
r17649 | var SessionAlreadyStarting = function (message) { | ||
this.name = "SessionAlreadyStarting"; | ||||
this.message = (message || ""); | ||||
}; | ||||
SessionAlreadyStarting.prototype = Error.prototype; | ||||
Jonathan Frederic
|
r17198 | // For backwards compatability. | ||
Zachary Sailer
|
r12987 | IPython.Session = Session; | ||
MinRK
|
r17649 | return { | ||
Session: Session, | ||||
Jessica B. Hamrick
|
r18198 | SessionAlreadyStarting: SessionAlreadyStarting | ||
MinRK
|
r17649 | }; | ||
Jonathan Frederic
|
r17198 | }); | ||