##// END OF EJS Templates
Merge pull request #7432 from minrk/load-race...
Thomas Kluyver -
r19895:36b0fab3 merge
parent child Browse files
Show More
@@ -9,6 +9,7 b' define(['
9 "use strict";
9 "use strict";
10
10
11 var KernelSelector = function(selector, notebook) {
11 var KernelSelector = function(selector, notebook) {
12 var that = this;
12 this.selector = selector;
13 this.selector = selector;
13 this.notebook = notebook;
14 this.notebook = notebook;
14 this.notebook.set_kernelselector(this);
15 this.notebook.set_kernelselector(this);
@@ -22,6 +23,11 b' define(['
22 this.bind_events();
23 this.bind_events();
23 // Make the object globally available for user convenience & inspection
24 // Make the object globally available for user convenience & inspection
24 IPython.kernelselector = this;
25 IPython.kernelselector = this;
26 this._finish_load = null;
27 this.loaded = new Promise(function(resolve, reject) {
28 that._finish_load = resolve;
29 });
30
25 Object.seal(this);
31 Object.seal(this);
26 };
32 };
27
33
@@ -47,26 +53,29 b' define(['
47 return -1;
53 return -1;
48 }
54 }
49 });
55 });
50
56
57 var i, ks, ks_submenu_entry;
51 // Create the Kernel > Change kernel submenu
58 // Create the Kernel > Change kernel submenu
52 for (var i = 0; i < keys.length; i++) {
59 for (i = 0; i < keys.length; i++) {
53 var ks = this.kernelspecs[keys[i]];
60 ks = this.kernelspecs[keys[i]];
54 var ks_submenu_entry = $("<li>").attr("id", "kernel-submenu-"+ks.name).append($('<a>')
61 ks_submenu_entry = $("<li>").attr("id", "kernel-submenu-"+ks.name).append($('<a>')
55 .attr('href', '#')
62 .attr('href', '#')
56 .click($.proxy(this.change_kernel, this, ks.name))
63 .click($.proxy(this.set_kernel, this, ks.name))
57 .text(ks.spec.display_name));
64 .text(ks.spec.display_name));
58 change_kernel_submenu.append(ks_submenu_entry);
65 change_kernel_submenu.append(ks_submenu_entry);
59 }
66 }
60
67
61 // Create the File > New Notebook submenu
68 // Create the File > New Notebook submenu
62 for (var i = 0; i < keys.length; i++) {
69 for (i = 0; i < keys.length; i++) {
63 var ks = this.kernelspecs[keys[i]];
70 ks = this.kernelspecs[keys[i]];
64 var ks_submenu_entry = $("<li>").attr("id", "new-notebook-submenu-"+ks.name).append($('<a>')
71 ks_submenu_entry = $("<li>").attr("id", "new-notebook-submenu-"+ks.name).append($('<a>')
65 .attr('href', '#')
72 .attr('href', '#')
66 .click($.proxy(this.new_notebook, this, ks.name))
73 .click($.proxy(this.new_notebook, this, ks.name))
67 .text(ks.spec.display_name));
74 .text(ks.spec.display_name));
68 new_notebook_submenu.append(ks_submenu_entry);
75 new_notebook_submenu.append(ks_submenu_entry);
69 }
76 }
77 // trigger loaded promise
78 this._finish_load();
70 };
79 };
71
80
72 KernelSelector.prototype._spec_changed = function (event, ks) {
81 KernelSelector.prototype._spec_changed = function (event, ks) {
@@ -80,7 +89,7 b' define(['
80 if (cur_kernel_entry.length) {
89 if (cur_kernel_entry.length) {
81 cur_kernel_entry.parent().prepend($("<li>").attr("class","divider"))
90 cur_kernel_entry.parent().prepend($("<li>").attr("class","divider"))
82 .prepend(cur_kernel_entry);
91 .prepend(cur_kernel_entry);
83 };
92 }
84
93
85 // load logo
94 // load logo
86 var logo_img = this.element.find("img.current_kernel_logo");
95 var logo_img = this.element.find("img.current_kernel_logo");
@@ -118,28 +127,26 b' define(['
118 }
127 }
119 };
128 };
120
129
121 KernelSelector.prototype.change_kernel = function (kernel_name) {
130 KernelSelector.prototype.set_kernel = function (kernel_name) {
122 /**
131 /** set the kernel by name, ensuring kernelspecs have been loaded, first */
123 * TODO, have a methods to set kernel spec directly ?
132 var that = this;
124 **/
133 return this.loaded.then(function () {
134 that._set_kernel(kernel_name);
135 });
136 };
137
138 KernelSelector.prototype._set_kernel = function (kernel_name) {
139 /** Actually set the kernel (kernelspecs have been loaded) */
125 if (kernel_name === this.current_selection) {
140 if (kernel_name === this.current_selection) {
141 // only trigger event if value changed
126 return;
142 return;
127 }
143 }
128 var ks = this.kernelspecs[kernel_name];
144 var ks = this.kernelspecs[kernel_name];
129
145 if (this.notebook._session_starting) {
130 try {
146 console.error("Cannot change kernel while waiting for pending session start.");
131 this.notebook.start_session(kernel_name);
132 } catch (e) {
133 if (e.name === 'SessionAlreadyStarting') {
134 console.log("Cannot change kernel while waiting for pending session start.");
135 } else {
136 // unhandled error
137 throw e;
138 }
139 // only trigger spec_changed if change was successful
140 return;
147 return;
141 }
148 }
142 console.log('spec', kernel_name, ks);
149 this.current_selection = kernel_name;
143 this.events.trigger('spec_changed.Kernel', ks);
150 this.events.trigger('spec_changed.Kernel', ks);
144 };
151 };
145
152
@@ -181,13 +188,7 b' define(['
181 this.events.on('spec_changed.Kernel', $.proxy(this._spec_changed, this));
188 this.events.on('spec_changed.Kernel', $.proxy(this._spec_changed, this));
182
189
183 this.events.on('kernel_created.Session', function (event, data) {
190 this.events.on('kernel_created.Session', function (event, data) {
184 if (data.kernel.name !== that.current_selection) {
191 that.set_kernel(data.kernel.name);
185 // If we created a 'python' session, we only know if it's Python
186 // 3 or 2 on the server's reply, so we fire the event again to
187 // set things up.
188 var ks = that.kernelspecs[data.kernel.name];
189 that.events.trigger('spec_changed.Kernel', ks);
190 }
191 });
192 });
192
193
193 var logo_img = this.element.find("img.current_kernel_logo");
194 var logo_img = this.element.find("img.current_kernel_logo");
@@ -248,6 +248,10 b' define(['
248 this.events.on('spec_changed.Kernel', function(event, data) {
248 this.events.on('spec_changed.Kernel', function(event, data) {
249 that.metadata.kernelspec =
249 that.metadata.kernelspec =
250 {name: data.name, display_name: data.spec.display_name};
250 {name: data.name, display_name: data.spec.display_name};
251 // start session if the current session isn't already correct
252 if (!(this.session && this.session.kernel && this.session.kernel.name === data.name)) {
253 that.start_session(data.name);
254 }
251 });
255 });
252
256
253 this.events.on('kernel_ready.Kernel', function(event, data) {
257 this.events.on('kernel_ready.Kernel', function(event, data) {
@@ -1763,19 +1767,6 b' define(['
1763 this.notebook_path = data.path;
1767 this.notebook_path = data.path;
1764 var trusted = true;
1768 var trusted = true;
1765
1769
1766 // Trigger an event changing the kernel spec - this will set the default
1767 // codemirror mode
1768 if (this.metadata.kernelspec !== undefined) {
1769 // TODO shoudl probably not trigger here,
1770 // should call the kernel selector, or custom.{js|css} not loaded.
1771 if(this.kernel_selector){
1772 // technically not perfect, we should check that the kernelspec matches
1773 this.kernel_selector.change_kernel(this.metadata.kernelspec.name);
1774 } else {
1775 console.log('do not have handle on kernel_selector');
1776 }
1777 }
1778
1779 // Set the codemirror mode from language_info metadata
1770 // Set the codemirror mode from language_info metadata
1780 if (this.metadata.language_info !== undefined) {
1771 if (this.metadata.language_info !== undefined) {
1781 var langinfo = this.metadata.language_info;
1772 var langinfo = this.metadata.language_info;
@@ -2077,6 +2068,7 b' define(['
2077 * @param {string} notebook_path - A notebook to load
2068 * @param {string} notebook_path - A notebook to load
2078 */
2069 */
2079 Notebook.prototype.load_notebook = function (notebook_path) {
2070 Notebook.prototype.load_notebook = function (notebook_path) {
2071 var that = this;
2080 this.notebook_path = notebook_path;
2072 this.notebook_path = notebook_path;
2081 this.notebook_name = utils.url_path_split(this.notebook_path)[1];
2073 this.notebook_name = utils.url_path_split(this.notebook_path)[1];
2082 this.events.trigger('notebook_loading.Notebook');
2074 this.events.trigger('notebook_loading.Notebook');
@@ -2192,8 +2184,6 b' define(['
2192 this.nbformat_minor = nbmodel.nbformat_minor;
2184 this.nbformat_minor = nbmodel.nbformat_minor;
2193 }
2185 }
2194
2186
2195 // Create the session after the notebook is completely loaded to prevent
2196 // code execution upon loading, which is a security risk.
2197 if (this.session === null) {
2187 if (this.session === null) {
2198 var kernel_name;
2188 var kernel_name;
2199 if (this.metadata.kernelspec) {
2189 if (this.metadata.kernelspec) {
@@ -2202,7 +2192,14 b' define(['
2202 } else {
2192 } else {
2203 kernel_name = utils.get_url_param('kernel_name');
2193 kernel_name = utils.get_url_param('kernel_name');
2204 }
2194 }
2205 this.start_session(kernel_name);
2195 if (kernel_name) {
2196 // setting kernel_name here triggers start_session
2197 this.kernel_selector.set_kernel(kernel_name);
2198 } else {
2199 // start a new session with the server's default kernel
2200 // spec_changed events will fire after kernel is loaded
2201 this.start_session();
2202 }
2206 }
2203 }
2207 // load our checkpoint list
2204 // load our checkpoint list
2208 this.list_checkpoints();
2205 this.list_checkpoints();
General Comments 0
You need to be logged in to leave comments. Login now