Show More
@@ -1,146 +1,156 b'' | |||
|
1 | 1 | // Copyright (c) IPython Development Team. |
|
2 | 2 | // Distributed under the terms of the Modified BSD License. |
|
3 | 3 | |
|
4 | 4 | define([ |
|
5 | 5 | 'base/js/namespace', |
|
6 | 6 | 'jquery', |
|
7 | 7 | 'base/js/utils', |
|
8 | 8 | ], function(IPython, $, utils) { |
|
9 | 9 | "use strict"; |
|
10 | 10 | |
|
11 | 11 | var KernelSelector = function(selector, notebook) { |
|
12 | 12 | this.selector = selector; |
|
13 | 13 | this.notebook = notebook; |
|
14 | 14 | this.notebook.set_kernelselector(this); |
|
15 | 15 | this.events = notebook.events; |
|
16 | 16 | this.current_selection = null; |
|
17 | 17 | this.kernelspecs = {}; |
|
18 | 18 | if (this.selector !== undefined) { |
|
19 | 19 | this.element = $(selector); |
|
20 | 20 | this.request_kernelspecs(); |
|
21 | 21 | } |
|
22 | 22 | this.bind_events(); |
|
23 | 23 | // Make the object globally available for user convenience & inspection |
|
24 | 24 | IPython.kernelselector = this; |
|
25 | 25 | Object.seal(this); |
|
26 | 26 | }; |
|
27 | 27 | |
|
28 | 28 | KernelSelector.prototype.request_kernelspecs = function() { |
|
29 | 29 | var url = utils.url_join_encode(this.notebook.base_url, 'api/kernelspecs'); |
|
30 | 30 | utils.promising_ajax(url).then($.proxy(this._got_kernelspecs, this)); |
|
31 | 31 | }; |
|
32 | 32 | |
|
33 | 33 | KernelSelector.prototype._got_kernelspecs = function(data) { |
|
34 | 34 | this.kernelspecs = data.kernelspecs; |
|
35 | 35 | var change_kernel_submenu = $("#menu-change-kernel-submenu"); |
|
36 | 36 | var keys = Object.keys(data.kernelspecs).sort(function (a, b) { |
|
37 | 37 | // sort by display_name |
|
38 | 38 | var da = data.kernelspecs[a].spec.display_name; |
|
39 | 39 | var db = data.kernelspecs[b].spec.display_name; |
|
40 | 40 | if (da === db) { |
|
41 | 41 | return 0; |
|
42 | 42 | } else if (da > db) { |
|
43 | 43 | return 1; |
|
44 | 44 | } else { |
|
45 | 45 | return -1; |
|
46 | 46 | } |
|
47 | 47 | }); |
|
48 | 48 | for (var i = 0; i < keys.length; i++) { |
|
49 | 49 | var ks = this.kernelspecs[keys[i]]; |
|
50 | 50 | var ks_submenu_entry = $("<li>").attr("id", "kernel-submenu-"+ks.name).append($('<a>') |
|
51 | 51 | .attr('href', '#') |
|
52 | 52 | .click($.proxy(this.change_kernel, this, ks.name)) |
|
53 | 53 | .text(ks.spec.display_name)); |
|
54 | 54 | change_kernel_submenu.append(ks_submenu_entry); |
|
55 | 55 | } |
|
56 | 56 | }; |
|
57 | ||
|
58 |
KernelSelector.prototype.change |
|
|
59 | /** | |
|
60 | * TODO, have a methods to set kernel spec directly ? | |
|
61 | **/ | |
|
62 |
|
|
|
63 | return; | |
|
57 | ||
|
58 | KernelSelector.prototype._spec_changed = function (event, ks) { | |
|
59 | /** event handler for spec_changed */ | |
|
60 | ||
|
61 | // update selection | |
|
62 | this.current_selection = ks.name; | |
|
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 | 75 | var css_url = ks.resources['kernel.css']; |
|
68 | 76 | if (css_url) { |
|
69 | 77 | $('#kernel-css').attr('href', css_url); |
|
70 | 78 | } else { |
|
71 | 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 | 83 | if (ks.resources['kernel.js']) { |
|
89 | 84 | require([ks.resources['kernel.js']], |
|
90 | 85 | function (kernel_mod) { |
|
91 | 86 | if (kernel_mod && kernel_mod.onload) { |
|
92 | 87 | kernel_mod.onload(); |
|
93 | 88 | } else { |
|
94 | 89 | console.warn("Kernel " + ks.name + " has a kernel.js file that does not contain "+ |
|
95 | 90 | "any asynchronous module definition. This is undefined behavior "+ |
|
96 | 91 | "and not recommended."); |
|
97 | 92 | } |
|
98 | 93 | }, function (err) { |
|
99 | 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 | 125 | KernelSelector.prototype.lock_switch = function() { |
|
107 | 126 | // should set a flag and display warning+reload if user want to |
|
108 | 127 | // re-change kernel. As UI discussion never finish |
|
109 | 128 | // making that a separate PR. |
|
110 | 129 | console.warn('switching kernel is not guaranteed to work !'); |
|
111 | 130 | }; |
|
112 | 131 | |
|
113 | 132 | KernelSelector.prototype.bind_events = function() { |
|
114 | 133 | var that = this; |
|
115 | var logo_img = this.element.find("img.current_kernel_logo"); | |
|
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 | }); | |
|
134 | this.events.on('spec_changed.Kernel', $.proxy(this._spec_changed, this)); | |
|
126 | 135 | |
|
127 | this.events.on('kernel_created.Session', function(event, data) { | |
|
136 | this.events.on('kernel_created.Session', function (event, data) { | |
|
128 | 137 | if (data.kernel.name !== that.current_selection) { |
|
129 | 138 | // If we created a 'python' session, we only know if it's Python |
|
130 | 139 | // 3 or 2 on the server's reply, so we fire the event again to |
|
131 | 140 | // set things up. |
|
132 | 141 | var ks = that.kernelspecs[data.kernel.name]; |
|
133 | 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 | 147 | logo_img.on("load", function() { |
|
138 | 148 | logo_img.show(); |
|
139 | 149 | }); |
|
140 | 150 | logo_img.on("error", function() { |
|
141 | 151 | logo_img.hide(); |
|
142 | 152 | }); |
|
143 | 153 | }; |
|
144 | 154 | |
|
145 | 155 | return {'KernelSelector': KernelSelector}; |
|
146 | 156 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now