Skip to content

Commit

Permalink
Merge pull request #7401 from mathieu1/new-notebook-same-kernel
Browse files Browse the repository at this point in the history
create new notebook with same kernel
  • Loading branch information
ellisonbg committed Jan 10, 2015
2 parents 3cd0ddb + c5df2a8 commit de9fe06
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 24 deletions.
47 changes: 47 additions & 0 deletions IPython/html/static/notebook/js/kernelselector.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ 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");

var keys = Object.keys(data.kernelspecs).sort(function (a, b) {
// sort by display_name
var da = data.kernelspecs[a].spec.display_name;
Expand All @@ -45,6 +47,8 @@ define([
return -1;
}
});

// Create the Kernel > Change kernel submenu
for (var i = 0; i < keys.length; i++) {
var ks = this.kernelspecs[keys[i]];
var ks_submenu_entry = $("<li>").attr("id", "kernel-submenu-"+ks.name).append($('<a>')
Expand All @@ -53,6 +57,16 @@ define([
.text(ks.spec.display_name));
change_kernel_submenu.append(ks_submenu_entry);
}

// Create the File > New Notebook submenu
for (var i = 0; i < keys.length; i++) {
var ks = this.kernelspecs[keys[i]];
var ks_submenu_entry = $("<li>").attr("id", "new-notebook-submenu-"+ks.name).append($('<a>')
.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) {
Expand All @@ -61,6 +75,13 @@ define([
// update selection
this.current_selection = ks.name;

// put the current kernel at the top of File > New Notebook
var cur_kernel_entry = $("#new-notebook-submenu-" + ks.name);
if (cur_kernel_entry.length) {
cur_kernel_entry.parent().prepend($("<li>").attr("class","divider"))
.prepend(cur_kernel_entry);
};

// load logo
var logo_img = this.element.find("img.current_kernel_logo");
$("#kernel_indicator").find('.kernel_indicator_name').text(ks.spec.display_name);
Expand Down Expand Up @@ -122,6 +143,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
Expand Down
22 changes: 1 addition & 21 deletions IPython/html/static/notebook/js/menubar.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,7 @@ define([
* File
*/
var that = this;
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.
var parent = utils.url_path_split(that.notebook.notebook_path)[0];
that.contents.new_untitled(parent, {type: "notebook"}).then(
function (data) {
w.location = utils.url_join_encode(
that.base_url, 'notebooks', data.path
);
},
function(error) {
w.close();
dialog.modal({
title : 'Creating Notebook Failed',
body : "The error was: " + error.message,
buttons : {'OK' : {'class' : 'btn-primary'}}
});
}
);
});

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));
Expand Down
7 changes: 4 additions & 3 deletions IPython/html/templates/notebook.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@
<ul class="nav navbar-nav">
<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">File</a>
<ul id="file_menu" class="dropdown-menu">
<li id="new_notebook"
title="Make a new notebook (Opens a new window)">
<a href="#">New</a></li>
<li id="new_notebook" class="dropdown-submenu">
<a href="#">New Notebook</a>
<ul class="dropdown-menu" id="menu-new-notebook-submenu"></ul>
</li>
<li id="open_notebook"
title="Opens a new window with the Dashboard view">
<a href="#">Open...</a></li>
Expand Down

0 comments on commit de9fe06

Please sign in to comment.