diff --git a/IPython/html/static/notebook/js/kernelselector.js b/IPython/html/static/notebook/js/kernelselector.js index 01f6d2a..2ff6e7b 100644 --- a/IPython/html/static/notebook/js/kernelselector.js +++ b/IPython/html/static/notebook/js/kernelselector.js @@ -11,6 +11,7 @@ define([ var KernelSelector = function(selector, notebook) { this.selector = selector; this.notebook = notebook; + this.notebook.set_kernelselector(this); this.events = notebook.events; this.current_selection = null; this.kernelspecs = {}; @@ -21,6 +22,7 @@ define([ this.bind_events(); // Make the object globally available for user convenience & inspection IPython.kernelselector = this; + Object.seal(this); }; KernelSelector.prototype.request_kernelspecs = function() { @@ -61,10 +63,29 @@ define([ }; KernelSelector.prototype.change_kernel = function(kernel_name) { + /** + * TODO, have a methods to set kernel spec directly ? + **/ + var that = this; if (kernel_name === this.current_selection) { return; } var ks = this.kernelspecs[kernel_name]; + var new_mode_url = 'kernelspecs/'+ks.name+'/kernel'; + + var css_url = this.notebook.base_url+new_mode_url+'.css'; + $.ajax({ + type: 'HEAD', + url: css_url, + success: function(){ + $('#kernel-css') + .attr('href',css_url); + }, + error:function(){ + console.info("No custom kernel.css at URL:", css_url) + } + }); + try { this.notebook.start_session(kernel_name); } catch (e) { @@ -78,8 +99,35 @@ define([ return; } this.events.trigger('spec_changed.Kernel', ks); + + + // load new mode kernel.js if exist + require([new_mode_url], + // if new mode has custom.js + function(new_mode){ + that.lock_switch(); + if(new_mode && new_mode.onload){ + new_mode.onload(); + } else { + console.warn("The current kernel defined a kernel.js file but does not contain "+ + "any asynchronous module definition. This is undefined behavior "+ + "which is not recommended"); + } + }, + function(err){ + // if new mode does not have custom.js + console.info("No custom kernel.css at URL:", new_mode_url) + } + ); }; - + + 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 + // making that a separate PR. + console.warn('switching kernel is not guaranteed to work !'); + }; + KernelSelector.prototype.bind_events = function() { var that = this; this.events.on('spec_changed.Kernel', function(event, data) { @@ -87,7 +135,7 @@ define([ that.element.find("#current_kernel_spec").find('.kernel_name').text(data.display_name); that.element.find("#current_kernel_logo").attr("src", "/kernelspecs/"+data.name+"/logo-64x64.png"); }); - + this.events.on('kernel_created.Session', function(event, data) { if (data.kernel.name !== that.current_selection) { // If we created a 'python' session, we only know if it's Python diff --git a/IPython/html/static/notebook/js/notebook.js b/IPython/html/static/notebook/js/notebook.js index e41cb4e..0d9e6f1 100644 --- a/IPython/html/static/notebook/js/notebook.js +++ b/IPython/html/static/notebook/js/notebook.js @@ -1745,7 +1745,14 @@ define([ // 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); + // TODO shoudl probably not trigger here, + // should call the kernel selector, or custom.{js|css} not loaded. + if(this.kernel_selector){ + // technically not perfect, we should check that the kernelspec matches + this.kernel_selector.change_kernel(this.metadata.kernelspec.name); + } else { + console.log('do not have handle on kernel_selector'); + } } // Set the codemirror mode from language_info metadata @@ -2188,6 +2195,10 @@ define([ this.events.trigger('notebook_loaded.Notebook'); }; + Notebook.prototype.set_kernelselector = function(k_selector){ + this.kernel_selector = k_selector; + }; + /** * Failure callback for loading a notebook from the server. * diff --git a/IPython/html/templates/notebook.html b/IPython/html/templates/notebook.html index 5dafad3..bdfe661 100644 --- a/IPython/html/templates/notebook.html +++ b/IPython/html/templates/notebook.html @@ -17,6 +17,7 @@ window.mathjax_url = "{{mathjax_url}}"; {{super()}} + {% endblock %} diff --git a/IPython/html/templates/page.html b/IPython/html/templates/page.html index 542296c..8e2ee8b 100644 --- a/IPython/html/templates/page.html +++ b/IPython/html/templates/page.html @@ -24,6 +24,7 @@ baseUrl: '{{static_url("", include_version=False)}}', paths: { nbextensions : '{{ base_url }}nbextensions', + kernelspecs : '{{ base_url }}kernelspecs', underscore : 'components/underscore/underscore-min', backbone : 'components/backbone/backbone-min', jquery: 'components/jquery/jquery.min',