Skip to content

Commit d0ddb98

Browse files
committed
Make the notebook execution buffer move repeatedly executed cells to the end of the queue.
This is consistent with how execution works, since the cell is only finally executed with the final text of the cell. This may still give surprising results, for example, if cell 1 defines a variable and cell 2 increments and prints the variable. If the execution order is cell 1, cell 2, cell 1, then before this commit cell 2 would print fine, whereas now cell 2 will throw an error.
1 parent e2f397a commit d0ddb98

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

notebook/static/notebook/js/notebook.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -2234,7 +2234,17 @@ define(function (require) {
22342234
Notebook.prototype.execute = function (cell) {
22352235
var executed = cell.execute();
22362236
if (!executed) {
2237-
this.pending_execution.push(cell)
2237+
var pending = this.pending_execution;
2238+
var index = pending.indexOf(cell);
2239+
if (index >= 0 ) {
2240+
// if the cell is already pending, move it to the end
2241+
for (var i = index; i < pending.length - 1; i++) {
2242+
pending[i] = pending[i+1];
2243+
}
2244+
pending[pending.length - 1] = cell;
2245+
} else {
2246+
pending.push(cell);
2247+
}
22382248
}
22392249
}
22402250

notebook/tests/notebook/buffering.js

+77-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22
// Test buffering for execution requests.
33
//
44
casper.notebook_test(function () {
5-
this.evaluate(function () {
5+
this.then(function() {
6+
// make sure there are at least three cells for the tests below.
7+
this.append_cell();
8+
this.append_cell();
9+
this.append_cell();
10+
})
11+
12+
this.thenEvaluate(function () {
13+
IPython.notebook.kernel.stop_channels();
614
var cell = IPython.notebook.get_cell(0);
715
cell.set_text('a=10; print(a)');
8-
IPython.notebook.kernel.stop_channels();
916
IPython.notebook.execute_cells([0]);
1017
IPython.notebook.kernel.reconnect(1);
1118
});
@@ -16,7 +23,7 @@ casper.notebook_test(function () {
1623
var result = this.get_output_cell(0);
1724
this.test.assertEquals(result.text, '10\n', 'kernels buffer execution requests if connection is down');
1825
});
19-
26+
2027
this.thenEvaluate(function () {
2128
var cell = IPython.notebook.get_cell(0);
2229
cell.set_text('a=11; print(a)');
@@ -33,4 +40,71 @@ casper.notebook_test(function () {
3340
this.test.assertEquals(result.text, '11\n', 'notebooks buffer cell execution requests if kernel is not set');
3441
});
3542

43+
// Repeated execution behavior differs in the two queues
44+
45+
this.thenEvaluate(function () {
46+
47+
var cell = IPython.notebook.get_cell(0);
48+
var cellplus = IPython.notebook.get_cell(1);
49+
var cellprint = IPython.notebook.get_cell(2);
50+
cell.set_text('k=1');
51+
cellplus.set_text('k+=1');
52+
cellprint.set_text('k*=2')
53+
54+
IPython.notebook.kernel.stop_channels();
55+
56+
// Repeated execution of cell queued up in the kernel executes
57+
// each execution request.
58+
IPython.notebook.execute_cells([0]);
59+
IPython.notebook.execute_cells([2]);
60+
IPython.notebook.execute_cells([1]);
61+
IPython.notebook.execute_cells([1]);
62+
IPython.notebook.execute_cells([1]);
63+
cellprint.set_text('print(k)')
64+
IPython.notebook.execute_cells([2]);
65+
66+
IPython.notebook.kernel.reconnect(1);
67+
});
68+
69+
this.wait_for_output(2);
70+
71+
this.then(function () {
72+
var result = this.get_output_cell(2);
73+
this.test.assertEquals(result.text, '5\n', 'kernel message buffer sends each message queued');
74+
});
75+
76+
this.thenEvaluate(function () {
77+
78+
var cell = IPython.notebook.get_cell(0);
79+
var cellplus = IPython.notebook.get_cell(1);
80+
var cellprint = IPython.notebook.get_cell(2);
81+
cell.set_text('n=1');
82+
cellplus.set_text('n+=1');
83+
cellprint.set_text('n*=2')
84+
85+
cell.kernel = null;
86+
cellplus.kernel = null;
87+
cellprint.kernel = null;
88+
IPython.notebook.kernel = null;
89+
90+
// Repeated execution of cell queued up in the notebook moves the cell
91+
// to the end of the queue, only executing it once.
92+
IPython.notebook.execute_cells([0]);
93+
IPython.notebook.execute_cells([2]);
94+
IPython.notebook.execute_cells([1]);
95+
IPython.notebook.execute_cells([1]);
96+
IPython.notebook.execute_cells([1]);
97+
cellprint.set_text('print(n)')
98+
IPython.notebook.execute_cells([2]);
99+
100+
IPython.notebook._session_started();
101+
});
102+
103+
this.wait_for_output(2);
104+
105+
this.then(function () {
106+
var result = this.get_output_cell(2);
107+
this.test.assertEquals(result.text, '2\n', 'notebook execution buffer moves repeatedly executed cell to end of queue');
108+
});
109+
36110
});

0 commit comments

Comments
 (0)