Show More
@@ -25,6 +25,7 b' define([' | |||
|
25 | 25 | // Make the object globally available for user convenience & inspection |
|
26 | 26 | IPython.kernelselector = this; |
|
27 | 27 | this._finish_load = null; |
|
28 | this._loaded = false; | |
|
28 | 29 | this.loaded = new Promise(function(resolve, reject) { |
|
29 | 30 | that._finish_load = resolve; |
|
30 | 31 | }); |
@@ -83,6 +84,7 b' define([' | |||
|
83 | 84 | |
|
84 | 85 | }); |
|
85 | 86 | // trigger loaded promise |
|
87 | this._loaded = true; | |
|
86 | 88 | this._finish_load(); |
|
87 | 89 | }; |
|
88 | 90 | |
@@ -154,28 +156,86 b' define([' | |||
|
154 | 156 | } |
|
155 | 157 | }; |
|
156 | 158 | |
|
157 |
KernelSelector.prototype.set_kernel = function ( |
|
|
158 |
/** set the kernel by name, ensuring kernelspecs have been loaded, first |
|
|
159 | KernelSelector.prototype.set_kernel = function (selected) { | |
|
160 | /** set the kernel by name, ensuring kernelspecs have been loaded, first | |
|
161 | ||
|
162 | kernel can be just a kernel name, or a notebook kernelspec metadata | |
|
163 | (name, project_name, display_name). | |
|
164 | */ | |
|
159 | 165 | var that = this; |
|
160 | return this.loaded.then(function () { | |
|
161 | that._set_kernel(kernel_name); | |
|
162 | }); | |
|
166 | if (typeof selected === 'string') { | |
|
167 | selected = { | |
|
168 | name: selected | |
|
169 | }; | |
|
170 | } | |
|
171 | if (this._loaded) { | |
|
172 | this._set_kernel(selected); | |
|
173 | } else { | |
|
174 | return this.loaded.then(function () { | |
|
175 | that._set_kernel(selected); | |
|
176 | }); | |
|
177 | } | |
|
163 | 178 | }; |
|
164 | 179 | |
|
165 |
KernelSelector.prototype._set_kernel = function ( |
|
|
180 | KernelSelector.prototype._set_kernel = function (selected) { | |
|
166 | 181 | /** Actually set the kernel (kernelspecs have been loaded) */ |
|
167 |
if ( |
|
|
182 | if (selected.name === this.current_selection) { | |
|
168 | 183 | // only trigger event if value changed |
|
169 | 184 | return; |
|
170 | 185 | } |
|
171 |
var ks = this.kernelspecs[ |
|
|
186 | var ks = this.kernelspecs[selected.name]; | |
|
187 | if (ks === undefined) { | |
|
188 | if (selected.project_name && selected.project_name.length > 0) { | |
|
189 | $.map(this.kernelspecs, function (k) { | |
|
190 | if (k.spec.project_name === selected.project_name) { | |
|
191 | ks = k; | |
|
192 | } | |
|
193 | }); | |
|
194 | } | |
|
195 | // if still undefined, trigger failure event | |
|
196 | if (ks === undefined) { | |
|
197 | this.events.trigger("spec_not_found.Kernel", selected); | |
|
198 | return; | |
|
199 | } | |
|
200 | } | |
|
172 | 201 | if (this.notebook._session_starting) { |
|
173 | 202 | console.error("Cannot change kernel while waiting for pending session start."); |
|
174 | 203 | return; |
|
175 | 204 | } |
|
176 |
this.current_selection = k |
|
|
205 | this.current_selection = ks.name; | |
|
177 | 206 | this.events.trigger('spec_changed.Kernel', ks); |
|
178 | 207 | }; |
|
208 | ||
|
209 | KernelSelector.prototype._spec_not_found = function (event, data) { | |
|
210 | var that = this; | |
|
211 | var select = $("<select>").addClass('form-control'); | |
|
212 | console.warn("Kernelspec not found:", data); | |
|
213 | $.map(this.kernelspecs, function (ks) { | |
|
214 | select.append( | |
|
215 | $('<option/>').attr('value', ks.name).text(ks.spec.display_name) | |
|
216 | ); | |
|
217 | }); | |
|
218 | ||
|
219 | var body = $("<form>").addClass("form-inline").append( | |
|
220 | $("<span>").text( | |
|
221 | "I couldn't find a kernel matching " + (data.display_name || data.name) + "." + | |
|
222 | " Please select a kernel:" | |
|
223 | ) | |
|
224 | ).append(select); | |
|
225 | ||
|
226 | dialog.modal({ | |
|
227 | title : 'Kernel not found', | |
|
228 | body : body, | |
|
229 | buttons : { | |
|
230 | OK : { | |
|
231 | class : 'btn-primary', | |
|
232 | click : function () { | |
|
233 | that.set_kernel(select.val()); | |
|
234 | } | |
|
235 | } | |
|
236 | } | |
|
237 | }); | |
|
238 | }; | |
|
179 | 239 | |
|
180 | 240 | KernelSelector.prototype.new_notebook = function (kernel_name) { |
|
181 | 241 | |
@@ -213,7 +273,7 b' define([' | |||
|
213 | 273 | KernelSelector.prototype.bind_events = function() { |
|
214 | 274 | var that = this; |
|
215 | 275 | this.events.on('spec_changed.Kernel', $.proxy(this._spec_changed, this)); |
|
216 | ||
|
276 | this.events.on('spec_not_found.Kernel', $.proxy(this._spec_not_found, this)); | |
|
217 | 277 | this.events.on('kernel_created.Session', function (event, data) { |
|
218 | 278 | that.set_kernel(data.kernel.name); |
|
219 | 279 | }); |
@@ -2192,18 +2192,14 b' define([' | |||
|
2192 | 2192 | } |
|
2193 | 2193 | |
|
2194 | 2194 | if (this.session === null) { |
|
2195 | var kernel_name; | |
|
2196 | if (this.metadata.kernelspec) { | |
|
2197 | var kernelspec = this.metadata.kernelspec || {}; | |
|
2198 | kernel_name = kernelspec.name; | |
|
2199 | } else { | |
|
2200 | kernel_name = utils.get_url_param('kernel_name'); | |
|
2201 | } | |
|
2195 | var kernel_name = utils.get_url_param('kernel_name'); | |
|
2202 | 2196 | if (kernel_name) { |
|
2203 | // setting kernel_name here triggers start_session | |
|
2204 | 2197 | this.kernel_selector.set_kernel(kernel_name); |
|
2198 | } else if (this.metadata.kernelspec) { | |
|
2199 | this.kernel_selector.set_kernel(this.metadata.kernelspec); | |
|
2205 | 2200 | } else { |
|
2206 | // start a new session with the server's default kernel | |
|
2201 | // setting kernel via set_kernel above triggers start_session, | |
|
2202 | // otherwise start a new session with the server's default kernel | |
|
2207 | 2203 | // spec_changed events will fire after kernel is loaded |
|
2208 | 2204 | this.start_session(); |
|
2209 | 2205 | } |
General Comments 0
You need to be logged in to leave comments.
Login now