session.js
186 lines
| 5.2 KiB
| application/javascript
|
JavascriptLexer
|
r17198 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
|
r12987 | |||
|
r17198 | define([ | ||
'base/js/namespace', | ||||
|
r17200 | 'jquery', | ||
|
r17198 | 'base/js/utils', | ||
'services/kernels/js/kernel', | ||||
|
r17202 | ], function(IPython, $, utils, kernel) { | ||
|
r13103 | "use strict"; | ||
|
r17198 | |||
|
r18198 | var Session = function (options) { | ||
|
r13060 | this.id = null; | ||
|
r18198 | this.notebook = { | ||
name: options.notebook_name, | ||||
path: options.notebook_path | ||||
}; | ||||
this.kernel = { | ||||
name: options.kernel_name | ||||
}; | ||||
|
r17212 | this.base_url = options.base_url; | ||
|
r17308 | this.ws_url = options.ws_url; | ||
|
r18198 | this.sessions_url = utils.url_join_encode(this.base_url, 'api/sessions'); | ||
this.notebook_obj = options.notebook; | ||||
this.events = options.events; | ||||
|
r12987 | }; | ||
|
r18198 | |||
/** | ||||
* GET /api/sessions | ||||
*/ | ||||
Session.prototype.list = function (success, error) { | ||||
$.ajax(this.sessions_url, { | ||||
processData: false, | ||||
cache: false, | ||||
type: "GET", | ||||
dataType: "json", | ||||
success: success, | ||||
error: this._on_error(error) | ||||
}); | ||||
}; | ||||
/** | ||||
* POST /api/sessions | ||||
*/ | ||||
|
r17649 | Session.prototype.start = function (success, error) { | ||
|
r13103 | var that = this; | ||
|
r18198 | var on_success = function (data, status, xhr) { | ||
var kernel_service_url = utils.url_path_join(that.base_url, "api/kernels"); | ||||
that.kernel = new kernel.Kernel(kernel_service_url, that.ws_url, that.notebook_obj, that.kernel_name); | ||||
that.kernel._kernel_started(data.kernel); | ||||
if (success) { | ||||
success(data, status, xhr); | ||||
|
r13103 | } | ||
}; | ||||
|
r18198 | var on_error = function (xhr, status, err) { | ||
that.events.trigger('status_dead.Kernel'); | ||||
if (error) { | ||||
error(xhr, status, err); | ||||
|
r17676 | } | ||
|
r13057 | }; | ||
|
r18198 | |||
$.ajax(this.sessions_url, { | ||||
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) | ||||
}); | ||||
|
r12987 | }; | ||
|
r18198 | |||
/** | ||||
* GET /api/sessions/[:session_id] | ||||
*/ | ||||
Session.prototype.get_info = function (success, error) { | ||||
var url = utils.url_join_encode(this.sessions_url, this.id); | ||||
$.ajax(url, { | ||||
processData: false, | ||||
cache: false, | ||||
type: "GET", | ||||
dataType: "json", | ||||
success: this._on_success(success), | ||||
error: this._on_error(error) | ||||
}); | ||||
|
r13103 | }; | ||
|
r18198 | |||
/** | ||||
* PATCH /api/sessions/[:session_id] | ||||
*/ | ||||
Session.prototype.change = function (notebook_name, notebook_path, kernel_name, success, error) { | ||||
this.notebook.name = notebook_name; | ||||
this.notebook.path = notebook_path; | ||||
this.kernel.name = kernel_name; | ||||
var url = utils.url_join_encode(this.sessions_url, this.id); | ||||
$.ajax(url, { | ||||
processData: false, | ||||
cache: false, | ||||
type: "PATCH", | ||||
data: JSON.stringify(this._get_model()), | ||||
|
r12997 | dataType : "json", | ||
|
r18198 | success: this._on_success(success), | ||
error: this._on_error(error) | ||||
}); | ||||
}; | ||||
Session.prototype.rename_notebook = function (name, path, success, error) { | ||||
this.change(name, path, this.kernel.name, success, error); | ||||
}; | ||||
/** | ||||
* DELETE /api/sessions/[:session_id] | ||||
*/ | ||||
Session.prototype.kill = function (success, error) { | ||||
|
r17676 | if (this.kernel) { | ||
this.kernel.running = false; | ||||
this.kernel.stop_channels(); | ||||
} | ||||
|
r18198 | |||
var url = utils.url_join_encode(this.sessions_url, this.id); | ||||
$.ajax(url, { | ||||
processData: false, | ||||
cache: false, | ||||
type: "DELETE", | ||||
dataType: "json", | ||||
success: this._on_success(success), | ||||
error: this._on_error(error) | ||||
}); | ||||
|
r12997 | }; | ||
|
r18198 | Session.prototype._get_model = function () { | ||
return { | ||||
notebook: this.notebook, | ||||
kernel: this.kernel | ||||
}; | ||||
|
r12987 | }; | ||
|
r17676 | |||
|
r18198 | Session.prototype._update_model = function (data) { | ||
this.id = data.id; | ||||
if (data.notebook) { | ||||
this.notebook.name = data.notebook.name; | ||||
this.notebook.path = data.notebook.path; | ||||
} | ||||
if (data.kernel) { | ||||
this.kernel.name = data.kernel.name; | ||||
} | ||||
|
r12987 | }; | ||
|
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); | ||||
} | ||||
}; | ||||
|
r12995 | }; | ||
|
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); | ||||
} | ||||
}; | ||||
|
r12995 | }; | ||
|
r18198 | |||
|
r17649 | var SessionAlreadyStarting = function (message) { | ||
this.name = "SessionAlreadyStarting"; | ||||
this.message = (message || ""); | ||||
}; | ||||
SessionAlreadyStarting.prototype = Error.prototype; | ||||
|
r17198 | // For backwards compatability. | ||
|
r12987 | IPython.Session = Session; | ||
|
r17649 | return { | ||
Session: Session, | ||||
|
r18198 | SessionAlreadyStarting: SessionAlreadyStarting | ||
|
r17649 | }; | ||
|
r17198 | }); | ||