session.js
150 lines
| 4.5 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 | |||
jon
|
r17210 | var Session = function(options){ | ||
Zachary Sailer
|
r12987 | this.kernel = null; | ||
Zachary Sailer
|
r13060 | this.id = null; | ||
jon
|
r17210 | this.notebook = options.notebook; | ||
Thomas Kluyver
|
r17384 | this.events = options.notebook.events; | ||
Jonathan Frederic
|
r17212 | this.name = options.notebook_name; | ||
this.path = options.notebook_path; | ||||
Thomas Kluyver
|
r17223 | this.kernel_name = options.kernel_name; | ||
Jonathan Frederic
|
r17212 | this.base_url = options.base_url; | ||
MinRK
|
r17308 | this.ws_url = options.ws_url; | ||
Zachary Sailer
|
r12987 | }; | ||
MinRK
|
r17649 | Session.prototype.start = function (success, error) { | ||
MinRK
|
r13103 | var that = this; | ||
var model = { | ||||
notebook : { | ||||
name : this.name, | ||||
path : this.path | ||||
Thomas Kluyver
|
r17223 | }, | ||
kernel : { | ||||
name : this.kernel_name | ||||
MinRK
|
r13103 | } | ||
}; | ||||
Zachary Sailer
|
r13057 | var settings = { | ||
processData : false, | ||||
cache : false, | ||||
type : "POST", | ||||
MinRK
|
r13103 | data: JSON.stringify(model), | ||
Zachary Sailer
|
r13057 | dataType : "json", | ||
MinRK
|
r13133 | success : function (data, status, xhr) { | ||
that._handle_start_success(data); | ||||
MinRK
|
r17649 | if (success) { | ||
success(data, status, xhr); | ||||
MinRK
|
r13133 | } | ||
}, | ||||
MinRK
|
r17676 | error : function (xhr, status, err) { | ||
that._handle_start_failure(xhr, status, err); | ||||
if (error !== undefined) { | ||||
error(xhr, status, err); | ||||
} | ||||
utils.log_ajax_error(xhr, status, err); | ||||
} | ||||
Zachary Sailer
|
r13057 | }; | ||
MinRK
|
r15238 | var url = utils.url_join_encode(this.base_url, 'api/sessions'); | ||
Zachary Sailer
|
r13057 | $.ajax(url, settings); | ||
Zachary Sailer
|
r12987 | }; | ||
MinRK
|
r13133 | Session.prototype.rename_notebook = function (name, path) { | ||
Zachary Sailer
|
r13060 | this.name = name; | ||
this.path = path; | ||||
MinRK
|
r13103 | var model = { | ||
notebook : { | ||||
name : this.name, | ||||
path : this.path | ||||
} | ||||
}; | ||||
Zachary Sailer
|
r12997 | var settings = { | ||
processData : false, | ||||
cache : false, | ||||
type : "PATCH", | ||||
MinRK
|
r13103 | data: JSON.stringify(model), | ||
Zachary Sailer
|
r12997 | dataType : "json", | ||
MinRK
|
r16445 | error : utils.log_ajax_error, | ||
Zachary Sailer
|
r12997 | }; | ||
MinRK
|
r15238 | var url = utils.url_join_encode(this.base_url, 'api/sessions', this.id); | ||
Zachary Sailer
|
r12997 | $.ajax(url, settings); | ||
MinRK
|
r13103 | }; | ||
Zachary Sailer
|
r12997 | |||
MinRK
|
r17649 | Session.prototype.delete = function (success, error) { | ||
Zachary Sailer
|
r12997 | var settings = { | ||
processData : false, | ||||
cache : false, | ||||
type : "DELETE", | ||||
dataType : "json", | ||||
MinRK
|
r17649 | success : success, | ||
error : error || utils.log_ajax_error, | ||||
Zachary Sailer
|
r12997 | }; | ||
MinRK
|
r17676 | if (this.kernel) { | ||
this.kernel.running = false; | ||||
this.kernel.stop_channels(); | ||||
} | ||||
MinRK
|
r15238 | var url = utils.url_join_encode(this.base_url, 'api/sessions', this.id); | ||
Zachary Sailer
|
r12997 | $.ajax(url, settings); | ||
}; | ||||
Zachary Sailer
|
r12987 | // Kernel related things | ||
/** | ||||
MinRK
|
r13133 | * Create the Kernel object associated with this Session. | ||
Zachary Sailer
|
r12987 | * | ||
MinRK
|
r13133 | * @method _handle_start_success | ||
Zachary Sailer
|
r12987 | */ | ||
MinRK
|
r13133 | Session.prototype._handle_start_success = function (data, status, xhr) { | ||
this.id = data.id; | ||||
Thomas Kluyver
|
r17380 | // If we asked for 'python', the response will have 'python3' or 'python2'. | ||
Thomas Kluyver
|
r17384 | this.kernel_name = data.kernel.name; | ||
this.events.trigger('started.Session', this); | ||||
MinRK
|
r15310 | var kernel_service_url = utils.url_path_join(this.base_url, "api/kernels"); | ||
MinRK
|
r17308 | this.kernel = new kernel.Kernel(kernel_service_url, this.ws_url, this.notebook, this.kernel_name); | ||
MinRK
|
r13133 | this.kernel._kernel_started(data.kernel); | ||
Zachary Sailer
|
r12987 | }; | ||
MinRK
|
r17676 | |||
Session.prototype._handle_start_failure = function (xhr, status, error) { | ||||
this.events.trigger('start_failed.Session', [this, xhr, status, error]); | ||||
this.events.trigger('status_dead.Kernel'); | ||||
}; | ||||
Zachary Sailer
|
r12987 | |||
/** | ||||
* Prompt the user to restart the IPython kernel. | ||||
* | ||||
* @method restart_kernel | ||||
*/ | ||||
Session.prototype.restart_kernel = function () { | ||||
Zachary Sailer
|
r12994 | this.kernel.restart(); | ||
Zachary Sailer
|
r12987 | }; | ||
Zachary Sailer
|
r12994 | Session.prototype.interrupt_kernel = function() { | ||
this.kernel.interrupt(); | ||||
Zachary Sailer
|
r12995 | }; | ||
Session.prototype.kill_kernel = function() { | ||||
MinRK
|
r13103 | this.kernel.kill(); | ||
Zachary Sailer
|
r12995 | }; | ||
Zachary Sailer
|
r12994 | |||
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, | ||||
SessionAlreadyStarting: SessionAlreadyStarting, | ||||
}; | ||||
Jonathan Frederic
|
r17198 | }); | ||