Show More
@@ -1,146 +1,156 b'' | |||||
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.notebook.set_kernelselector(this); |
|
14 | this.notebook.set_kernelselector(this); | |
15 | this.events = notebook.events; |
|
15 | this.events = notebook.events; | |
16 | this.current_selection = null; |
|
16 | this.current_selection = null; | |
17 | this.kernelspecs = {}; |
|
17 | this.kernelspecs = {}; | |
18 | if (this.selector !== undefined) { |
|
18 | if (this.selector !== undefined) { | |
19 | this.element = $(selector); |
|
19 | this.element = $(selector); | |
20 | this.request_kernelspecs(); |
|
20 | this.request_kernelspecs(); | |
21 | } |
|
21 | } | |
22 | this.bind_events(); |
|
22 | this.bind_events(); | |
23 | // Make the object globally available for user convenience & inspection |
|
23 | // Make the object globally available for user convenience & inspection | |
24 | IPython.kernelselector = this; |
|
24 | IPython.kernelselector = this; | |
25 | Object.seal(this); |
|
25 | Object.seal(this); | |
26 | }; |
|
26 | }; | |
27 |
|
27 | |||
28 | KernelSelector.prototype.request_kernelspecs = function() { |
|
28 | KernelSelector.prototype.request_kernelspecs = function() { | |
29 | var url = utils.url_join_encode(this.notebook.base_url, 'api/kernelspecs'); |
|
29 | var url = utils.url_join_encode(this.notebook.base_url, 'api/kernelspecs'); | |
30 | utils.promising_ajax(url).then($.proxy(this._got_kernelspecs, this)); |
|
30 | utils.promising_ajax(url).then($.proxy(this._got_kernelspecs, this)); | |
31 | }; |
|
31 | }; | |
32 |
|
32 | |||
33 | KernelSelector.prototype._got_kernelspecs = function(data) { |
|
33 | KernelSelector.prototype._got_kernelspecs = function(data) { | |
34 | this.kernelspecs = data.kernelspecs; |
|
34 | this.kernelspecs = data.kernelspecs; | |
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].spec.display_name; |
|
38 | var da = data.kernelspecs[a].spec.display_name; | |
39 | var db = data.kernelspecs[b].spec.display_name; |
|
39 | var db = data.kernelspecs[b].spec.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 ks_submenu_entry = $("<li>").attr("id", "kernel-submenu-"+ks.name).append($('<a>') |
|
50 | var ks_submenu_entry = $("<li>").attr("id", "kernel-submenu-"+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.spec.display_name)); |
|
53 | .text(ks.spec.display_name)); | |
54 | change_kernel_submenu.append(ks_submenu_entry); |
|
54 | change_kernel_submenu.append(ks_submenu_entry); | |
55 | } |
|
55 | } | |
56 | }; |
|
56 | }; | |
57 |
|
57 | |||
58 |
KernelSelector.prototype.change |
|
58 | KernelSelector.prototype._spec_changed = function (event, ks) { | |
59 | /** |
|
59 | /** event handler for spec_changed */ | |
60 | * TODO, have a methods to set kernel spec directly ? |
|
60 | ||
61 | **/ |
|
61 | // update selection | |
62 |
|
|
62 | this.current_selection = ks.name; | |
63 | return; |
|
63 | ||
|
64 | // load logo | |||
|
65 | var logo_img = this.element.find("img.current_kernel_logo"); | |||
|
66 | $("#kernel_indicator").find('.kernel_indicator_name').text(ks.spec.display_name); | |||
|
67 | if (ks.resources['logo-64x64']) { | |||
|
68 | logo_img.attr("src", ks.resources['logo-64x64']); | |||
|
69 | logo_img.show(); | |||
|
70 | } else { | |||
|
71 | logo_img.hide(); | |||
64 | } |
|
72 | } | |
65 | var ks = this.kernelspecs[kernel_name]; |
|
|||
66 |
|
73 | |||
|
74 | // load kernel css | |||
67 | var css_url = ks.resources['kernel.css']; |
|
75 | var css_url = ks.resources['kernel.css']; | |
68 | if (css_url) { |
|
76 | if (css_url) { | |
69 | $('#kernel-css').attr('href', css_url); |
|
77 | $('#kernel-css').attr('href', css_url); | |
70 | } else { |
|
78 | } else { | |
71 | $('#kernel-css').attr('href', ''); |
|
79 | $('#kernel-css').attr('href', ''); | |
72 | } |
|
80 | } | |
73 |
|
||||
74 | try { |
|
|||
75 | this.notebook.start_session(kernel_name); |
|
|||
76 | } catch (e) { |
|
|||
77 | if (e.name === 'SessionAlreadyStarting') { |
|
|||
78 | console.log("Cannot change kernel while waiting for pending session start."); |
|
|||
79 | } else { |
|
|||
80 | // unhandled error |
|
|||
81 | throw e; |
|
|||
82 | } |
|
|||
83 | // only trigger spec_changed if change was successful |
|
|||
84 | return; |
|
|||
85 | } |
|
|||
86 | this.events.trigger('spec_changed.Kernel', ks); |
|
|||
87 |
|
81 | |||
|
82 | // load kernel js | |||
88 | if (ks.resources['kernel.js']) { |
|
83 | if (ks.resources['kernel.js']) { | |
89 | require([ks.resources['kernel.js']], |
|
84 | require([ks.resources['kernel.js']], | |
90 | function (kernel_mod) { |
|
85 | function (kernel_mod) { | |
91 | if (kernel_mod && kernel_mod.onload) { |
|
86 | if (kernel_mod && kernel_mod.onload) { | |
92 | kernel_mod.onload(); |
|
87 | kernel_mod.onload(); | |
93 | } else { |
|
88 | } else { | |
94 | console.warn("Kernel " + ks.name + " has a kernel.js file that does not contain "+ |
|
89 | console.warn("Kernel " + ks.name + " has a kernel.js file that does not contain "+ | |
95 | "any asynchronous module definition. This is undefined behavior "+ |
|
90 | "any asynchronous module definition. This is undefined behavior "+ | |
96 | "and not recommended."); |
|
91 | "and not recommended."); | |
97 | } |
|
92 | } | |
98 | }, function (err) { |
|
93 | }, function (err) { | |
99 | console.warn("Failed to load kernel.js from ", ks.resources['kernel.js'], err); |
|
94 | console.warn("Failed to load kernel.js from ", ks.resources['kernel.js'], err); | |
100 | } |
|
95 | } | |
101 | ); |
|
96 | ); | |
102 | } |
|
97 | } | |
|
98 | }; | |||
103 |
|
99 | |||
|
100 | KernelSelector.prototype.change_kernel = function (kernel_name) { | |||
|
101 | /** | |||
|
102 | * TODO, have a methods to set kernel spec directly ? | |||
|
103 | **/ | |||
|
104 | if (kernel_name === this.current_selection) { | |||
|
105 | return; | |||
|
106 | } | |||
|
107 | var ks = this.kernelspecs[kernel_name]; | |||
|
108 | ||||
|
109 | try { | |||
|
110 | this.notebook.start_session(kernel_name); | |||
|
111 | } catch (e) { | |||
|
112 | if (e.name === 'SessionAlreadyStarting') { | |||
|
113 | console.log("Cannot change kernel while waiting for pending session start."); | |||
|
114 | } else { | |||
|
115 | // unhandled error | |||
|
116 | throw e; | |||
|
117 | } | |||
|
118 | // only trigger spec_changed if change was successful | |||
|
119 | return; | |||
|
120 | } | |||
|
121 | console.log('spec', kernel_name, ks); | |||
|
122 | this.events.trigger('spec_changed.Kernel', ks); | |||
104 | }; |
|
123 | }; | |
105 |
|
124 | |||
106 | KernelSelector.prototype.lock_switch = function() { |
|
125 | KernelSelector.prototype.lock_switch = function() { | |
107 | // should set a flag and display warning+reload if user want to |
|
126 | // should set a flag and display warning+reload if user want to | |
108 | // re-change kernel. As UI discussion never finish |
|
127 | // re-change kernel. As UI discussion never finish | |
109 | // making that a separate PR. |
|
128 | // making that a separate PR. | |
110 | console.warn('switching kernel is not guaranteed to work !'); |
|
129 | console.warn('switching kernel is not guaranteed to work !'); | |
111 | }; |
|
130 | }; | |
112 |
|
131 | |||
113 | KernelSelector.prototype.bind_events = function() { |
|
132 | KernelSelector.prototype.bind_events = function() { | |
114 | var that = this; |
|
133 | var that = this; | |
115 | var logo_img = this.element.find("img.current_kernel_logo"); |
|
134 | this.events.on('spec_changed.Kernel', $.proxy(this._spec_changed, this)); | |
116 | this.events.on('spec_changed.Kernel', function(event, data) { |
|
|||
117 | that.current_selection = data.name; |
|
|||
118 | $("#kernel_indicator").find('.kernel_indicator_name').text(data.spec.display_name); |
|
|||
119 | if (data.resources['logo-64x64']) { |
|
|||
120 | logo_img.attr("src", data.resources['logo-64x64']); |
|
|||
121 | logo_img.show(); |
|
|||
122 | } else { |
|
|||
123 | logo_img.hide(); |
|
|||
124 | } |
|
|||
125 | }); |
|
|||
126 |
|
135 | |||
127 | this.events.on('kernel_created.Session', function(event, data) { |
|
136 | this.events.on('kernel_created.Session', function (event, data) { | |
128 | if (data.kernel.name !== that.current_selection) { |
|
137 | if (data.kernel.name !== that.current_selection) { | |
129 | // If we created a 'python' session, we only know if it's Python |
|
138 | // If we created a 'python' session, we only know if it's Python | |
130 | // 3 or 2 on the server's reply, so we fire the event again to |
|
139 | // 3 or 2 on the server's reply, so we fire the event again to | |
131 | // set things up. |
|
140 | // set things up. | |
132 | var ks = that.kernelspecs[data.kernel.name]; |
|
141 | var ks = that.kernelspecs[data.kernel.name]; | |
133 | that.events.trigger('spec_changed.Kernel', ks); |
|
142 | that.events.trigger('spec_changed.Kernel', ks); | |
134 | } |
|
143 | } | |
135 | }); |
|
144 | }); | |
136 |
|
145 | |||
|
146 | var logo_img = this.element.find("img.current_kernel_logo"); | |||
137 | logo_img.on("load", function() { |
|
147 | logo_img.on("load", function() { | |
138 | logo_img.show(); |
|
148 | logo_img.show(); | |
139 | }); |
|
149 | }); | |
140 | logo_img.on("error", function() { |
|
150 | logo_img.on("error", function() { | |
141 | logo_img.hide(); |
|
151 | logo_img.hide(); | |
142 | }); |
|
152 | }); | |
143 | }; |
|
153 | }; | |
144 |
|
154 | |||
145 | return {'KernelSelector': KernelSelector}; |
|
155 | return {'KernelSelector': KernelSelector}; | |
146 | }); |
|
156 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now