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