kernelselector.js
62 lines
| 2.1 KiB
| application/javascript
|
JavascriptLexer
Thomas Kluyver
|
r17370 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
define([ | ||||
'base/js/namespace', | ||||
'jquery', | ||||
'base/js/utils', | ||||
], function(IPython, $, utils) { | ||||
"use strict"; | ||||
var KernelSelector = function(selector, notebook) { | ||||
this.selector = selector; | ||||
this.notebook = notebook; | ||||
Thomas Kluyver
|
r17380 | this.events = notebook.events; | ||
Thomas Kluyver
|
r17370 | this.kernelspecs = {}; | ||
if (this.selector !== undefined) { | ||||
this.element = $(selector); | ||||
this.request_kernelspecs(); | ||||
} | ||||
Thomas Kluyver
|
r17380 | this.bind_events(); | ||
Thomas Kluyver
|
r17371 | // For now, this is how we make this object available elsewhere | ||
IPython.kernelselector = this; | ||||
Thomas Kluyver
|
r17370 | }; | ||
KernelSelector.prototype.request_kernelspecs = function() { | ||||
var url = utils.url_join_encode(this.notebook.base_url, 'api/kernelspecs'); | ||||
$.ajax(url, {success: $.proxy(this.got_kernelspecs, this)}); | ||||
}; | ||||
KernelSelector.prototype.got_kernelspecs = function(data, status, xhr) { | ||||
this.kernelspecs = {}; | ||||
var menu = this.element.find("#kernel_selector"); | ||||
for (var i = 0; i < data.length; i++) { | ||||
var ks = data[i]; | ||||
this.kernelspecs[ks.name] = ks; | ||||
var ksentry = $("<li>").attr("id", "kernel-" +ks.name).append($('<a>') | ||||
.attr('href', '#') | ||||
.click($.proxy(this.change_kernel, this, ks.name)) | ||||
.text(ks.display_name)); | ||||
menu.append(ksentry); | ||||
} | ||||
}; | ||||
KernelSelector.prototype.change_kernel = function(kernel_name) { | ||||
Thomas Kluyver
|
r17371 | if (kernel_name === this.notebook.kernel.name) { | ||
Thomas Kluyver
|
r17370 | return; | ||
} | ||||
Thomas Kluyver
|
r17380 | var ks = this.kernelspecs[kernel_name]; | ||
this.events.trigger('spec_changed.Kernel', ks); | ||||
Thomas Kluyver
|
r17370 | this.notebook.session.delete(); | ||
this.notebook.start_session(kernel_name); | ||||
}; | ||||
Thomas Kluyver
|
r17380 | KernelSelector.prototype.bind_events = function() { | ||
var that = this; | ||||
this.events.on('spec_changed.Kernel', function(event, data) { | ||||
that.element.find("#current_kernel_spec").text(data.display_name); | ||||
}); | ||||
Thomas Kluyver
|
r17371 | }; | ||
Thomas Kluyver
|
r17370 | return {'KernelSelector': KernelSelector}; | ||
}); | ||||