Show More
@@ -11,6 +11,7 b' define([' | |||
|
11 | 11 | var KernelSelector = function(selector, notebook) { |
|
12 | 12 | this.selector = selector; |
|
13 | 13 | this.notebook = notebook; |
|
14 | this.notebook.set_kernelselector(this); | |
|
14 | 15 | this.events = notebook.events; |
|
15 | 16 | this.current_selection = null; |
|
16 | 17 | this.kernelspecs = {}; |
@@ -21,6 +22,7 b' define([' | |||
|
21 | 22 | this.bind_events(); |
|
22 | 23 | // Make the object globally available for user convenience & inspection |
|
23 | 24 | IPython.kernelselector = this; |
|
25 | Object.seal(this); | |
|
24 | 26 | }; |
|
25 | 27 | |
|
26 | 28 | KernelSelector.prototype.request_kernelspecs = function() { |
@@ -61,10 +63,29 b' define([' | |||
|
61 | 63 | }; |
|
62 | 64 | |
|
63 | 65 | KernelSelector.prototype.change_kernel = function(kernel_name) { |
|
66 | /** | |
|
67 | * TODO, have a methods to set kernel spec directly ? | |
|
68 | **/ | |
|
69 | var that = this; | |
|
64 | 70 | if (kernel_name === this.current_selection) { |
|
65 | 71 | return; |
|
66 | 72 | } |
|
67 | 73 | var ks = this.kernelspecs[kernel_name]; |
|
74 | var new_mode_url = 'kernelspecs/'+ks.name+'/kernel'; | |
|
75 | ||
|
76 | var css_url = this.notebook.base_url+new_mode_url+'.css'; | |
|
77 | $.ajax({ | |
|
78 | type: 'HEAD', | |
|
79 | url: css_url, | |
|
80 | success: function(){ | |
|
81 | $('#kernel-css') | |
|
82 | .attr('href',css_url); | |
|
83 | }, | |
|
84 | error:function(){ | |
|
85 | console.info("No custom kernel.css at URL:", css_url) | |
|
86 | } | |
|
87 | }); | |
|
88 | ||
|
68 | 89 | try { |
|
69 | 90 | this.notebook.start_session(kernel_name); |
|
70 | 91 | } catch (e) { |
@@ -78,8 +99,35 b' define([' | |||
|
78 | 99 | return; |
|
79 | 100 | } |
|
80 | 101 | this.events.trigger('spec_changed.Kernel', ks); |
|
102 | ||
|
103 | ||
|
104 | // load new mode kernel.js if exist | |
|
105 | require([new_mode_url], | |
|
106 | // if new mode has custom.js | |
|
107 | function(new_mode){ | |
|
108 | that.lock_switch(); | |
|
109 | if(new_mode && new_mode.onload){ | |
|
110 | new_mode.onload(); | |
|
111 | } else { | |
|
112 | console.warn("The current kernel defined a kernel.js file but does not contain "+ | |
|
113 | "any asynchronous module definition. This is undefined behavior "+ | |
|
114 | "which is not recommended"); | |
|
115 | } | |
|
116 | }, | |
|
117 | function(err){ | |
|
118 | // if new mode does not have custom.js | |
|
119 | console.info("No custom kernel.css at URL:", new_mode_url) | |
|
120 | } | |
|
121 | ); | |
|
81 | 122 | }; |
|
82 | ||
|
123 | ||
|
124 | KernelSelector.prototype.lock_switch = function() { | |
|
125 | // should set a flag and display warning+reload if user want to | |
|
126 | // re-change kernel. As UI discussion never finish | |
|
127 | // making that a separate PR. | |
|
128 | console.warn('switching kernel is not guaranteed to work !'); | |
|
129 | }; | |
|
130 | ||
|
83 | 131 | KernelSelector.prototype.bind_events = function() { |
|
84 | 132 | var that = this; |
|
85 | 133 | this.events.on('spec_changed.Kernel', function(event, data) { |
@@ -87,7 +135,7 b' define([' | |||
|
87 | 135 | that.element.find("#current_kernel_spec").find('.kernel_name').text(data.display_name); |
|
88 | 136 | that.element.find("#current_kernel_logo").attr("src", "/kernelspecs/"+data.name+"/logo-64x64.png"); |
|
89 | 137 | }); |
|
90 | ||
|
138 | ||
|
91 | 139 | this.events.on('kernel_created.Session', function(event, data) { |
|
92 | 140 | if (data.kernel.name !== that.current_selection) { |
|
93 | 141 | // If we created a 'python' session, we only know if it's Python |
@@ -1745,7 +1745,14 b' define([' | |||
|
1745 | 1745 | // Trigger an event changing the kernel spec - this will set the default |
|
1746 | 1746 | // codemirror mode |
|
1747 | 1747 | if (this.metadata.kernelspec !== undefined) { |
|
1748 | this.events.trigger('spec_changed.Kernel', this.metadata.kernelspec); | |
|
1748 | // TODO shoudl probably not trigger here, | |
|
1749 | // should call the kernel selector, or custom.{js|css} not loaded. | |
|
1750 | if(this.kernel_selector){ | |
|
1751 | // technically not perfect, we should check that the kernelspec matches | |
|
1752 | this.kernel_selector.change_kernel(this.metadata.kernelspec.name); | |
|
1753 | } else { | |
|
1754 | console.log('do not have handle on kernel_selector'); | |
|
1755 | } | |
|
1749 | 1756 | } |
|
1750 | 1757 | |
|
1751 | 1758 | // Set the codemirror mode from language_info metadata |
@@ -2188,6 +2195,10 b' define([' | |||
|
2188 | 2195 | this.events.trigger('notebook_loaded.Notebook'); |
|
2189 | 2196 | }; |
|
2190 | 2197 | |
|
2198 | Notebook.prototype.set_kernelselector = function(k_selector){ | |
|
2199 | this.kernel_selector = k_selector; | |
|
2200 | }; | |
|
2201 | ||
|
2191 | 2202 | /** |
|
2192 | 2203 | * Failure callback for loading a notebook from the server. |
|
2193 | 2204 | * |
@@ -17,6 +17,7 b' window.mathjax_url = "{{mathjax_url}}";' | |||
|
17 | 17 | {{super()}} |
|
18 | 18 | |
|
19 | 19 | <link rel="stylesheet" href="{{ static_url("notebook/css/override.css") }}" type="text/css" /> |
|
20 | <link rel="stylesheet" href="" id='kernel-css' type="text/css" /> | |
|
20 | 21 | |
|
21 | 22 | {% endblock %} |
|
22 | 23 |
@@ -24,6 +24,7 b'' | |||
|
24 | 24 | baseUrl: '{{static_url("", include_version=False)}}', |
|
25 | 25 | paths: { |
|
26 | 26 | nbextensions : '{{ base_url }}nbextensions', |
|
27 | kernelspecs : '{{ base_url }}kernelspecs', | |
|
27 | 28 | underscore : 'components/underscore/underscore-min', |
|
28 | 29 | backbone : 'components/backbone/backbone-min', |
|
29 | 30 | jquery: 'components/jquery/jquery.min', |
General Comments 0
You need to be logged in to leave comments.
Login now