mirror of
https://github.com/jupyter/notebook.git
synced 2025-01-30 12:11:32 +08:00
Add kernel-select dropdown to new notebook button
most recent choice is remembered in localStorage
This commit is contained in:
parent
f94e482478
commit
d312486840
4
IPython/html/static/style/style.min.css
vendored
4
IPython/html/static/style/style.min.css
vendored
@ -8164,6 +8164,10 @@ input.engine_num_input {
|
||||
.file_icon:before.pull-right {
|
||||
margin-left: .3em;
|
||||
}
|
||||
ul#new-notebook-menu {
|
||||
left: auto;
|
||||
right: 0;
|
||||
}
|
||||
/*!
|
||||
*
|
||||
* IPython notebook
|
||||
|
@ -14,6 +14,7 @@ require([
|
||||
'tree/js/sessionlist',
|
||||
'tree/js/kernellist',
|
||||
'tree/js/terminallist',
|
||||
'tree/js/newnotebook',
|
||||
'auth/js/loginwidget',
|
||||
// only loaded, not used:
|
||||
'jqueryui',
|
||||
@ -27,11 +28,12 @@ require([
|
||||
page,
|
||||
utils,
|
||||
contents_service,
|
||||
notebooklist,
|
||||
clusterlist,
|
||||
sesssionlist,
|
||||
notebooklist,
|
||||
clusterlist,
|
||||
sesssionlist,
|
||||
kernellist,
|
||||
terminallist,
|
||||
newnotebook,
|
||||
loginwidget){
|
||||
"use strict";
|
||||
|
||||
@ -63,24 +65,12 @@ require([
|
||||
|
||||
var login_widget = new loginwidget.LoginWidget('#login_widget', common_options);
|
||||
|
||||
$('#new_notebook').click(function (e) {
|
||||
var w = window.open();
|
||||
contents.new_untitled(common_options.notebook_path, {type: "notebook"}).then(
|
||||
function (data) {
|
||||
w.location = utils.url_join_encode(
|
||||
common_options.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'}}
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
var nnw = new newnotebook.NewNotebookWidget("#new-notebook-buttons",
|
||||
$.extend(
|
||||
{contents: contents},
|
||||
common_options
|
||||
)
|
||||
);
|
||||
|
||||
var interval_id=0;
|
||||
// auto refresh every xx secondes, no need to be fast,
|
||||
|
112
IPython/html/static/tree/js/newnotebook.js
Normal file
112
IPython/html/static/tree/js/newnotebook.js
Normal file
@ -0,0 +1,112 @@
|
||||
// Copyright (c) IPython Development Team.
|
||||
// Distributed under the terms of the Modified BSD License.
|
||||
|
||||
define([
|
||||
'jquery',
|
||||
'base/js/namespace',
|
||||
'base/js/utils',
|
||||
'base/js/dialog',
|
||||
], function ($, IPython, utils, dialog) {
|
||||
"use strict";
|
||||
|
||||
var NewNotebookWidget = function (selector, options) {
|
||||
this.selector = selector;
|
||||
this.base_url = options.base_url;
|
||||
this.notebook_path = options.notebook_path;
|
||||
this.contents = options.contents;
|
||||
this.current_selection = null;
|
||||
this.kernelspecs = {};
|
||||
if (this.selector !== undefined) {
|
||||
this.element = $(selector);
|
||||
this.request_kernelspecs();
|
||||
}
|
||||
this.bind_events();
|
||||
};
|
||||
|
||||
NewNotebookWidget.prototype.bind_events = function () {
|
||||
var that = this;
|
||||
this.element.find('#new_notebook').click(function () {
|
||||
that.new_notebook();
|
||||
});
|
||||
};
|
||||
|
||||
NewNotebookWidget.prototype.request_kernelspecs = function () {
|
||||
/** request and then load kernel specs */
|
||||
var url = utils.url_join_encode(this.base_url, 'api/kernelspecs');
|
||||
utils.promising_ajax(url).then($.proxy(this._load_kernelspecs, this));
|
||||
};
|
||||
|
||||
NewNotebookWidget.prototype._load_kernelspecs = function (data) {
|
||||
/** load kernelspec list */
|
||||
this.kernelspecs = {};
|
||||
var menu = this.element.find("#new-notebook-menu");
|
||||
for (var i = 0; i < data.length; i++) {
|
||||
var ks = data[i];
|
||||
this.kernelspecs[ks.name] = ks;
|
||||
var ksentry = $("<li>").attr("id", "kernel-" +ks.name).append($('<a>')
|
||||
.attr('href', '#')
|
||||
.click($.proxy(this.new_with_kernel, this, ks.name))
|
||||
.text(ks.display_name)
|
||||
.attr('title', 'Create a new notebook with ' + ks.display_name)
|
||||
);
|
||||
menu.append(ksentry);
|
||||
}
|
||||
this._load_default_kernelspec();
|
||||
};
|
||||
|
||||
NewNotebookWidget.prototype._load_default_kernelspec = function () {
|
||||
/** load default kernelspec name from localStorage, if defined */
|
||||
this.select_kernel(localStorage.default_kernel_name);
|
||||
};
|
||||
|
||||
NewNotebookWidget.prototype.select_kernel = function (kernel_name) {
|
||||
/** select the current default kernel */
|
||||
this.current_selection = kernel_name;
|
||||
var spec = this.kernelspecs[kernel_name];
|
||||
var display_name;
|
||||
if (spec) {
|
||||
display_name = spec.display_name;
|
||||
localStorage.default_kernel_name = kernel_name;
|
||||
} else {
|
||||
display_name = 'default kernel';
|
||||
delete localStorage.default_kernel_name;
|
||||
}
|
||||
this.element.find("#new_notebook").attr('title',
|
||||
'Create a new notebook with ' + display_name
|
||||
);
|
||||
};
|
||||
|
||||
NewNotebookWidget.prototype.new_with_kernel = function (kernel_name) {
|
||||
/** record current selection and open a new notebook */
|
||||
this.select_kernel(kernel_name);
|
||||
this.new_notebook(kernel_name);
|
||||
};
|
||||
|
||||
NewNotebookWidget.prototype.new_notebook = function (kernel_name) {
|
||||
/** create and open a new notebook */
|
||||
var that = this;
|
||||
kernel_name = kernel_name || this.current_selection;
|
||||
var w = window.open();
|
||||
this.contents.new_untitled(that.notebook_path, {type: "notebook"}).then(
|
||||
function (data) {
|
||||
var url = utils.url_join_encode(
|
||||
that.base_url, 'notebooks', data.path
|
||||
);
|
||||
if (kernel_name) {
|
||||
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'}}
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
return {'NewNotebookWidget': NewNotebookWidget};
|
||||
});
|
@ -154,3 +154,9 @@ input.engine_num_input {
|
||||
.file_icon:before {
|
||||
.icon(@fa-var-file-o)
|
||||
}
|
||||
|
||||
ul#new-notebook-menu {
|
||||
// align right instead of left
|
||||
left: auto;
|
||||
right: 0;
|
||||
}
|
@ -43,10 +43,20 @@ data-terminals-available="{{terminals_available}}"
|
||||
</form>
|
||||
</div>
|
||||
<div class="col-sm-4 no-padding tree-buttons">
|
||||
<span id="notebook_buttons" class="pull-right">
|
||||
<button id="new_notebook" title="Create new notebook" class="btn btn-default btn-xs">New Notebook</button>
|
||||
<button id="refresh_notebook_list" title="Refresh notebook list" class="btn btn-default btn-xs"><i class="fa fa-refresh"></i></button>
|
||||
</span>
|
||||
<span id="notebook_buttons" class="pull-right">
|
||||
<div id="new-notebook-buttons" class="btn-group">
|
||||
<button id="new_notebook" class="btn btn-default btn-xs">
|
||||
New Notebook
|
||||
</button>
|
||||
<button class="dropdown-toggle btn btn-default btn-xs" data-toggle="dropdown">
|
||||
<span class="caret"></span>
|
||||
</button>
|
||||
<ul id="new-notebook-menu" class="dropdown-menu"></ul>
|
||||
</div>
|
||||
|
||||
|
||||
<button id="refresh_notebook_list" title="Refresh notebook list" class="btn btn-default btn-xs"><i class="fa fa-refresh"></i></button>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user