Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a scheduling list for running marked cells #946

Merged
merged 14 commits into from
Sep 24, 2017
90 changes: 79 additions & 11 deletions src/jupyter_contrib_nbextensions/nbextensions/runtools/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,83 @@ define([
var cells = IPython.notebook.get_cells();
for (var i=0; i<ncells; i++) {
var _cell=cells[i];
if (_cell.metadata.run_control != undefined && _cell.metadata.run_control.marked == true )
if (_cell.metadata.run_control !== undefined && _cell.metadata.run_control.marked === true )
showCell(_cell, 'o', show);
}
}

var run_list = [];
/**
* Execute next cell in run list, if it is still marked
*
*/
function execute_next_marked_cell() {
while (run_list.length > 0) {
var runcell = run_list.shift();
var end = IPython.notebook.ncells();
for (var i=0; i<end; i++) {
if (runcell === IPython.notebook.get_cell(i)) {
if (runcell.metadata.run_control !== undefined && runcell.metadata.run_control.marked === true ) {
IPython.notebook.select(i);
console.log('a')
IPython.notebook.execute_cell();
return;
}
}
}
}
}

/**
* Run code cells marked in metadata
*
*/
function run_marked() {
var current = IPython.notebook.get_selected_index();
var end = IPython.notebook.ncells();
for (var i=0; i<end; i++) {
run_list = [];
/* Mark all selected cells as scheduled to be run with new gutter background color */
for (var i=0; i<end; i++) {
IPython.notebook.select(i);
var cell = IPython.notebook.get_selected_cell();
if ((cell instanceof IPython.CodeCell)) {
if ((cell instanceof IPython.CodeCell)) {
if (cell.metadata.run_control != undefined) {
if (cell.metadata.run_control.marked == true ) {
IPython.notebook.execute_cell();
}
}
var g=cell.code_mirror.getGutterElement();
$(g).css({"background-color": "#00f"});
run_list.push(cell);
}
}
}
}
execute_next_marked_cell();
}

/*
* Execute next cell in run_list when notified execution of last cell has been finished
* @param evt Event
* @param data Cell that has finished executing
*/
var finished_execute_event = function(evt, data)
{
var cell = data.cell;
/* Reset gutter color no non-queued state */
if ((cell instanceof IPython.CodeCell)) {
if (cell.metadata.run_control != undefined) {
if (cell.metadata.run_control.marked == true ) {
var g=cell.code_mirror.getGutterElement();
$(g).css({"background-color": "#0f0"});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reset, this should just set an empty string, I think

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, I think maybe it's not a reset, and the comment needs altering?

}
}
}
IPython.notebook.select(current);
execute_next_marked_cell();
}

/**
* Mark or unmark a cell
*
* @param cell
* @param value
* @param cell cell to set
* @param {Boolean} value is cell marked
*/
function setCell(cell,value) {
if (cell.metadata.run_control == undefined) cell.metadata.run_control = {};
Expand Down Expand Up @@ -286,6 +333,25 @@ define([
}
};


var interrupt_execution = function () {
if (run_list.length > 0 ) {
while (run_list.length > 0) {
var runcell = run_list.shift();
var end = IPython.notebook.ncells();
for (var i=0; i<end; i++) {
if (runcell === IPython.notebook.get_cell(i)) {
var g=runcell.code_mirror.getGutterElement();
$(g).css({"background-color": "#0f0"});
}
}
}

} else {
IPython.notebook.kernel.interrupt();
}
}

/**
* Create floating toolbar
*
Expand Down Expand Up @@ -341,7 +407,7 @@ define([
.tooltip({ title : 'Run all - ignore errors' , delay: {show: 500, hide: 100}});
$('#run_m').on('click', function(e) { run_marked(); e.target.blur() })
.tooltip({ title : 'Run marked codecells (Alt-R)' , delay: {show: 500, hide: 100}});
$('#interrupt_b').on('click', function(e) { IPython.notebook.kernel.interrupt(); e.target.blur() })
$('#interrupt_b').on('click', function(e) { interrupt_execution(); e.target.blur() })
.tooltip({ title : 'Interrupt' , delay: {show: 500, hide: 100}});

$('#mark_toggle').on('click', function() { toggle_marker() })
Expand Down Expand Up @@ -533,7 +599,7 @@ define([
var found = jQuery.inArray("CodeMirror-foldgutter", gutters);
if (found == -1) {
cell.code_mirror.setOption('gutters', [gutters, "CodeMirror-foldgutter"]);
//cell.code_mirror.refresh();
cell.code_mirror.refresh();
}
}
}
Expand All @@ -555,6 +621,7 @@ define([
}
};


/**
* Called after extension was loaded
*
Expand All @@ -571,6 +638,7 @@ define([
require(['codemirror/addon/fold/foldgutter'], initGutter)
})
}
events.on('finished_execute.CodeCell', finished_execute_event);
};

return { load_ipython_extension : load_extension };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Description: Runtools provide a number of additional functions for working with
Link: readme.md
Icon: icon.png
Main: main.js
Compatibility: 4.x
Compatibility: 4.x, 5.x