From ba26f173137f43390a25335d9dc70e4696a820d5 2014-12-05 20:06:54 From: Min RK Date: 2014-12-05 20:06:54 Subject: [PATCH] Add kernel-select dropdown to new notebook button most recent choice is remembered in localStorage --- diff --git a/IPython/html/static/style/style.min.css b/IPython/html/static/style/style.min.css index d8a6ef6..a2ccd2f 100644 --- a/IPython/html/static/style/style.min.css +++ b/IPython/html/static/style/style.min.css @@ -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 diff --git a/IPython/html/static/tree/js/main.js b/IPython/html/static/tree/js/main.js index 617f5f5..7eb99c9 100644 --- a/IPython/html/static/tree/js/main.js +++ b/IPython/html/static/tree/js/main.js @@ -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, diff --git a/IPython/html/static/tree/js/newnotebook.js b/IPython/html/static/tree/js/newnotebook.js new file mode 100644 index 0000000..ae24c21 --- /dev/null +++ b/IPython/html/static/tree/js/newnotebook.js @@ -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 = $("
  • ").attr("id", "kernel-" +ks.name).append($('') + .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}; +}); diff --git a/IPython/html/static/tree/less/tree.less b/IPython/html/static/tree/less/tree.less index b1d22cf..23cfc31 100644 --- a/IPython/html/static/tree/less/tree.less +++ b/IPython/html/static/tree/less/tree.less @@ -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; +} \ No newline at end of file diff --git a/IPython/html/templates/tree.html b/IPython/html/templates/tree.html index b800b69..cf1765f 100644 --- a/IPython/html/templates/tree.html +++ b/IPython/html/templates/tree.html @@ -43,10 +43,20 @@ data-terminals-available="{{terminals_available}}"
    - - - - + +
    + + + +
    + + + +