session.js
121 lines
| 3.5 KiB
| application/javascript
|
JavascriptLexer
Zachary Sailer
|
r12987 | //---------------------------------------------------------------------------- | ||
MinRK
|
r13103 | // Copyright (C) 2013 The IPython Development Team | ||
Zachary Sailer
|
r12987 | // | ||
// Distributed under the terms of the BSD License. The full license is in | ||||
// the file COPYING, distributed as part of this software. | ||||
//---------------------------------------------------------------------------- | ||||
//============================================================================ | ||||
// Notebook | ||||
//============================================================================ | ||||
var IPython = (function (IPython) { | ||||
MinRK
|
r13103 | "use strict"; | ||
Zachary Sailer
|
r12987 | |||
MinRK
|
r13103 | var utils = IPython.utils; | ||
MinRK
|
r15234 | var Session = function(notebook, options){ | ||
Zachary Sailer
|
r12987 | this.kernel = null; | ||
Zachary Sailer
|
r13060 | this.id = null; | ||
MinRK
|
r13103 | this.notebook = notebook; | ||
MinRK
|
r15234 | this.name = notebook.notebook_name; | ||
this.path = notebook.notebook_path; | ||||
MinRK
|
r15238 | this.base_url = notebook.base_url; | ||
Zachary Sailer
|
r12987 | }; | ||
MinRK
|
r13133 | Session.prototype.start = function(callback) { | ||
MinRK
|
r13103 | var that = this; | ||
var model = { | ||||
notebook : { | ||||
name : this.name, | ||||
path : this.path | ||||
} | ||||
}; | ||||
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); | ||||
if (callback) { | ||||
callback(data, status, xhr); | ||||
} | ||||
}, | ||||
MinRK
|
r16445 | error : utils.log_ajax_error, | ||
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
|
r13133 | Session.prototype.delete = function() { | ||
Zachary Sailer
|
r12997 | var settings = { | ||
processData : false, | ||||
cache : false, | ||||
type : "DELETE", | ||||
dataType : "json", | ||||
MinRK
|
r16445 | error : utils.log_ajax_error, | ||
Zachary Sailer
|
r12997 | }; | ||
Paul Ivanov
|
r13308 | this.kernel.running = false; | ||
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; | ||||
MinRK
|
r15310 | var kernel_service_url = utils.url_path_join(this.base_url, "api/kernels"); | ||
MinRK
|
r15242 | this.kernel = new IPython.Kernel(kernel_service_url); | ||
MinRK
|
r13133 | this.kernel._kernel_started(data.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 | |||
Zachary Sailer
|
r12987 | IPython.Session = Session; | ||
return IPython; | ||||
}(IPython)); | ||||