kernelselector.js
159 lines
| 5.8 KiB
| application/javascript
|
JavascriptLexer
|
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; | ||||
|
r19448 | this.notebook.set_kernelselector(this); | ||
|
r17380 | this.events = notebook.events; | ||
|
r18032 | this.current_selection = null; | ||
|
r17370 | this.kernelspecs = {}; | ||
if (this.selector !== undefined) { | ||||
this.element = $(selector); | ||||
this.request_kernelspecs(); | ||||
} | ||||
|
r17380 | this.bind_events(); | ||
|
r17392 | // Make the object globally available for user convenience & inspection | ||
IPython.kernelselector = this; | ||||
|
r19404 | Object.seal(this); | ||
|
r17370 | }; | ||
KernelSelector.prototype.request_kernelspecs = function() { | ||||
var url = utils.url_join_encode(this.notebook.base_url, 'api/kernelspecs'); | ||||
|
r19263 | utils.promising_ajax(url).then($.proxy(this._got_kernelspecs, this)); | ||
|
r17370 | }; | ||
|
r19263 | KernelSelector.prototype._got_kernelspecs = function(data) { | ||
this.kernelspecs = data.kernelspecs; | ||||
|
r17370 | var menu = this.element.find("#kernel_selector"); | ||
|
r17387 | var change_kernel_submenu = $("#menu-change-kernel-submenu"); | ||
|
r19263 | var keys = Object.keys(data.kernelspecs).sort(function (a, b) { | ||
// sort by display_name | ||||
var da = data.kernelspecs[a].display_name; | ||||
var db = data.kernelspecs[b].display_name; | ||||
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]]; | ||||
|
r17370 | 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); | ||||
|
r17387 | |||
var ks_submenu_entry = $("<li>").attr("id", "kernel-submenu-"+ks.name).append($('<a>') | ||||
.attr('href', '#') | ||||
.click($.proxy(this.change_kernel, this, ks.name)) | ||||
.text(ks.display_name)); | ||||
change_kernel_submenu.append(ks_submenu_entry); | ||||
|
r17370 | } | ||
}; | ||||
KernelSelector.prototype.change_kernel = function(kernel_name) { | ||||
|
r19404 | /** | ||
* TODO, have a methods to set kernel spec directly ? | ||||
**/ | ||||
var that = this; | ||||
|
r17384 | if (kernel_name === this.current_selection) { | ||
|
r17370 | return; | ||
} | ||||
|
r17380 | var ks = this.kernelspecs[kernel_name]; | ||
|
r19404 | var new_mode_url = 'kernelspecs/'+ks.name+'/kernel'; | ||
var css_url = this.notebook.base_url+new_mode_url+'.css'; | ||||
$.ajax({ | ||||
type: 'HEAD', | ||||
url: css_url, | ||||
success: function(){ | ||||
$('#kernel-css') | ||||
.attr('href',css_url); | ||||
}, | ||||
error:function(){ | ||||
|
r19406 | console.info("No custom kernel.css at URL:", css_url) | ||
|
r19404 | } | ||
}); | ||||
|
r17649 | 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; | ||||
} | ||||
|
r17380 | this.events.trigger('spec_changed.Kernel', ks); | ||
|
r19404 | |||
// load new mode kernel.js if exist | ||||
require([new_mode_url], | ||||
// if new mode has custom.js | ||||
function(new_mode){ | ||||
that.lock_switch(); | ||||
if(new_mode && new_mode.onload){ | ||||
new_mode.onload(); | ||||
} else { | ||||
|
r19406 | console.warn("The current kernel defined a kernel.js file but does not contain "+ | ||
"any asynchronous module definition. This is undefined behavior "+ | ||||
|
r19405 | "which is not recommended"); | ||
|
r19404 | } | ||
}, | ||||
function(err){ | ||||
// if new mode does not have custom.js | ||||
|
r19406 | console.info("No custom kernel.css at URL:", new_mode_url) | ||
|
r19404 | } | ||
); | ||||
|
r17370 | }; | ||
|
r19404 | |||
KernelSelector.prototype.lock_switch = function() { | ||||
|
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. | ||||
|
r19405 | console.warn('switching kernel is not guaranteed to work !'); | ||
|
r19404 | }; | ||
|
r17380 | KernelSelector.prototype.bind_events = function() { | ||
var that = this; | ||||
this.events.on('spec_changed.Kernel', function(event, data) { | ||||
|
r17384 | that.current_selection = data.name; | ||
|
r17382 | that.element.find("#current_kernel_spec").find('.kernel_name').text(data.display_name); | ||
|
r19584 | that.element.find("#current_kernel_logo").attr("src", that.notebook.base_url+"kernelspecs/"+data.name+"/logo-64x64.png"); | ||
|
r17380 | }); | ||
|
r19404 | |||
|
r18919 | this.events.on('kernel_created.Session', function(event, data) { | ||
if (data.kernel.name !== that.current_selection) { | ||||
|
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. | ||||
|
r18919 | var ks = that.kernelspecs[data.kernel.name]; | ||
|
r17384 | that.events.trigger('spec_changed.Kernel', ks); | ||
} | ||||
}); | ||||
|
r19374 | |||
var logo_img = this.element.find("#current_kernel_logo") | ||||
logo_img.on("load", function() { | ||||
logo_img.show(); | ||||
}); | ||||
logo_img.on("error", function() { | ||||
logo_img.hide(); | ||||
}); | ||||
|
r17371 | }; | ||
|
r17370 | return {'KernelSelector': KernelSelector}; | ||
}); | ||||