##// END OF EJS Templates
use single WebSocket connection for all channels...
use single WebSocket connection for all channels multiplex on a 'channel' key in message, rather than separate websockets. Unlike zmq, there aren't different message patterns that require the channels to be separate. Reduces FD count by factor of 3 and connection complexity in js.

File last commit:

r19745:bff10f31
r19824:17ebfc54
Show More
kernelselector.js
156 lines | 5.6 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;
Bussonnier Matthias
move setting nb.kernelselector inside kernelselector itself
r19448 this.notebook.set_kernelselector(this);
Thomas Kluyver
Use JS events for switching kernelspecs
r17380 this.events = notebook.events;
MinRK
allow kernel_name to be undefined in js...
r18032 this.current_selection = null;
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
Address review comments
r17392 // Make the object globally available for user convenience & inspection
IPython.kernelselector = this;
Matthias BUSSONNIER
load the per kernel kernel.js and kernel.css...
r19404 Object.seal(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');
Min RK
kernelspecs is a dict...
r19263 utils.promising_ajax(url).then($.proxy(this._got_kernelspecs, this));
Thomas Kluyver
Allow switching kernel from the notebook UI
r17370 };
Min RK
kernelspecs is a dict...
r19263 KernelSelector.prototype._got_kernelspecs = function(data) {
this.kernelspecs = data.kernelspecs;
Thomas Kluyver
Add submenu under kernel for changing kernel
r17387 var change_kernel_submenu = $("#menu-change-kernel-submenu");
Min RK
kernelspecs is a dict...
r19263 var keys = Object.keys(data.kernelspecs).sort(function (a, b) {
// sort by display_name
Min RK
add resource URLs to kernelspec model...
r19681 var da = data.kernelspecs[a].spec.display_name;
var db = data.kernelspecs[b].spec.display_name;
Min RK
kernelspecs is a dict...
r19263 if (da === db) {
return 0;
} else if (da > db) {
return 1;
} else {
return -1;
}
});
for (var i = 0; i < keys.length; i++) {
var ks = this.kernelspecs[keys[i]];
Thomas Kluyver
Add submenu under kernel for changing kernel
r17387 var ks_submenu_entry = $("<li>").attr("id", "kernel-submenu-"+ks.name).append($('<a>')
.attr('href', '#')
.click($.proxy(this.change_kernel, this, ks.name))
Min RK
add resource URLs to kernelspec model...
r19681 .text(ks.spec.display_name));
Thomas Kluyver
Add submenu under kernel for changing kernel
r17387 change_kernel_submenu.append(ks_submenu_entry);
Thomas Kluyver
Allow switching kernel from the notebook UI
r17370 }
};
Min RK
load kernel js, css, logo on spec_changed event...
r19745
KernelSelector.prototype._spec_changed = function (event, ks) {
/** event handler for spec_changed */
// update selection
this.current_selection = ks.name;
// load logo
var logo_img = this.element.find("img.current_kernel_logo");
$("#kernel_indicator").find('.kernel_indicator_name').text(ks.spec.display_name);
if (ks.resources['logo-64x64']) {
logo_img.attr("src", ks.resources['logo-64x64']);
logo_img.show();
} else {
logo_img.hide();
Thomas Kluyver
Allow switching kernel from the notebook UI
r17370 }
Min RK
add resource URLs to kernelspec model...
r19681
Min RK
load kernel js, css, logo on spec_changed event...
r19745 // load kernel css
Min RK
add resource URLs to kernelspec model...
r19681 var css_url = ks.resources['kernel.css'];
if (css_url) {
$('#kernel-css').attr('href', css_url);
} else {
$('#kernel-css').attr('href', '');
}
Min RK
load kernel js, css, logo on spec_changed event...
r19745 // load kernel js
Min RK
add resource URLs to kernelspec model...
r19681 if (ks.resources['kernel.js']) {
require([ks.resources['kernel.js']],
function (kernel_mod) {
if (kernel_mod && kernel_mod.onload) {
kernel_mod.onload();
} else {
console.warn("Kernel " + ks.name + " has a kernel.js file that does not contain "+
"any asynchronous module definition. This is undefined behavior "+
"and not recommended.");
}
}, function (err) {
console.warn("Failed to load kernel.js from ", ks.resources['kernel.js'], err);
Matthias BUSSONNIER
load the per kernel kernel.js and kernel.css...
r19404 }
Min RK
add resource URLs to kernelspec model...
r19681 );
}
Min RK
load kernel js, css, logo on spec_changed event...
r19745 };
Min RK
add resource URLs to kernelspec model...
r19681
Min RK
load kernel js, css, logo on spec_changed event...
r19745 KernelSelector.prototype.change_kernel = function (kernel_name) {
/**
* TODO, have a methods to set kernel spec directly ?
**/
if (kernel_name === this.current_selection) {
return;
}
var ks = this.kernelspecs[kernel_name];
try {
this.notebook.start_session(kernel_name);
} catch (e) {
if (e.name === 'SessionAlreadyStarting') {
console.log("Cannot change kernel while waiting for pending session start.");
} else {
// unhandled error
throw e;
}
// only trigger spec_changed if change was successful
return;
}
console.log('spec', kernel_name, ks);
this.events.trigger('spec_changed.Kernel', ks);
Thomas Kluyver
Allow switching kernel from the notebook UI
r17370 };
Matthias BUSSONNIER
load the per kernel kernel.js and kernel.css...
r19404
KernelSelector.prototype.lock_switch = function() {
Matthias Bussonnier
adresses Min comments
r19406 // should set a flag and display warning+reload if user want to
// re-change kernel. As UI discussion never finish
// making that a separate PR.
Matthias Bussonnier
Fix typos, rephrase english. Thanks Kyle.
r19405 console.warn('switching kernel is not guaranteed to work !');
Matthias BUSSONNIER
load the per kernel kernel.js and kernel.css...
r19404 };
Thomas Kluyver
Use JS events for switching kernelspecs
r17380 KernelSelector.prototype.bind_events = function() {
var that = this;
Min RK
load kernel js, css, logo on spec_changed event...
r19745 this.events.on('spec_changed.Kernel', $.proxy(this._spec_changed, this));
Matthias BUSSONNIER
load the per kernel kernel.js and kernel.css...
r19404
Min RK
load kernel js, css, logo on spec_changed event...
r19745 this.events.on('kernel_created.Session', function (event, data) {
Min RK
fix event hookup for kernel changed...
r18919 if (data.kernel.name !== that.current_selection) {
Thomas Kluyver
Don't refer to global kernelselector object in Session
r17384 // If we created a 'python' session, we only know if it's Python
// 3 or 2 on the server's reply, so we fire the event again to
// set things up.
Min RK
fix event hookup for kernel changed...
r18919 var ks = that.kernelspecs[data.kernel.name];
Thomas Kluyver
Don't refer to global kernelselector object in Session
r17384 that.events.trigger('spec_changed.Kernel', ks);
}
});
Thomas Kluyver
Hide kernel logo if it's missing
r19374
Min RK
load kernel js, css, logo on spec_changed event...
r19745 var logo_img = this.element.find("img.current_kernel_logo");
Thomas Kluyver
Hide kernel logo if it's missing
r19374 logo_img.on("load", function() {
logo_img.show();
});
logo_img.on("error", function() {
logo_img.hide();
});
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};
});