Skip to content

Commit 1000c6a

Browse files
committed
[runtools] don't alter selection when running cells
1 parent cd3b514 commit 1000c6a

File tree

1 file changed

+36
-28
lines changed
  • src/jupyter_contrib_nbextensions/nbextensions/runtools

1 file changed

+36
-28
lines changed

src/jupyter_contrib_nbextensions/nbextensions/runtools/main.js

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,15 @@ define([
7575
help: 'Run cells above',
7676
help_index: 'xa',
7777
handler: function() {
78-
var mode = Jupyter.notebook.get_selected_cell().mode;
79-
Jupyter.notebook.execute_cells_above();
80-
Jupyter.notebook.select_next();
81-
var type = Jupyter.notebook.get_selected_cell().cell_type;
82-
if (mode === "edit" && type === "code") Jupyter.notebook.edit_mode();
78+
execute_cells_above();
8379
return false;
8480
}
8581
};
8682
add_command_shortcuts[params["run_cells_below"]] = {
8783
help: 'Run cells below',
8884
help_index: 'aa',
8985
handler: function() {
90-
var mode = Jupyter.notebook.get_selected_cell().mode;
91-
Jupyter.notebook.execute_cells_below();
92-
var type = Jupyter.notebook.get_selected_cell().cell_type;
93-
if (mode === "edit" && type === "code") Jupyter.notebook.edit_mode();
86+
execute_cells_below();
9487
return false;
9588
}
9689
};
@@ -131,9 +124,7 @@ define([
131124
help_index: 'ra',
132125
handler: function() {
133126
var pos = Jupyter.notebook.element.scrollTop();
134-
var ic = Jupyter.notebook.get_selected_index();
135-
Jupyter.notebook.execute_all_cells();
136-
Jupyter.notebook.select(ic);
127+
execute_all_cells();
137128
Jupyter.notebook.element.animate({
138129
scrollTop: pos
139130
}, 100);
@@ -222,19 +213,40 @@ define([
222213
for (var i = 0; i < end; i++) {
223214
if (runcell === cells[i]) {
224215
if (runcell.metadata.run_control !== undefined && runcell.metadata.run_control.marked === true) {
225-
IPython.notebook.select(i);
226216
var g = runcell.code_mirror.getGutterElement();
227217
$(g).css({
228218
"background-color": params.run_color
229219
});
230-
IPython.notebook.execute_cell();
220+
runcell.execute();
231221
return;
232222
}
233223
}
234224
}
235225
}
236226
}
237227

228+
function _execute_without_selecting(idx_start, idx_end, stop_on_error) {
229+
// notebook.execute_cells alters selection, this doesn't
230+
var cells = Jupyter.notebook.get_cells();
231+
idx_start = idx_start !== undefined ? idx_start : 0;
232+
idx_end = idx_end !== undefined ? idx_end : cells.length;
233+
for (var ii = idx_start; ii < idx_end; ii++) {
234+
cells[ii].execute(stop_on_error);
235+
}
236+
}
237+
238+
function execute_cells_above() {
239+
_execute_without_selecting(0, Jupyter.notebook.get_selected_index());
240+
}
241+
242+
function execute_cells_below() {
243+
_execute_without_selecting(Jupyter.notebook.get_selected_index(), undefined);
244+
}
245+
246+
function execute_all_cells(stop_on_error) {
247+
_execute_without_selecting(0, undefined, stop_on_error);
248+
}
249+
238250
/**
239251
* Run code cells marked in metadata
240252
*
@@ -413,11 +425,7 @@ define([
413425
*
414426
*/
415427
var run_all_cells_ignore_errors = function() {
416-
var cells = Jupyter.notebook.get_cells();
417-
var ncells = cells.length;
418-
for (var i = 0; i < ncells; i++) {
419-
cells[i].execute(false);
420-
}
428+
execute_all_cells(false);
421429
};
422430

423431
/**
@@ -467,8 +475,9 @@ define([
467475
'position': 'absolute'
468476
});
469477
$('#run_c').on('click', function(e) {
470-
Jupyter.notebook.execute_cell();
471-
e.target.blur()
478+
var idx = Jupyter.notebook.get_selected_index();
479+
_execute_without_selecting(idx, idx + 1);
480+
e.target.blur();
472481
})
473482
.tooltip({
474483
delay: {
@@ -477,9 +486,8 @@ define([
477486
}
478487
});
479488
$('#run_ca').on('click', function(e) {
480-
Jupyter.notebook.execute_cells_above();
481-
Jupyter.notebook.select_next();
482-
e.target.blur()
489+
execute_cells_above();
490+
e.target.blur();
483491
})
484492
.tooltip({
485493
delay: {
@@ -488,8 +496,8 @@ define([
488496
}
489497
});
490498
$('#run_cb').on('click', function(e) {
491-
Jupyter.notebook.execute_cells_below();
492-
e.target.blur()
499+
execute_cells_below();
500+
e.target.blur();
493501
})
494502
.tooltip({
495503
delay: {
@@ -498,8 +506,8 @@ define([
498506
}
499507
});
500508
$('#run_a').on('click', function(e) {
501-
Jupyter.notebook.execute_all_cells();
502-
e.target.blur()
509+
execute_all_cells();
510+
e.target.blur();
503511
})
504512
.tooltip({
505513
delay: {

0 commit comments

Comments
 (0)