Skip to content

Instantly share code, notes, and snippets.

@juhasch
Last active December 15, 2015 09:49
Show Gist options
  • Select an option

  • Save juhasch/5241322 to your computer and use it in GitHub Desktop.

Select an option

Save juhasch/5241322 to your computer and use it in GitHub Desktop.
Run code cells in an iPython notebook until a breakpoint is encountered Updated version with div.breakpoint element to display breakpoint indicator.
//----------------------------------------------------------------------------
// Copyright (C) 2013 The IPython Development Team
//
// Distributed under the terms of the BSD License. The full license is in
// the file COPYING, distributed as part of this software.
//----------------------------------------------------------------------------
//============================================================================
// Breakpoint extension
// Allow running code cells in an notebook until a breakpoint is encountered
// If the breakpoint is set in the currently selected cell, run the cell anyway,
// allowing to step through the complete notebook
//============================================================================
var CellToolbar= IPython.CellToolbar;
var breakpoint = CellToolbar.utils.checkbox_ui_generator('Breakpoint',
// setter
function(cell, value){
if (cell.metadata.run_control == undefined){cell.metadata.run_control = {}}
cell.metadata.run_control.breakpoint = value
if (value == true) {
cell.element.find('div.input_prompt').css("background","#f7f7f7");
} else {
cell.element.find('div.input_prompt').css("background","0");
}
},
//getter
function(cell){ var ns = cell.metadata.run_control;
// if the _draft namespace does not exist return undefined
// (will be interpreted as false by checkbox) otherwise
// return the value
return (ns == undefined)? undefined: ns.breakpoint
}
);
CellToolbar.register_callback('breakpoint.chkb', breakpoint);
CellToolbar.register_preset('Breakpoint', ['breakpoint.chkb'])
var run_breakpoint = function () {
var start = IPython.notebook.get_selected_index();
var end = IPython.notebook.ncells()
for (var i=start; i<end; i++) {
IPython.notebook.select(i);
var cell = IPython.notebook.get_selected_cell();
if ((cell instanceof IPython.CodeCell)) {
if (cell.metadata.run_control != undefined) {
if (cell.metadata.run_control.breakpoint == true && i > start) {
break;
}
} else {
IPython.notebook.execute_selected_cell({add_new:false});
}
}
}
};
var init_breakpoint = function(){
// Add button
IPython.toolbar.add_buttons_group([
{
id : 'run_until_break',
label : 'Run Until Breakpoint',
icon : 'ui-icon-bullet',
callback : run_breakpoint
}
]);
// Mark cells with breakpoint enabled
var cells = IPython.notebook.get_cells();
for(var i in cells){
var cell = cells[i];
if ((cell instanceof IPython.CodeCell)) {
if (cell.metadata.run_control != undefined) {
if (cell.metadata.run_control.breakpoint == true) {
cell.element.find('div.input_prompt').css("background","#f7f7f7");
}
}
}
}
};
$([IPython.events]).on('notebook_loaded.Notebook',init_breakpoint);
console.log("Breakpoint extension loaded correctly")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment