##// END OF EJS Templates
kernelspecs is a dict...
Min RK -
Show More
@@ -7,8 +7,6 b' from tornado import web'
7
7
8 from ...base.handlers import IPythonHandler, json_errors
8 from ...base.handlers import IPythonHandler, json_errors
9
9
10 from IPython.kernel.kernelspec import _pythonfirst
11
12
10
13 class MainKernelSpecHandler(IPythonHandler):
11 class MainKernelSpecHandler(IPythonHandler):
14 SUPPORTED_METHODS = ('GET',)
12 SUPPORTED_METHODS = ('GET',)
@@ -17,18 +15,21 b' class MainKernelSpecHandler(IPythonHandler):'
17 @json_errors
15 @json_errors
18 def get(self):
16 def get(self):
19 ksm = self.kernel_spec_manager
17 ksm = self.kernel_spec_manager
20 results = []
18 km = self.kernel_manager
21 for kernel_name in sorted(ksm.find_kernel_specs(), key=_pythonfirst):
19 model = {}
20 model['default'] = km.default_kernel_name
21 model['kernelspecs'] = specs = {}
22 for kernel_name in ksm.find_kernel_specs():
22 try:
23 try:
23 d = ksm.get_kernel_spec(kernel_name).to_dict()
24 d = ksm.get_kernel_spec(kernel_name).to_dict()
24 except Exception:
25 except Exception:
25 self.log.error("Failed to load kernel spec: '%s'", kernel_name, exc_info=True)
26 self.log.error("Failed to load kernel spec: '%s'", kernel_name, exc_info=True)
26 continue
27 continue
27 d['name'] = kernel_name
28 d['name'] = kernel_name
28 results.append(d)
29 specs[kernel_name] = d
29
30
30 self.set_header("Content-Type", 'application/json')
31 self.set_header("Content-Type", 'application/json')
31 self.finish(json.dumps(results))
32 self.finish(json.dumps(model))
32
33
33
34
34 class KernelSpecHandler(IPythonHandler):
35 class KernelSpecHandler(IPythonHandler):
@@ -78,16 +78,22 b' class APITest(NotebookTestBase):'
78 with open(pjoin(bad_kernel_dir, 'kernel.json'), 'w') as f:
78 with open(pjoin(bad_kernel_dir, 'kernel.json'), 'w') as f:
79 f.write("garbage")
79 f.write("garbage")
80
80
81 specs = self.ks_api.list().json()
81 model = self.ks_api.list().json()
82 assert isinstance(specs, list)
82 assert isinstance(model, dict)
83 self.assertEqual(model['default'], NATIVE_KERNEL_NAME)
84 specs = model['kernelspecs']
85 assert isinstance(specs, dict)
83 # 2: the sample kernelspec created in setUp, and the native Python kernel
86 # 2: the sample kernelspec created in setUp, and the native Python kernel
84 self.assertGreaterEqual(len(specs), 2)
87 self.assertGreaterEqual(len(specs), 2)
85
88
86 shutil.rmtree(bad_kernel_dir)
89 shutil.rmtree(bad_kernel_dir)
87
90
88 def test_list_kernelspecs(self):
91 def test_list_kernelspecs(self):
89 specs = self.ks_api.list().json()
92 model = self.ks_api.list().json()
90 assert isinstance(specs, list)
93 assert isinstance(model, dict)
94 self.assertEqual(model['default'], NATIVE_KERNEL_NAME)
95 specs = model['kernelspecs']
96 assert isinstance(specs, dict)
91
97
92 # 2: the sample kernelspec created in setUp, and the native Python kernel
98 # 2: the sample kernelspec created in setUp, and the native Python kernel
93 self.assertGreaterEqual(len(specs), 2)
99 self.assertGreaterEqual(len(specs), 2)
@@ -98,8 +104,8 b' class APITest(NotebookTestBase):'
98 def is_default_kernelspec(s):
104 def is_default_kernelspec(s):
99 return s['name'] == NATIVE_KERNEL_NAME and s['display_name'].startswith("IPython")
105 return s['name'] == NATIVE_KERNEL_NAME and s['display_name'].startswith("IPython")
100
106
101 assert any(is_sample_kernelspec(s) for s in specs), specs
107 assert any(is_sample_kernelspec(s) for s in specs.values()), specs
102 assert any(is_default_kernelspec(s) for s in specs), specs
108 assert any(is_default_kernelspec(s) for s in specs.values()), specs
103
109
104 def test_get_kernelspec(self):
110 def test_get_kernelspec(self):
105 spec = self.ks_api.kernel_spec_info('Sample').json() # Case insensitive
111 spec = self.ks_api.kernel_spec_info('Sample').json() # Case insensitive
@@ -25,16 +25,27 b' define(['
25
25
26 KernelSelector.prototype.request_kernelspecs = function() {
26 KernelSelector.prototype.request_kernelspecs = function() {
27 var url = utils.url_join_encode(this.notebook.base_url, 'api/kernelspecs');
27 var url = utils.url_join_encode(this.notebook.base_url, 'api/kernelspecs');
28 $.ajax(url, {success: $.proxy(this._got_kernelspecs, this)});
28 utils.promising_ajax(url).then($.proxy(this._got_kernelspecs, this));
29 };
29 };
30
30
31 KernelSelector.prototype._got_kernelspecs = function(data, status, xhr) {
31 KernelSelector.prototype._got_kernelspecs = function(data) {
32 this.kernelspecs = {};
32 this.kernelspecs = data.kernelspecs;
33 var menu = this.element.find("#kernel_selector");
33 var menu = this.element.find("#kernel_selector");
34 var change_kernel_submenu = $("#menu-change-kernel-submenu");
34 var change_kernel_submenu = $("#menu-change-kernel-submenu");
35 for (var i = 0; i < data.length; i++) {
35 var keys = Object.keys(data.kernelspecs).sort(function (a, b) {
36 var ks = data[i];
36 // sort by display_name
37 this.kernelspecs[ks.name] = ks;
37 var da = data.kernelspecs[a].display_name;
38 var db = data.kernelspecs[b].display_name;
39 if (da === db) {
40 return 0;
41 } else if (da > db) {
42 return 1;
43 } else {
44 return -1;
45 }
46 });
47 for (var i = 0; i < keys.length; i++) {
48 var ks = this.kernelspecs[keys[i]];
38 var ksentry = $("<li>").attr("id", "kernel-" +ks.name).append($('<a>')
49 var ksentry = $("<li>").attr("id", "kernel-" +ks.name).append($('<a>')
39 .attr('href', '#')
50 .attr('href', '#')
40 .click($.proxy(this.change_kernel, this, ks.name))
51 .click($.proxy(this.change_kernel, this, ks.name))
@@ -38,11 +38,21 b' define(['
38
38
39 NewNotebookWidget.prototype._load_kernelspecs = function (data) {
39 NewNotebookWidget.prototype._load_kernelspecs = function (data) {
40 /** load kernelspec list */
40 /** load kernelspec list */
41 this.kernelspecs = {};
41 this.kernelspecs = data.kernelspecs;
42 var menu = this.element.find("#new-notebook-menu");
42 var menu = this.element.find("#new-notebook-menu");
43 for (var i = 0; i < data.length; i++) {
43 var keys = Object.keys(data.kernelspecs).sort(function (a, b) {
44 var ks = data[i];
44 var da = data.kernelspecs[a].display_name;
45 this.kernelspecs[ks.name] = ks;
45 var db = data.kernelspecs[b].display_name;
46 if (da === db) {
47 return 0;
48 } else if (da > db) {
49 return 1;
50 } else {
51 return -1;
52 }
53 });
54 for (var i = 0; i < keys.length; i++) {
55 var ks = this.kernelspecs[keys[i]];
46 var li = $("<li>")
56 var li = $("<li>")
47 .attr("id", "kernel-" +ks.name)
57 .attr("id", "kernel-" +ks.name)
48 .data('kernelspec', ks).append(
58 .data('kernelspec', ks).append(
@@ -59,12 +69,12 b' define(['
59 );
69 );
60 menu.append(li);
70 menu.append(li);
61 }
71 }
62 this._load_default_kernelspec();
72 this._load_default_kernelspec(data['default']);
63 };
73 };
64
74
65 NewNotebookWidget.prototype._load_default_kernelspec = function () {
75 NewNotebookWidget.prototype._load_default_kernelspec = function (default_name) {
66 /** load default kernelspec name from localStorage, if defined */
76 /** load default kernelspec name from localStorage, if defined */
67 this.select_kernel(localStorage.default_kernel_name);
77 this.select_kernel(localStorage.default_kernel_name || default_name);
68 };
78 };
69
79
70 NewNotebookWidget.prototype.select_kernel = function (kernel_name) {
80 NewNotebookWidget.prototype.select_kernel = function (kernel_name) {
General Comments 0
You need to be logged in to leave comments. Login now