From b73e2adc11c06038be82ca08bf704665838de9a1 2015-01-09 14:46:06 From: Mathieu Date: 2015-01-09 14:46:06 Subject: [PATCH] add choice of kernel for new notebook --- diff --git a/IPython/html/static/notebook/js/kernelselector.js b/IPython/html/static/notebook/js/kernelselector.js index cd3535b..ab0fe95 100644 --- a/IPython/html/static/notebook/js/kernelselector.js +++ b/IPython/html/static/notebook/js/kernelselector.js @@ -33,6 +33,9 @@ define([ KernelSelector.prototype._got_kernelspecs = function(data) { this.kernelspecs = data.kernelspecs; var change_kernel_submenu = $("#menu-change-kernel-submenu"); + var new_notebook_submenu = $("#menu-new-notebook-submenu"); + + // Create the change kernel submenu var keys = Object.keys(data.kernelspecs).sort(function (a, b) { // sort by display_name var da = data.kernelspecs[a].spec.display_name; @@ -53,6 +56,41 @@ define([ .text(ks.spec.display_name)); change_kernel_submenu.append(ks_submenu_entry); } + + // Create the new notebook submenu + /** This code is supposed to put the current kernel at the top of the submenu + * but at the time _got_kernelspecs gets called, this.notebook.kernel is null + * + * var current_kernel_name = this.notebook.kernel.name + * var keys = Object.keys(data.kernelspecs).sort(function (a, b) { + * // sort by display_name, putting the current kernel on top + * var da = data.kernelspecs[a].spec.display_name; + * var db = data.kernelspecs[b].spec.display_name; + * if (da === db) { + * return 0; + * } else if (db == current_kernel_name || da > db) { + * return 1; + * } else { + * return -1; + * } + * }); + */ + + /** Uncomment to add header the the new notebook submenu + * + * new_notebook_submenu.append($("
  • ").attr("id","notebook-kernels") + * .attr("class","dropdown-header") + * .attr("role","presentation") + * .text("Notebook")) + */ + for (var i = 0; i < keys.length; i++) { + var ks = this.kernelspecs[keys[i]]; + var ks_submenu_entry = $("
  • ").attr("id", "new-notebook-submenu-"+ks.name).append($('') + .attr('href', '#') + .click($.proxy(this.new_notebook, this, ks.name)) + .text(ks.spec.display_name)); + new_notebook_submenu.append(ks_submenu_entry); + } }; KernelSelector.prototype._spec_changed = function (event, ks) { @@ -122,6 +160,32 @@ define([ this.events.trigger('spec_changed.Kernel', ks); }; + KernelSelector.prototype.new_notebook = function (kernel_name) { + + var w = window.open(); + // Create a new notebook in the same path as the current + // notebook's path. + var that = this; + var parent = utils.url_path_split(that.notebook.notebook_path)[0]; + that.notebook.contents.new_untitled(parent, {type: "notebook"}).then( + function (data) { + var url = utils.url_join_encode( + that.notebook.base_url, 'notebooks', data.path + ); + url += "?kernel_name=" + kernel_name; + w.location = url; + }, + function(error) { + w.close(); + dialog.modal({ + title : 'Creating Notebook Failed', + body : "The error was: " + error.message, + buttons : {'OK' : {'class' : 'btn-primary'}} + }); + } + ); + }; + KernelSelector.prototype.lock_switch = function() { // should set a flag and display warning+reload if user want to // re-change kernel. As UI discussion never finish diff --git a/IPython/html/static/notebook/js/menubar.js b/IPython/html/static/notebook/js/menubar.js index d3840fc..bcef1b1 100644 --- a/IPython/html/static/notebook/js/menubar.js +++ b/IPython/html/static/notebook/js/menubar.js @@ -98,7 +98,13 @@ define([ * File */ var that = this; - this.element.find('#new_notebook').click(function () { + /*var new_buttons = new newnotebook.NewNotebookWidget("#new-buttons", + $.extend( + {contents: contents}, + common_options + ) + );*/ + /*this.element.find('#new_notebook').click(function () { var w = window.open(); // Create a new notebook in the same path as the current // notebook's path. @@ -120,7 +126,7 @@ define([ }); } ); - }); + });*/ this.element.find('#open_notebook').click(function () { var parent = utils.url_path_split(that.notebook.notebook_path)[0]; window.open(utils.url_join_encode(that.base_url, 'tree', parent)); diff --git a/IPython/html/templates/notebook.html b/IPython/html/templates/notebook.html index dbefa0a..28c44ea 100644 --- a/IPython/html/templates/notebook.html +++ b/IPython/html/templates/notebook.html @@ -67,9 +67,10 @@ class="notebook_app"