##// END OF EJS Templates
Use JS events for switching kernelspecs
Thomas Kluyver -
Show More
@@ -11,12 +11,13 b' define(['
11 var KernelSelector = function(selector, notebook) {
11 var KernelSelector = function(selector, notebook) {
12 this.selector = selector;
12 this.selector = selector;
13 this.notebook = notebook;
13 this.notebook = notebook;
14 this.events = notebook.events;
14 this.kernelspecs = {};
15 this.kernelspecs = {};
15 if (this.selector !== undefined) {
16 if (this.selector !== undefined) {
16 this.element = $(selector);
17 this.element = $(selector);
17 this.request_kernelspecs();
18 this.request_kernelspecs();
18 }
19 }
19
20 this.bind_events();
20 // For now, this is how we make this object available elsewhere
21 // For now, this is how we make this object available elsewhere
21 IPython.kernelselector = this;
22 IPython.kernelselector = this;
22 };
23 };
@@ -44,15 +45,17 b' define(['
44 if (kernel_name === this.notebook.kernel.name) {
45 if (kernel_name === this.notebook.kernel.name) {
45 return;
46 return;
46 }
47 }
48 var ks = this.kernelspecs[kernel_name];
49 this.events.trigger('spec_changed.Kernel', ks);
47 this.notebook.session.delete();
50 this.notebook.session.delete();
48 this.notebook.start_session(kernel_name);
51 this.notebook.start_session(kernel_name);
49 };
52 };
50
53
51 KernelSelector.prototype.set_displayed_name = function(kernel_name) {
54 KernelSelector.prototype.bind_events = function() {
52 var ks = this.kernelspecs[kernel_name]
55 var that = this;
53 if (ks !== undefined) {
56 this.events.on('spec_changed.Kernel', function(event, data) {
54 this.element.find("#current_kernel_spec").text(ks.display_name);
57 that.element.find("#current_kernel_spec").text(data.display_name);
55 }
58 });
56 };
59 };
57
60
58 return {'KernelSelector': KernelSelector};
61 return {'KernelSelector': KernelSelector};
@@ -215,6 +215,13 b' define(['
215 }
215 }
216 });
216 });
217 });
217 });
218
219 this.events.on('spec_changed.Kernel', function(event, data) {
220 that.set_kernelspec_metadata(data);
221 if (data.codemirror_mode) {
222 that.set_codemirror_mode(data.codemirror_mode);
223 }
224 });
218
225
219 var collapse_time = function (time) {
226 var collapse_time = function (time) {
220 var app_height = $('#ipython-main-app').height(); // content height
227 var app_height = $('#ipython-main-app').height(); // content height
@@ -338,8 +345,7 b' define(['
338 keyboard_manager: this.keyboard_manager});
345 keyboard_manager: this.keyboard_manager});
339 };
346 };
340
347
341 Notebook.prototype.set_kernelspec_metadata = function(kernel_name) {
348 Notebook.prototype.set_kernelspec_metadata = function(ks) {
342 var ks = IPython.kernelselector.kernelspecs[kernel_name]
343 var tostore = {};
349 var tostore = {};
344 for (field in ks) {
350 for (field in ks) {
345 if (field !== 'argv' && field !== 'env') {
351 if (field !== 'argv' && field !== 'env') {
@@ -347,11 +353,6 b' define(['
347 }
353 }
348 }
354 }
349 this.metadata.kernelspec = tostore;
355 this.metadata.kernelspec = tostore;
350
351 // Update the codemirror mode for code cells
352 if (tostore.codemirror_mode) {
353 this.set_codemirror_mode(tostore.codemirror_mode);
354 }
355 }
356 }
356
357
357 // Cell indexing, retrieval, etc.
358 // Cell indexing, retrieval, etc.
@@ -1781,10 +1782,10 b' define(['
1781 this.notebook_name = data.name;
1782 this.notebook_name = data.name;
1782 var trusted = true;
1783 var trusted = true;
1783
1784
1784 // Set the default codemirror mode before we load the cells
1785 // Trigger an event changing the kernel spec - this will set the default
1785 var cm_mode = (this.metadata.kernelspec || {}).codemirror_mode;
1786 // codemirror mode
1786 if (cm_mode) {
1787 if (this.metadata.kernelspec !== undefined) {
1787 this.set_codemirror_mode(cm_mode);
1788 this.events.trigger('spec_changed.Kernel', this.metadata.kernelspec);
1788 }
1789 }
1789
1790
1790 // Only handle 1 worksheet for now.
1791 // Only handle 1 worksheet for now.
@@ -91,13 +91,17 b' define(['
91 */
91 */
92 Session.prototype._handle_start_success = function (data, status, xhr) {
92 Session.prototype._handle_start_success = function (data, status, xhr) {
93 this.id = data.id;
93 this.id = data.id;
94 // If we asked for 'python', the response will have 'python3' or 'python2'
94 // If we asked for 'python', the response will have 'python3' or 'python2'.
95 this.kernel_name = data.kernel.name;
95 // In this case, fire the spec changed event again to update the name
96 // and highlighting.
97 if (data.kernel.name !== this.kernel_name) {
98 this.kernel_name = data.kernel.name;
99 var ks = IPython.kernelselector.kernelspecs[this.kernel_name];
100 this.notebook.events.trigger('spec_changed.Kernel', ks);
101 }
96 var kernel_service_url = utils.url_path_join(this.base_url, "api/kernels");
102 var kernel_service_url = utils.url_path_join(this.base_url, "api/kernels");
97 this.kernel = new kernel.Kernel(kernel_service_url, this.ws_url, this.notebook, this.kernel_name);
103 this.kernel = new kernel.Kernel(kernel_service_url, this.ws_url, this.notebook, this.kernel_name);
98 this.kernel._kernel_started(data.kernel);
104 this.kernel._kernel_started(data.kernel);
99 IPython.kernelselector.set_displayed_name(this.kernel_name);
100 this.notebook.set_kernelspec_metadata(this.kernel_name);
101 };
105 };
102
106
103 /**
107 /**
General Comments 0
You need to be logged in to leave comments. Login now