From 95790be9560cb5f5ba1bc15917aeb223727ca93a Mon Sep 17 00:00:00 2001 From: Paul Ivanov Date: Thu, 19 Jan 2017 15:50:02 -0800 Subject: [PATCH] kernel halt confirmation dialog --- notebook/static/notebook/js/actions.js | 14 +++++++ notebook/static/notebook/js/menubar.js | 1 + notebook/static/notebook/js/notebook.js | 33 +++++++++++++-- notebook/static/services/kernels/kernel.js | 3 +- notebook/static/services/sessions/session.js | 5 ++- notebook/templates/notebook.html | 6 ++- notebook/tests/notebook/kernel_menu.js | 44 ++++++++++++++++++++ 7 files changed, 98 insertions(+), 8 deletions(-) create mode 100644 notebook/tests/notebook/kernel_menu.js diff --git a/notebook/static/notebook/js/actions.js b/notebook/static/notebook/js/actions.js index 69e3a5356..67874e793 100644 --- a/notebook/static/notebook/js/actions.js +++ b/notebook/static/notebook/js/actions.js @@ -75,6 +75,20 @@ define(function(require){ env.notebook.show_shortcuts_editor(); } }, + 'halt-kernel': { + help: 'Halt the kernel (no confirmation dialog)', + handler: function (env) { + env.notebook.halt_kernel({confirm: false}); + } + }, + 'confirm-halt-kernel':{ + icon: 'fa-repeat', + help_index : 'hb', + help: 'Shutdown the kernel (with confirmation dialog)', + handler : function (env) { + env.notebook.halt_kernel(); + } + }, 'restart-kernel': { help: 'restart the kernel (no confirmation dialog)', handler: function (env) { diff --git a/notebook/static/notebook/js/menubar.js b/notebook/static/notebook/js/menubar.js index 84537fd8c..1b4ebb05e 100644 --- a/notebook/static/notebook/js/menubar.js +++ b/notebook/static/notebook/js/menubar.js @@ -250,6 +250,7 @@ define([ '#rename_notebook' : 'rename-notebook', '#find_and_replace' : 'find-and-replace', '#save_checkpoint': 'save-notebook', + '#halt_kernel': 'confirm-halt-kernel', '#restart_kernel': 'confirm-restart-kernel', '#restart_clear_output': 'confirm-restart-kernel-and-clear-output', '#restart_run_all': 'confirm-restart-kernel-and-run-all-cells', diff --git a/notebook/static/notebook/js/notebook.js b/notebook/static/notebook/js/notebook.js index a0b019c07..019e08b3e 100644 --- a/notebook/static/notebook/js/notebook.js +++ b/notebook/static/notebook/js/notebook.js @@ -2262,6 +2262,28 @@ define([ * Prompt the user to restart the kernel. * if options.confirm === false, no confirmation dialog is shown. */ + Notebook.prototype.halt_kernel = function (options) { + var that = this; + var halt_options = {}; + halt_options.confirm = (options || {}).confirm; + halt_options.dialog = { + title : "Halt kernel?", + body : $("

").text( + 'Do you want to halt the current kernel? All variables will be lost.' + ), + buttons : { + "Halt" : { + "class" : "btn-danger", + "click" : function () {}, + }, + } + }; + halt_options.kernel_action = function() { + that.session.delete(); + }; + return this._restart_kernel(halt_options); + }; + Notebook.prototype.restart_kernel = function (options) { var that = this; var restart_options = {}; @@ -2297,11 +2319,14 @@ define([ that.events.one('kernel_ready.Kernel', resolve_promise); }, reject_promise); } - - if (options.confirm === false) { + + var do_kernel_action = options.kernel_action || restart_and_resolve; + + // no need to confirm if the kernel is not connected + if (options.confirm === false || !that.kernel.is_connected()) { var default_button = options.dialog.buttons[Object.keys(options.dialog.buttons)[0]]; promise.then(default_button.click); - restart_and_resolve(); + do_kernel_action(); return promise; } options.dialog.notebook = this; @@ -2316,7 +2341,7 @@ define([ var click = button.click; button.click = function () { promise.then(click); - restart_and_resolve(); + do_kernel_action(); }; }); options.dialog.buttons = buttons; diff --git a/notebook/static/services/kernels/kernel.js b/notebook/static/services/kernels/kernel.js index 4e1cbd1c0..482d699bf 100644 --- a/notebook/static/services/kernels/kernel.js +++ b/notebook/static/services/kernels/kernel.js @@ -317,8 +317,7 @@ define([ }; var on_error = function (xhr, status, err) { - that.events.trigger('kernel_dead.Kernel', {kernel: that}); - that._kernel_dead(); + that.events.trigger('kernel_failed_restart.Kernel', {kernel: that}); if (error) { error(xhr, status, err); } diff --git a/notebook/static/services/sessions/session.js b/notebook/static/services/sessions/session.js index 440097a12..2243de6da 100644 --- a/notebook/static/services/sessions/session.js +++ b/notebook/static/services/sessions/session.js @@ -62,6 +62,9 @@ define([ this.events.on('kernel_dead.Kernel', function () { that.delete(); }); + this.events.on('kernel_failed_restart.Kernel', function () { + that.notebook.start_session(); + }); }; @@ -188,7 +191,7 @@ define([ * @param {function} [error] - functon executed on ajax error */ Session.prototype.delete = function (success, error) { - if (this.kernel) { + if (this.kernel && this.kernel.is_connected()) { this.events.trigger('kernel_killed.Session', {session: this, kernel: this.kernel}); this.kernel._kernel_dead(); } diff --git a/notebook/templates/notebook.html b/notebook/templates/notebook.html index 3dbfc393f..47836fa09 100644 --- a/notebook/templates/notebook.html +++ b/notebook/templates/notebook.html @@ -254,7 +254,7 @@ data-notebook-path="{{notebook_path | urlencode}}" title="Send KeyboardInterrupt (CTRL-C) to the Kernel"> Interrupt -

  • Restart
  • @@ -270,6 +270,10 @@ data-notebook-path="{{notebook_path | urlencode}}" title="Reconnect to the Kernel"> Reconnect +
  • + Shutdown +