From 58f37e13781c8cda84b6ce406c069ac6fb18cbbe 2014-07-24 23:31:55 From: Thomas Kluyver Date: 2014-07-24 23:31:55 Subject: [PATCH] Allow switching kernel from the notebook UI --- diff --git a/IPython/html/static/notebook/js/kernelselector.js b/IPython/html/static/notebook/js/kernelselector.js new file mode 100644 index 0000000..8dcf714 --- /dev/null +++ b/IPython/html/static/notebook/js/kernelselector.js @@ -0,0 +1,58 @@ +// 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; + this.kernelspecs = {}; + this.current = "python"; + if (this.selector !== undefined) { + this.element = $(selector); + this.style(); + this.request_kernelspecs(); + } + }; + + KernelSelector.prototype.style = function() { + }; + + 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 = $("
  • ").attr("id", "kernel-" +ks.name).append($('') + .attr('href', '#') + .click($.proxy(this.change_kernel, this, ks.name)) + .text(ks.display_name)); + menu.append(ksentry); + } + }; + + KernelSelector.prototype.change_kernel = function(kernel_name) { + console.log("change_kernel " + kernel_name + " from " + this.current); + if (kernel_name === this.current) { + return; + } + this.notebook.session.delete(); + this.notebook.start_session(kernel_name); + this.current = kernel_name; + var display_name = this.kernelspecs[kernel_name].display_name; + this.element.find("#current_kernel_spec").text(display_name); + }; + + return {'KernelSelector': KernelSelector}; +}); diff --git a/IPython/html/static/notebook/js/main.js b/IPython/html/static/notebook/js/main.js index 78c91bd..ca945fb 100644 --- a/IPython/html/static/notebook/js/main.js +++ b/IPython/html/static/notebook/js/main.js @@ -18,6 +18,7 @@ require([ 'notebook/js/savewidget', 'notebook/js/keyboardmanager', 'notebook/js/config', + 'notebook/js/kernelselector', // only loaded, not used: 'custom/custom', ], function( @@ -36,7 +37,8 @@ require([ notificationarea, savewidget, keyboardmanager, - config + config, + kernelselector ) { "use strict"; @@ -90,6 +92,8 @@ require([ notebook: notebook, keyboard_manager: keyboard_manager}); notification_area.init_notification_widgets(); + var kernel_selector = new kernelselector.KernelSelector( + '#kernel_selector_widget', notebook); $('body').append('