replace project_name with language_name

- automatically choose language match, if only one
- only list language matches in select dialog
- list all kernels if no language match
This commit is contained in:
Min RK 2015-01-20 15:05:08 -08:00
parent e514d930cf
commit f5e2bb712d
2 changed files with 42 additions and 21 deletions

View File

@ -26,7 +26,7 @@ define([
IPython.kernelselector = this; IPython.kernelselector = this;
this._finish_load = null; this._finish_load = null;
this._loaded = false; this._loaded = false;
this.loaded = new Promise(function(resolve, reject) { this.loaded = new Promise(function(resolve) {
that._finish_load = resolve; that._finish_load = resolve;
}); });
@ -38,16 +38,12 @@ define([
utils.promising_ajax(url).then($.proxy(this._got_kernelspecs, this)); utils.promising_ajax(url).then($.proxy(this._got_kernelspecs, this));
}; };
KernelSelector.prototype._got_kernelspecs = function(data) { var _sorted_names = function(kernelspecs) {
var that = this; // sort kernel names
this.kernelspecs = data.kernelspecs; return Object.keys(kernelspecs).sort(function (a, b) {
var change_kernel_submenu = $("#menu-change-kernel-submenu");
var new_notebook_submenu = $("#menu-new-notebook-submenu");
var keys = Object.keys(data.kernelspecs).sort(function (a, b) {
// sort by display_name // sort by display_name
var da = data.kernelspecs[a].spec.display_name; var da = kernelspecs[a].spec.display_name;
var db = data.kernelspecs[b].spec.display_name; var db = kernelspecs[b].spec.display_name;
if (da === db) { if (da === db) {
return 0; return 0;
} else if (da > db) { } else if (da > db) {
@ -56,6 +52,14 @@ define([
return -1; return -1;
} }
}); });
};
KernelSelector.prototype._got_kernelspecs = function(data) {
var that = this;
this.kernelspecs = data.kernelspecs;
var change_kernel_submenu = $("#menu-change-kernel-submenu");
var new_notebook_submenu = $("#menu-new-notebook-submenu");
var keys = _sorted_names(data.kernelspecs);
keys.map(function (key) { keys.map(function (key) {
// Create the Kernel > Change kernel submenu // Create the Kernel > Change kernel submenu
@ -160,7 +164,7 @@ define([
/** set the kernel by name, ensuring kernelspecs have been loaded, first /** set the kernel by name, ensuring kernelspecs have been loaded, first
kernel can be just a kernel name, or a notebook kernelspec metadata kernel can be just a kernel name, or a notebook kernelspec metadata
(name, project_name, display_name). (name, language_name, display_name).
*/ */
var that = this; var that = this;
if (typeof selected === 'string') { if (typeof selected === 'string') {
@ -183,18 +187,28 @@ define([
// only trigger event if value changed // only trigger event if value changed
return; return;
} }
var ks = this.kernelspecs[selected.name]; var kernelspecs = this.kernelspecs;
var ks = kernelspecs[selected.name];
if (ks === undefined) { if (ks === undefined) {
if (selected.project_name && selected.project_name.length > 0) { var available = _sorted_names(kernelspecs);
$.map(this.kernelspecs, function (k) { var matches = [];
if (k.spec.project_name === selected.project_name) { if (selected.language_name && selected.language_name.length > 0) {
ks = k; $.map(available, function (name) {
if (kernelspecs[name].spec.language_name.toLowerCase() === selected.language_name.toLowerCase()) {
matches.push(name);
} }
}); });
} }
if (matches.length === 1) {
ks = kernelspecs[matches[0]];
}
// if still undefined, trigger failure event // if still undefined, trigger failure event
if (ks === undefined) { if (ks === undefined) {
this.events.trigger("spec_not_found.Kernel", selected); this.events.trigger("spec_not_found.Kernel", {
selected: selected,
matches: matches,
available: available,
});
return; return;
} }
} }
@ -210,15 +224,22 @@ define([
var that = this; var that = this;
var select = $("<select>").addClass('form-control'); var select = $("<select>").addClass('form-control');
console.warn("Kernelspec not found:", data); console.warn("Kernelspec not found:", data);
$.map(this.kernelspecs, function (ks) { var names;
if (data.matches.length > 1) {
names = data.matches;
} else {
names = data.available;
}
$.map(names, function (name) {
var ks = that.kernelspecs[name];
select.append( select.append(
$('<option/>').attr('value', ks.name).text(ks.spec.display_name) $('<option/>').attr('value', ks.name).text(ks.spec.display_name || ks.name)
); );
}); });
var body = $("<form>").addClass("form-inline").append( var body = $("<form>").addClass("form-inline").append(
$("<span>").text( $("<span>").text(
"I couldn't find a kernel matching " + (data.display_name || data.name) + "." + "I couldn't find a kernel matching " + (data.selected.display_name || data.name) + "." +
" Please select a kernel:" " Please select a kernel:"
) )
).append(select); ).append(select);

View File

@ -249,7 +249,7 @@ define([
that.metadata.kernelspec = { that.metadata.kernelspec = {
name: data.name, name: data.name,
display_name: data.spec.display_name, display_name: data.spec.display_name,
project_name: data.spec.project_name, language_name: data.spec.language_name,
}; };
// start session if the current session isn't already correct // start session if the current session isn't already correct
if (!(this.session && this.session.kernel && this.session.kernel.name === data.name)) { if (!(this.session && this.session.kernel && this.session.kernel.name === data.name)) {