diff --git a/IPython/html/static/notebook/js/kernelselector.js b/IPython/html/static/notebook/js/kernelselector.js index 953a232..6c4845e 100644 --- a/IPython/html/static/notebook/js/kernelselector.js +++ b/IPython/html/static/notebook/js/kernelselector.js @@ -25,6 +25,7 @@ define([ // Make the object globally available for user convenience & inspection IPython.kernelselector = this; this._finish_load = null; + this._loaded = false; this.loaded = new Promise(function(resolve, reject) { that._finish_load = resolve; }); @@ -83,6 +84,7 @@ define([ }); // trigger loaded promise + this._loaded = true; this._finish_load(); }; @@ -154,28 +156,86 @@ define([ } }; - KernelSelector.prototype.set_kernel = function (kernel_name) { - /** set the kernel by name, ensuring kernelspecs have been loaded, first */ + KernelSelector.prototype.set_kernel = function (selected) { + /** set the kernel by name, ensuring kernelspecs have been loaded, first + + kernel can be just a kernel name, or a notebook kernelspec metadata + (name, project_name, display_name). + */ var that = this; - return this.loaded.then(function () { - that._set_kernel(kernel_name); - }); + if (typeof selected === 'string') { + selected = { + name: selected + }; + } + if (this._loaded) { + this._set_kernel(selected); + } else { + return this.loaded.then(function () { + that._set_kernel(selected); + }); + } }; - KernelSelector.prototype._set_kernel = function (kernel_name) { + KernelSelector.prototype._set_kernel = function (selected) { /** Actually set the kernel (kernelspecs have been loaded) */ - if (kernel_name === this.current_selection) { + if (selected.name === this.current_selection) { // only trigger event if value changed return; } - var ks = this.kernelspecs[kernel_name]; + var ks = this.kernelspecs[selected.name]; + if (ks === undefined) { + if (selected.project_name && selected.project_name.length > 0) { + $.map(this.kernelspecs, function (k) { + if (k.spec.project_name === selected.project_name) { + ks = k; + } + }); + } + // if still undefined, trigger failure event + if (ks === undefined) { + this.events.trigger("spec_not_found.Kernel", selected); + return; + } + } if (this.notebook._session_starting) { console.error("Cannot change kernel while waiting for pending session start."); return; } - this.current_selection = kernel_name; + this.current_selection = ks.name; this.events.trigger('spec_changed.Kernel', ks); }; + + KernelSelector.prototype._spec_not_found = function (event, data) { + var that = this; + var select = $("