Show More
@@ -1,156 +1,158 | |||||
1 | // Copyright (c) IPython Development Team. |
|
1 | // Copyright (c) IPython Development Team. | |
2 | // Distributed under the terms of the Modified BSD License. |
|
2 | // Distributed under the terms of the Modified BSD License. | |
3 |
|
3 | |||
4 | define([ |
|
4 | define([ | |
5 | 'base/js/namespace', |
|
5 | 'base/js/namespace', | |
6 | 'jquery', |
|
6 | 'jquery', | |
7 | 'base/js/utils', |
|
7 | 'base/js/utils', | |
8 | ], function(IPython, $, utils) { |
|
8 | ], function(IPython, $, utils) { | |
9 | "use strict"; |
|
9 | "use strict"; | |
10 |
|
10 | |||
11 | var KernelSelector = function(selector, notebook) { |
|
11 | var KernelSelector = function(selector, notebook) { | |
12 | this.selector = selector; |
|
12 | this.selector = selector; | |
13 | this.notebook = notebook; |
|
13 | this.notebook = notebook; | |
14 | this.events = notebook.events; |
|
14 | this.events = notebook.events; | |
15 | this.current_selection = null; |
|
15 | this.current_selection = null; | |
16 | this.kernelspecs = {}; |
|
16 | this.kernelspecs = {}; | |
17 | if (this.selector !== undefined) { |
|
17 | if (this.selector !== undefined) { | |
18 | this.element = $(selector); |
|
18 | this.element = $(selector); | |
19 | this.request_kernelspecs(); |
|
19 | this.request_kernelspecs(); | |
20 | } |
|
20 | } | |
21 | this.bind_events(); |
|
21 | this.bind_events(); | |
22 | // Make the object globally available for user convenience & inspection |
|
22 | // Make the object globally available for user convenience & inspection | |
23 | IPython.kernelselector = this; |
|
23 | IPython.kernelselector = this; | |
24 | Object.seal(this); |
|
24 | Object.seal(this); | |
25 | }; |
|
25 | }; | |
26 |
|
26 | |||
27 | KernelSelector.prototype.request_kernelspecs = function() { |
|
27 | KernelSelector.prototype.request_kernelspecs = function() { | |
28 | var url = utils.url_join_encode(this.notebook.base_url, 'api/kernelspecs'); |
|
28 | var url = utils.url_join_encode(this.notebook.base_url, 'api/kernelspecs'); | |
29 | utils.promising_ajax(url).then($.proxy(this._got_kernelspecs, this)); |
|
29 | utils.promising_ajax(url).then($.proxy(this._got_kernelspecs, this)); | |
30 | }; |
|
30 | }; | |
31 |
|
31 | |||
32 | KernelSelector.prototype._got_kernelspecs = function(data) { |
|
32 | KernelSelector.prototype._got_kernelspecs = function(data) { | |
33 | this.kernelspecs = data.kernelspecs; |
|
33 | this.kernelspecs = data.kernelspecs; | |
34 | var menu = this.element.find("#kernel_selector"); |
|
34 | var menu = this.element.find("#kernel_selector"); | |
35 | var change_kernel_submenu = $("#menu-change-kernel-submenu"); |
|
35 | var change_kernel_submenu = $("#menu-change-kernel-submenu"); | |
36 | var keys = Object.keys(data.kernelspecs).sort(function (a, b) { |
|
36 | var keys = Object.keys(data.kernelspecs).sort(function (a, b) { | |
37 | // sort by display_name |
|
37 | // sort by display_name | |
38 | var da = data.kernelspecs[a].display_name; |
|
38 | var da = data.kernelspecs[a].display_name; | |
39 | var db = data.kernelspecs[b].display_name; |
|
39 | var db = data.kernelspecs[b].display_name; | |
40 | if (da === db) { |
|
40 | if (da === db) { | |
41 | return 0; |
|
41 | return 0; | |
42 | } else if (da > db) { |
|
42 | } else if (da > db) { | |
43 | return 1; |
|
43 | return 1; | |
44 | } else { |
|
44 | } else { | |
45 | return -1; |
|
45 | return -1; | |
46 | } |
|
46 | } | |
47 | }); |
|
47 | }); | |
48 | for (var i = 0; i < keys.length; i++) { |
|
48 | for (var i = 0; i < keys.length; i++) { | |
49 | var ks = this.kernelspecs[keys[i]]; |
|
49 | var ks = this.kernelspecs[keys[i]]; | |
50 | var ksentry = $("<li>").attr("id", "kernel-" +ks.name).append($('<a>') |
|
50 | var ksentry = $("<li>").attr("id", "kernel-" +ks.name).append($('<a>') | |
51 | .attr('href', '#') |
|
51 | .attr('href', '#') | |
52 | .click($.proxy(this.change_kernel, this, ks.name)) |
|
52 | .click($.proxy(this.change_kernel, this, ks.name)) | |
53 | .text(ks.display_name)); |
|
53 | .text(ks.display_name)); | |
54 | menu.append(ksentry); |
|
54 | menu.append(ksentry); | |
55 |
|
55 | |||
56 | var ks_submenu_entry = $("<li>").attr("id", "kernel-submenu-"+ks.name).append($('<a>') |
|
56 | var ks_submenu_entry = $("<li>").attr("id", "kernel-submenu-"+ks.name).append($('<a>') | |
57 | .attr('href', '#') |
|
57 | .attr('href', '#') | |
58 | .click($.proxy(this.change_kernel, this, ks.name)) |
|
58 | .click($.proxy(this.change_kernel, this, ks.name)) | |
59 | .text(ks.display_name)); |
|
59 | .text(ks.display_name)); | |
60 | change_kernel_submenu.append(ks_submenu_entry); |
|
60 | change_kernel_submenu.append(ks_submenu_entry); | |
61 | } |
|
61 | } | |
62 | }; |
|
62 | }; | |
63 |
|
63 | |||
64 | KernelSelector.prototype.change_kernel = function(kernel_name) { |
|
64 | KernelSelector.prototype.change_kernel = function(kernel_name) { | |
65 | /** |
|
65 | /** | |
66 | * TODO, have a methods to set kernel spec directly ? |
|
66 | * TODO, have a methods to set kernel spec directly ? | |
67 | **/ |
|
67 | **/ | |
68 | var that = this; |
|
68 | var that = this; | |
69 | if (kernel_name === this.current_selection) { |
|
69 | if (kernel_name === this.current_selection) { | |
70 | return; |
|
70 | return; | |
71 | } |
|
71 | } | |
72 | var ks = this.kernelspecs[kernel_name]; |
|
72 | var ks = this.kernelspecs[kernel_name]; | |
73 | var new_mode_url = 'kernelspecs/'+ks.name+'/kernel'; |
|
73 | var new_mode_url = 'kernelspecs/'+ks.name+'/kernel'; | |
74 |
|
74 | |||
75 | var css_url = this.notebook.base_url+new_mode_url+'.css'; |
|
75 | var css_url = this.notebook.base_url+new_mode_url+'.css'; | |
76 | $.ajax({ |
|
76 | $.ajax({ | |
77 | type: 'HEAD', |
|
77 | type: 'HEAD', | |
78 | url: css_url, |
|
78 | url: css_url, | |
79 | success: function(){ |
|
79 | success: function(){ | |
80 | $('#kernel-css') |
|
80 | $('#kernel-css') | |
81 | .attr('href',css_url); |
|
81 | .attr('href',css_url); | |
82 | }, |
|
82 | }, | |
83 | error:function(){ |
|
83 | error:function(){ | |
84 | console.warn(new_mode_url+' does not provide custom URL, you might see a 404 error that should not prevent '+ |
|
84 | console.info("No custom kernel.css at URL:", css_url) | |
85 | ' the Jupyter notebook from working :' ,css_url ); |
|
|||
86 | } |
|
85 | } | |
87 | }); |
|
86 | }); | |
88 |
|
87 | |||
89 | try { |
|
88 | try { | |
90 | this.notebook.start_session(kernel_name); |
|
89 | this.notebook.start_session(kernel_name); | |
91 | } catch (e) { |
|
90 | } catch (e) { | |
92 | if (e.name === 'SessionAlreadyStarting') { |
|
91 | if (e.name === 'SessionAlreadyStarting') { | |
93 | console.log("Cannot change kernel while waiting for pending session start."); |
|
92 | console.log("Cannot change kernel while waiting for pending session start."); | |
94 | } else { |
|
93 | } else { | |
95 | // unhandled error |
|
94 | // unhandled error | |
96 | throw e; |
|
95 | throw e; | |
97 | } |
|
96 | } | |
98 | // only trigger spec_changed if change was successful |
|
97 | // only trigger spec_changed if change was successful | |
99 | return; |
|
98 | return; | |
100 | } |
|
99 | } | |
101 | this.events.trigger('spec_changed.Kernel', ks); |
|
100 | this.events.trigger('spec_changed.Kernel', ks); | |
102 |
|
101 | |||
103 |
|
102 | |||
104 | // load new mode kernel.js if exist |
|
103 | // load new mode kernel.js if exist | |
105 | require([new_mode_url], |
|
104 | require([new_mode_url], | |
106 | // if new mode has custom.js |
|
105 | // if new mode has custom.js | |
107 | function(new_mode){ |
|
106 | function(new_mode){ | |
108 | that.lock_switch(); |
|
107 | that.lock_switch(); | |
109 | if(new_mode && new_mode.onload){ |
|
108 | if(new_mode && new_mode.onload){ | |
110 | new_mode.onload(); |
|
109 | new_mode.onload(); | |
111 | } else { |
|
110 | } else { | |
112 | console.warn("The current kernel defined a kernel.js file but does not contain"+ |
|
111 | console.warn("The current kernel defined a kernel.js file but does not contain "+ | |
113 | "any asynchronous module definition. This is undefined behavior"+ |
|
112 | "any asynchronous module definition. This is undefined behavior "+ | |
114 | "which is not recommended"); |
|
113 | "which is not recommended"); | |
115 | } |
|
114 | } | |
116 | }, |
|
115 | }, | |
117 | function(err){ |
|
116 | function(err){ | |
118 | // if new mode does not have custom.js |
|
117 | // if new mode does not have custom.js | |
119 | console.warn('Any above 404 on '+new_mode_url+'.js is normal'); |
|
118 | console.info("No custom kernel.css at URL:", new_mode_url) | |
120 | } |
|
119 | } | |
121 | ); |
|
120 | ); | |
122 | }; |
|
121 | }; | |
123 |
|
122 | |||
124 | KernelSelector.prototype.lock_switch = function() { |
|
123 | KernelSelector.prototype.lock_switch = function() { | |
|
124 | // should set a flag and display warning+reload if user want to | |||
|
125 | // re-change kernel. As UI discussion never finish | |||
|
126 | // making that a separate PR. | |||
125 | console.warn('switching kernel is not guaranteed to work !'); |
|
127 | console.warn('switching kernel is not guaranteed to work !'); | |
126 | }; |
|
128 | }; | |
127 |
|
129 | |||
128 | KernelSelector.prototype.bind_events = function() { |
|
130 | KernelSelector.prototype.bind_events = function() { | |
129 | var that = this; |
|
131 | var that = this; | |
130 | this.events.on('spec_changed.Kernel', function(event, data) { |
|
132 | this.events.on('spec_changed.Kernel', function(event, data) { | |
131 | that.current_selection = data.name; |
|
133 | that.current_selection = data.name; | |
132 | that.element.find("#current_kernel_spec").find('.kernel_name').text(data.display_name); |
|
134 | that.element.find("#current_kernel_spec").find('.kernel_name').text(data.display_name); | |
133 | that.element.find("#current_kernel_logo").attr("src", "/kernelspecs/"+data.name+"/logo-64x64.png"); |
|
135 | that.element.find("#current_kernel_logo").attr("src", "/kernelspecs/"+data.name+"/logo-64x64.png"); | |
134 | }); |
|
136 | }); | |
135 |
|
137 | |||
136 | this.events.on('kernel_created.Session', function(event, data) { |
|
138 | this.events.on('kernel_created.Session', function(event, data) { | |
137 | if (data.kernel.name !== that.current_selection) { |
|
139 | if (data.kernel.name !== that.current_selection) { | |
138 | // If we created a 'python' session, we only know if it's Python |
|
140 | // If we created a 'python' session, we only know if it's Python | |
139 | // 3 or 2 on the server's reply, so we fire the event again to |
|
141 | // 3 or 2 on the server's reply, so we fire the event again to | |
140 | // set things up. |
|
142 | // set things up. | |
141 | var ks = that.kernelspecs[data.kernel.name]; |
|
143 | var ks = that.kernelspecs[data.kernel.name]; | |
142 | that.events.trigger('spec_changed.Kernel', ks); |
|
144 | that.events.trigger('spec_changed.Kernel', ks); | |
143 | } |
|
145 | } | |
144 | }); |
|
146 | }); | |
145 |
|
147 | |||
146 | var logo_img = this.element.find("#current_kernel_logo") |
|
148 | var logo_img = this.element.find("#current_kernel_logo") | |
147 | logo_img.on("load", function() { |
|
149 | logo_img.on("load", function() { | |
148 | logo_img.show(); |
|
150 | logo_img.show(); | |
149 | }); |
|
151 | }); | |
150 | logo_img.on("error", function() { |
|
152 | logo_img.on("error", function() { | |
151 | logo_img.hide(); |
|
153 | logo_img.hide(); | |
152 | }); |
|
154 | }); | |
153 | }; |
|
155 | }; | |
154 |
|
156 | |||
155 | return {'KernelSelector': KernelSelector}; |
|
157 | return {'KernelSelector': KernelSelector}; | |
156 | }); |
|
158 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now