Skip to content

Commit

Permalink
[runtools] don't alter selection when running cells
Browse files Browse the repository at this point in the history
  • Loading branch information
jcb91 committed Oct 26, 2017
1 parent cd3b514 commit 1000c6a
Showing 1 changed file with 36 additions and 28 deletions.
64 changes: 36 additions & 28 deletions src/jupyter_contrib_nbextensions/nbextensions/runtools/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,15 @@ define([
help: 'Run cells above',
help_index: 'xa',
handler: function() {
var mode = Jupyter.notebook.get_selected_cell().mode;
Jupyter.notebook.execute_cells_above();
Jupyter.notebook.select_next();
var type = Jupyter.notebook.get_selected_cell().cell_type;
if (mode === "edit" && type === "code") Jupyter.notebook.edit_mode();
execute_cells_above();
return false;
}
};
add_command_shortcuts[params["run_cells_below"]] = {
help: 'Run cells below',
help_index: 'aa',
handler: function() {
var mode = Jupyter.notebook.get_selected_cell().mode;
Jupyter.notebook.execute_cells_below();
var type = Jupyter.notebook.get_selected_cell().cell_type;
if (mode === "edit" && type === "code") Jupyter.notebook.edit_mode();
execute_cells_below();
return false;
}
};
Expand Down Expand Up @@ -131,9 +124,7 @@ define([
help_index: 'ra',
handler: function() {
var pos = Jupyter.notebook.element.scrollTop();
var ic = Jupyter.notebook.get_selected_index();
Jupyter.notebook.execute_all_cells();
Jupyter.notebook.select(ic);
execute_all_cells();
Jupyter.notebook.element.animate({
scrollTop: pos
}, 100);
Expand Down Expand Up @@ -222,19 +213,40 @@ define([
for (var i = 0; i < end; i++) {
if (runcell === cells[i]) {
if (runcell.metadata.run_control !== undefined && runcell.metadata.run_control.marked === true) {
IPython.notebook.select(i);
var g = runcell.code_mirror.getGutterElement();
$(g).css({
"background-color": params.run_color
});
IPython.notebook.execute_cell();
runcell.execute();
return;
}
}
}
}
}

function _execute_without_selecting(idx_start, idx_end, stop_on_error) {
// notebook.execute_cells alters selection, this doesn't
var cells = Jupyter.notebook.get_cells();
idx_start = idx_start !== undefined ? idx_start : 0;
idx_end = idx_end !== undefined ? idx_end : cells.length;
for (var ii = idx_start; ii < idx_end; ii++) {
cells[ii].execute(stop_on_error);
}
}

function execute_cells_above() {
_execute_without_selecting(0, Jupyter.notebook.get_selected_index());
}

function execute_cells_below() {
_execute_without_selecting(Jupyter.notebook.get_selected_index(), undefined);
}

function execute_all_cells(stop_on_error) {
_execute_without_selecting(0, undefined, stop_on_error);
}

/**
* Run code cells marked in metadata
*
Expand Down Expand Up @@ -413,11 +425,7 @@ define([
*
*/
var run_all_cells_ignore_errors = function() {
var cells = Jupyter.notebook.get_cells();
var ncells = cells.length;
for (var i = 0; i < ncells; i++) {
cells[i].execute(false);
}
execute_all_cells(false);
};

/**
Expand Down Expand Up @@ -467,8 +475,9 @@ define([
'position': 'absolute'
});
$('#run_c').on('click', function(e) {
Jupyter.notebook.execute_cell();
e.target.blur()
var idx = Jupyter.notebook.get_selected_index();
_execute_without_selecting(idx, idx + 1);
e.target.blur();
})
.tooltip({
delay: {
Expand All @@ -477,9 +486,8 @@ define([
}
});
$('#run_ca').on('click', function(e) {
Jupyter.notebook.execute_cells_above();
Jupyter.notebook.select_next();
e.target.blur()
execute_cells_above();
e.target.blur();
})
.tooltip({
delay: {
Expand All @@ -488,8 +496,8 @@ define([
}
});
$('#run_cb').on('click', function(e) {
Jupyter.notebook.execute_cells_below();
e.target.blur()
execute_cells_below();
e.target.blur();
})
.tooltip({
delay: {
Expand All @@ -498,8 +506,8 @@ define([
}
});
$('#run_a').on('click', function(e) {
Jupyter.notebook.execute_all_cells();
e.target.blur()
execute_all_cells();
e.target.blur();
})
.tooltip({
delay: {
Expand Down

0 comments on commit 1000c6a

Please sign in to comment.