public
Last active

Run code cells in an iPython notebook until a breakpoint is encountered Updated version with div.breakpoint element to display breakpoint indicator.

  • Download Gist
breakpoint.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
//----------------------------------------------------------------------------
// 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 - execute notebook cells until breakpoint
// If a breakpoint is set at the currently selected cell, run cell anyway,
// allowing to step through the notebook
//============================================================================
 
 
/**
* Toggle breakpoint marker on/off
*
* @param {Object} current codecell
* @param {Boolean} turn breakpoint on/off
*/
function makeMarker(cell,val) {
var input = cell.element.find('div.breakpoint');
cell.metadata.run_control.breakpoint = val ;
if (val == true) {
input.html('‚óŹ');
console.log('true');
} else {
input.html('');
console.log('false');
}
}
 
/**
* Clear all breakpoints in notebook
*
*/
var clear_breakpoints = function() {
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){cell.metadata.run_control = {}} {
makeMarker(cell,false);
}
}
}
};
 
/**
* Run code cells from current cell to next breakpoint
*
*/
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;
}
}
IPython.notebook.execute_selected_cell({add_new:false});
}
}
};
 
/**
* Loop through all code cells to find which input prompt was clicked
* and turn breakpoint on/off
*
* @param {Object} div.input_prompt object where the mouse was clicked at
*/
function clickPrompt(prompt_clicked) {
var cells = IPython.notebook.get_cells();
for(var i in cells){
var cell = cells[i];
if ((cell instanceof IPython.CodeCell)) {
var prompt = cell.element.find('div.input_prompt');
if (prompt_clicked.is(prompt)) {
if (cell.metadata.run_control == undefined){cell.metadata.run_control = {}}
makeMarker(cell,!cell.metadata.run_control.breakpoint);
}
}
}
};
 
 
/**
* Add run control buttons to toolbar and mark cells with breakpoint enabled,
* and add callback function to set/remove breakpoints
*
*/
var init_breakpoint = function(){
IPython.toolbar.add_buttons_group([
{
id : 'run_c',
label : 'Run Cell',
icon : 'ui-icon-seek-next',
callback : function () {
IPython.notebook.execute_selected_cell();
}
},
{
id : 'run_cb',
label : 'Run Cells Below',
icon : 'ui-icon-seek-end',
callback : function () {
IPython.notebook.execute_cells_below();
}
},
{
id : 'run_a',
label : 'Run All',
icon : 'ui-icon-play',
callback : function () {
IPython.notebook.execute_all_cells();
}
},
{
id : 'interrupt_b',
label : 'Interrupt',
icon : 'ui-icon-stop',
callback : function () {
IPython.notebook.kernel.interrupt();
}
},
{
id : 'run_until_break',
label : 'Run Until Breakpoint',
icon : 'ui-icon-bullet',
callback : run_breakpoint
},
{
id : 'clear_all_breakpoints',
label : 'Clear all Breakpoints',
icon : 'ui-icon-radio-off',
callback : clear_breakpoints
}
]);
 
var cells = IPython.notebook.get_cells();
for(var i in cells){
var cell = cells[i];
if ((cell instanceof IPython.CodeCell)) {
var prompt = cell.element.find('div.input_prompt');
prompt.click( function() {clickPrompt($(this)); } )
prompt.append('<div class="breakpoint" style="color: rgb(136, 34, 34);"></div');
if (cell.metadata.run_control != undefined) {
makeMarker(cell,cell.metadata.run_control.breakpoint);
}
}
}
};
$([IPython.events]).on('notebook_loaded.Notebook',init_breakpoint);
 
console.log("Breakpoint extension loaded correctly")

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.