From 2168349e9c74004e015f34431c8ba05249fa8507 2014-07-24 23:55:00 From: Thomas Kluyver Date: 2014-07-24 23:55:00 Subject: [PATCH] Use JS events for switching kernelspecs --- diff --git a/IPython/html/static/notebook/js/kernelselector.js b/IPython/html/static/notebook/js/kernelselector.js index ab85176..a2d8db9 100644 --- a/IPython/html/static/notebook/js/kernelselector.js +++ b/IPython/html/static/notebook/js/kernelselector.js @@ -11,12 +11,13 @@ define([ var KernelSelector = function(selector, notebook) { this.selector = selector; this.notebook = notebook; + this.events = notebook.events; this.kernelspecs = {}; if (this.selector !== undefined) { this.element = $(selector); this.request_kernelspecs(); } - + this.bind_events(); // For now, this is how we make this object available elsewhere IPython.kernelselector = this; }; @@ -44,15 +45,17 @@ define([ if (kernel_name === this.notebook.kernel.name) { return; } + var ks = this.kernelspecs[kernel_name]; + this.events.trigger('spec_changed.Kernel', ks); this.notebook.session.delete(); this.notebook.start_session(kernel_name); }; - KernelSelector.prototype.set_displayed_name = function(kernel_name) { - var ks = this.kernelspecs[kernel_name] - if (ks !== undefined) { - this.element.find("#current_kernel_spec").text(ks.display_name); - } + KernelSelector.prototype.bind_events = function() { + var that = this; + this.events.on('spec_changed.Kernel', function(event, data) { + that.element.find("#current_kernel_spec").text(data.display_name); + }); }; return {'KernelSelector': KernelSelector}; diff --git a/IPython/html/static/notebook/js/notebook.js b/IPython/html/static/notebook/js/notebook.js index 496ec41..7879d3e 100644 --- a/IPython/html/static/notebook/js/notebook.js +++ b/IPython/html/static/notebook/js/notebook.js @@ -215,6 +215,13 @@ define([ } }); }); + + this.events.on('spec_changed.Kernel', function(event, data) { + that.set_kernelspec_metadata(data); + if (data.codemirror_mode) { + that.set_codemirror_mode(data.codemirror_mode); + } + }); var collapse_time = function (time) { var app_height = $('#ipython-main-app').height(); // content height @@ -338,8 +345,7 @@ define([ keyboard_manager: this.keyboard_manager}); }; - Notebook.prototype.set_kernelspec_metadata = function(kernel_name) { - var ks = IPython.kernelselector.kernelspecs[kernel_name] + Notebook.prototype.set_kernelspec_metadata = function(ks) { var tostore = {}; for (field in ks) { if (field !== 'argv' && field !== 'env') { @@ -347,11 +353,6 @@ define([ } } this.metadata.kernelspec = tostore; - - // Update the codemirror mode for code cells - if (tostore.codemirror_mode) { - this.set_codemirror_mode(tostore.codemirror_mode); - } } // Cell indexing, retrieval, etc. @@ -1781,10 +1782,10 @@ define([ this.notebook_name = data.name; var trusted = true; - // Set the default codemirror mode before we load the cells - var cm_mode = (this.metadata.kernelspec || {}).codemirror_mode; - if (cm_mode) { - this.set_codemirror_mode(cm_mode); + // Trigger an event changing the kernel spec - this will set the default + // codemirror mode + if (this.metadata.kernelspec !== undefined) { + this.events.trigger('spec_changed.Kernel', this.metadata.kernelspec); } // Only handle 1 worksheet for now. diff --git a/IPython/html/static/services/sessions/js/session.js b/IPython/html/static/services/sessions/js/session.js index 30174b4..f84623d 100644 --- a/IPython/html/static/services/sessions/js/session.js +++ b/IPython/html/static/services/sessions/js/session.js @@ -91,13 +91,17 @@ define([ */ Session.prototype._handle_start_success = function (data, status, xhr) { this.id = data.id; - // If we asked for 'python', the response will have 'python3' or 'python2' - this.kernel_name = data.kernel.name; + // If we asked for 'python', the response will have 'python3' or 'python2'. + // In this case, fire the spec changed event again to update the name + // and highlighting. + if (data.kernel.name !== this.kernel_name) { + this.kernel_name = data.kernel.name; + var ks = IPython.kernelselector.kernelspecs[this.kernel_name]; + this.notebook.events.trigger('spec_changed.Kernel', ks); + } var kernel_service_url = utils.url_path_join(this.base_url, "api/kernels"); this.kernel = new kernel.Kernel(kernel_service_url, this.ws_url, this.notebook, this.kernel_name); this.kernel._kernel_started(data.kernel); - IPython.kernelselector.set_displayed_name(this.kernel_name); - this.notebook.set_kernelspec_metadata(this.kernel_name); }; /**