##// END OF EJS Templates
Use JS events for switching kernelspecs
Use JS events for switching kernelspecs

File last commit:

r17380:2168349e
r17380:2168349e
Show More
kernelselector.js
62 lines | 2.1 KiB | application/javascript | JavascriptLexer
Thomas Kluyver
Allow switching kernel from the notebook UI
r17370 // Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
define([
'base/js/namespace',
'jquery',
'base/js/utils',
], function(IPython, $, utils) {
"use strict";
var KernelSelector = function(selector, notebook) {
this.selector = selector;
this.notebook = notebook;
Thomas Kluyver
Use JS events for switching kernelspecs
r17380 this.events = notebook.events;
Thomas Kluyver
Allow switching kernel from the notebook UI
r17370 this.kernelspecs = {};
if (this.selector !== undefined) {
this.element = $(selector);
this.request_kernelspecs();
}
Thomas Kluyver
Use JS events for switching kernelspecs
r17380 this.bind_events();
Thomas Kluyver
Change displayed kernel name when our session is started
r17371 // For now, this is how we make this object available elsewhere
IPython.kernelselector = this;
Thomas Kluyver
Allow switching kernel from the notebook UI
r17370 };
KernelSelector.prototype.request_kernelspecs = function() {
var url = utils.url_join_encode(this.notebook.base_url, 'api/kernelspecs');
$.ajax(url, {success: $.proxy(this.got_kernelspecs, this)});
};
KernelSelector.prototype.got_kernelspecs = function(data, status, xhr) {
this.kernelspecs = {};
var menu = this.element.find("#kernel_selector");
for (var i = 0; i < data.length; i++) {
var ks = data[i];
this.kernelspecs[ks.name] = ks;
var ksentry = $("<li>").attr("id", "kernel-" +ks.name).append($('<a>')
.attr('href', '#')
.click($.proxy(this.change_kernel, this, ks.name))
.text(ks.display_name));
menu.append(ksentry);
}
};
KernelSelector.prototype.change_kernel = function(kernel_name) {
Thomas Kluyver
Change displayed kernel name when our session is started
r17371 if (kernel_name === this.notebook.kernel.name) {
Thomas Kluyver
Allow switching kernel from the notebook UI
r17370 return;
}
Thomas Kluyver
Use JS events for switching kernelspecs
r17380 var ks = this.kernelspecs[kernel_name];
this.events.trigger('spec_changed.Kernel', ks);
Thomas Kluyver
Allow switching kernel from the notebook UI
r17370 this.notebook.session.delete();
this.notebook.start_session(kernel_name);
};
Thomas Kluyver
Use JS events for switching kernelspecs
r17380 KernelSelector.prototype.bind_events = function() {
var that = this;
this.events.on('spec_changed.Kernel', function(event, data) {
that.element.find("#current_kernel_spec").text(data.display_name);
});
Thomas Kluyver
Change displayed kernel name when our session is started
r17371 };
Thomas Kluyver
Allow switching kernel from the notebook UI
r17370 return {'KernelSelector': KernelSelector};
});