Skip to content

Commit 17e6b4d

Browse files
committed
Notebook cell execution buffering.
This buffering happens when the kernel is not set in the notebook/cell.
1 parent 890b563 commit 17e6b4d

File tree

2 files changed

+64
-22
lines changed

2 files changed

+64
-22
lines changed

notebook/static/notebook/js/codecell.js

+28-19
Original file line numberDiff line numberDiff line change
@@ -299,41 +299,50 @@ define([
299299
/**
300300
* Execute current code cell to the kernel
301301
* @method execute
302+
*
303+
* If the cell cannot execute (e.g., the kernel does not exist),
304+
* we return `false` and the cell will be left in a pending execution state.
305+
* At some point, the cell should be executed again until execution succeeds.
306+
*
307+
* @returns true if execution was requested, and false if the cell cannot request execution.
302308
*/
303309
CodeCell.prototype.execute = function (stop_on_error) {
304-
if (!this.kernel || !this.kernel.is_connected()) {
305-
console.log("Can't execute, kernel is not connected.");
306-
return;
307-
}
308-
309-
this.output_area.clear_output(false, true);
310310

311311
if (stop_on_error === undefined) {
312312
stop_on_error = true;
313313
}
314314

315+
// Prepare for execution
316+
this.output_area.clear_output(false, true);
315317
var old_msg_id = this.last_msg_id;
316-
317318
if (old_msg_id) {
318319
this.kernel.clear_callbacks_for_msg(old_msg_id);
319-
if (old_msg_id) {
320-
delete CodeCell.msg_cells[old_msg_id];
321-
}
320+
delete CodeCell.msg_cells[old_msg_id];
321+
this.last_msg_id = null;
322322
}
323+
323324
if (this.get_text().trim().length === 0) {
324325
// nothing to do
325326
this.set_input_prompt(null);
326-
return;
327+
return true;
327328
}
328-
this.set_input_prompt('*');
329-
this.element.addClass("running");
330-
var callbacks = this.get_callbacks();
331329

332-
this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true,
333-
stop_on_error : stop_on_error});
334-
CodeCell.msg_cells[this.last_msg_id] = this;
335-
this.render();
336-
this.events.trigger('execute.CodeCell', {cell: this});
330+
this.set_input_prompt('*');
331+
this.element.addClass("running");
332+
333+
if (this.kernel) {
334+
var callbacks = this.get_callbacks();
335+
this.last_msg_id = this.kernel.execute(this.get_text(), callbacks,
336+
{silent: false,
337+
store_history: true,
338+
stop_on_error: stop_on_error});
339+
CodeCell.msg_cells[this.last_msg_id] = this;
340+
this.render();
341+
this.events.trigger('execute.CodeCell', {cell: this});
342+
return true;
343+
} else {
344+
return false;
345+
}
337346
};
338347

339348
/**

notebook/static/notebook/js/notebook.js

+36-3
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ define(function (require) {
153153
this.dirty = null;
154154
this.trusted = null;
155155
this._fully_loaded = false;
156+
157+
this.pending_execution = [];
156158

157159
// Trigger cell toolbar registration.
158160
default_celltoolbar.register(this);
@@ -2090,6 +2092,7 @@ define(function (require) {
20902092
cell.set_kernel(this.session.kernel);
20912093
}
20922094
}
2095+
this.execute_pending_cells();
20932096
};
20942097

20952098
/**
@@ -2220,6 +2223,36 @@ define(function (require) {
22202223
return promise;
22212224
};
22222225

2226+
/**
2227+
* Execute a single cell
2228+
*
2229+
* @param cell - the cell to execute
2230+
*
2231+
* If the cell can not be executed, it is added to a list of
2232+
* cells pending execution.
2233+
*/
2234+
Notebook.prototype.execute = function (cell) {
2235+
var executed = cell.execute();
2236+
if (!executed) {
2237+
this.pending_execution.push(cell)
2238+
}
2239+
}
2240+
2241+
/**
2242+
* Execute pending cells
2243+
*
2244+
*/
2245+
Notebook.prototype.execute_pending_cells = function () {
2246+
var cell;
2247+
while (cell = this.pending_execution.shift()) {
2248+
var executed = cell.execute();
2249+
if (!executed) {
2250+
console.error("Error trying to execute pending cells");
2251+
break;
2252+
}
2253+
}
2254+
}
2255+
22232256
/**
22242257
* Execute cells corresponding to the given indices.
22252258
*
@@ -2233,7 +2266,7 @@ define(function (require) {
22332266
var cell;
22342267
for (var i = 0; i < indices.length; i++) {
22352268
cell = this.get_cell(indices[i]);
2236-
cell.execute();
2269+
this.execute(cell);
22372270
}
22382271

22392272
this.select(indices[indices.length - 1]);
@@ -2272,7 +2305,7 @@ define(function (require) {
22722305
} else {
22732306
var cell = this.get_selected_cell();
22742307
cell_index = this.find_cell_index(cell);
2275-
cell.execute();
2308+
this.execute(cell);
22762309
}
22772310

22782311
// If we are at the end always insert a new cell and return
@@ -2305,7 +2338,7 @@ define(function (require) {
23052338
} else {
23062339
var cell = this.get_selected_cell();
23072340
cell_index = this.find_cell_index(cell);
2308-
cell.execute();
2341+
this.execute(cell);
23092342
}
23102343

23112344
// If we are at the end always insert a new cell and return

0 commit comments

Comments
 (0)