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('
    x'+
                          'x'+
    diff --git a/IPython/html/static/notebook/js/notebook.js b/IPython/html/static/notebook/js/notebook.js
    index d87ab63..0af6642 100644
    --- a/IPython/html/static/notebook/js/notebook.js
    +++ b/IPython/html/static/notebook/js/notebook.js
    @@ -1496,7 +1496,11 @@ define([
          * 
          * @method start_session
          */
    -    Notebook.prototype.start_session = function () {
    +    Notebook.prototype.start_session = function (kernel_name) {
    +        if (kernel_name === undefined) {
    +            kernel_name = this.default_kernel_name;
    +        }
    +        console.log("start_session", kernel_name);
             this.session = new session.Session({
                 base_url: this.base_url,
                 ws_url: this.ws_url,
    @@ -1505,7 +1509,7 @@ define([
                 // For now, create all sessions with the 'python' kernel, which is the
                 // default. Later, the user will be able to select kernels. This is
                 // overridden if KernelManager.kernel_cmd is specified for the server.
    -            kernel_name: this.default_kernel_name,
    +            kernel_name: kernel_name,
                 notebook: this});
     
             this.session.start($.proxy(this._session_started, this));
    diff --git a/IPython/html/templates/notebook.html b/IPython/html/templates/notebook.html
    index 959621f..0ad6185 100644
    --- a/IPython/html/templates/notebook.html
    +++ b/IPython/html/templates/notebook.html
    @@ -40,6 +40,12 @@ class="notebook_app"
         
     
     
    +
    +    Python
    +    
    +
    +
     {% endblock %}