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.

Revisions

  1. juhasch revised this gist Apr 1, 2013. 1 changed file with 69 additions and 66 deletions.
    135 changes: 69 additions & 66 deletions breakpoint.js
    Original file line number Diff line number Diff line change
    @@ -6,48 +6,51 @@
    //----------------------------------------------------------------------------

    //============================================================================
    // Breakpoint extension
    // Execute notebook cells until a breakpoint in encountered
    // 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
    //============================================================================

    var breakpointColor = "#EEB5B5";

    var CellToolbar= IPython.CellToolbar;

    var helper_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",breakpointColor);
    } 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
    }
    );

    var breakpoint = function(div, cell){
    if ((cell instanceof IPython.CodeCell)) {
    return helper_breakpoint(div,cell)
    } //else do nothing
    }

    CellToolbar.register_callback('breakpoint.chkb', breakpoint);
    /**
    * 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');
    }
    }

    CellToolbar.register_preset('Breakpoint', ['breakpoint.chkb'])
    /**
    * 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 () {
    // run code cells from current cell to next breakpoint
    var start = IPython.notebook.get_selected_index();
    var end = IPython.notebook.ncells()
    for (var i=start; i<end; i++) {
    @@ -64,37 +67,33 @@ var run_breakpoint = function () {
    }
    };

    function clickPrompt(val) {
    // loop through all code cells to find which input prompt was clicked
    // TODO: make sure we check the right checkbox, we now assume there is only one
    /**
    * 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 (val.is(prompt)) {
    if (prompt_clicked.is(prompt)) {
    if (cell.metadata.run_control == undefined){cell.metadata.run_control = {}}
    var value = cell.metadata.run_control.breakpoint;
    // toggle breakpoint
    if (value == true) {
    cell.element.find('div.input_prompt').css("background","0");
    cell.metadata.run_control.breakpoint = false ;
    var c = cell.element.find('input');
    c.prop("checked",false)
    } else {
    cell.element.find('div.input_prompt').css("background",breakpointColor);
    cell.metadata.run_control.breakpoint = true ;
    var c = cell.element.find('input');
    c.prop("checked",true)
    }
    makeMarker(cell,!cell.metadata.run_control.breakpoint);
    }
    }
    }
    };

    var init_breakpoint = function(){

    // Add run control buttons to toolbar
    /**
    * 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',
    @@ -120,33 +119,37 @@ var init_breakpoint = function(){
    IPython.notebook.execute_all_cells();
    }
    },
    {
    id : 'run_until_break',
    label : 'Run Until Breakpoint',
    icon : 'ui-icon-bullet',
    callback : run_breakpoint
    },
    {
    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
    }
    ]);

    // Mark cells with breakpoint enabled and add callback function to set/remove breakpoints
    // by clicking on the input prompt
    var cells = IPython.notebook.get_cells();
    for(var i in cells){
    var cell = cells[i];
    if ((cell instanceof IPython.CodeCell)) {
    cell.element.find('div.input_prompt').click( function() {clickPrompt($(this)); } )
    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) {
    if (cell.metadata.run_control.breakpoint == true) {
    cell.element.find('div.input_prompt').css("background",breakpointColor);
    }
    makeMarker(cell,cell.metadata.run_control.breakpoint);
    }
    }
    }
  2. juhasch revised this gist Mar 27, 2013. 1 changed file with 75 additions and 11 deletions.
    86 changes: 75 additions & 11 deletions breakpoint.js
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,8 @@
    // allowing to step through the notebook
    //============================================================================

    var breakpointColor = "#EEB5B5";

    var CellToolbar= IPython.CellToolbar;

    var helper_breakpoint = CellToolbar.utils.checkbox_ui_generator('Breakpoint',
    @@ -20,7 +22,7 @@ var helper_breakpoint = CellToolbar.utils.checkbox_ui_generator('Breakpoint',
    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");
    cell.element.find('div.input_prompt').css("background",breakpointColor);
    } else {
    cell.element.find('div.input_prompt').css("background","0");
    }
    @@ -35,7 +37,7 @@ var helper_breakpoint = CellToolbar.utils.checkbox_ui_generator('Breakpoint',
    );

    var breakpoint = function(div, cell){
    if ((cell instanceof IPython.CodeCell)) {
    if ((cell instanceof IPython.CodeCell)) {
    return helper_breakpoint(div,cell)
    } //else do nothing
    }
    @@ -45,7 +47,7 @@ CellToolbar.register_callback('breakpoint.chkb', breakpoint);
    CellToolbar.register_preset('Breakpoint', ['breakpoint.chkb'])

    var run_breakpoint = function () {

    // run code cells from current cell to next breakpoint
    var start = IPython.notebook.get_selected_index();
    var end = IPython.notebook.ncells()
    for (var i=start; i<end; i++) {
    @@ -62,32 +64,94 @@ var run_breakpoint = function () {
    }
    };

    function clickPrompt(val) {
    // loop through all code cells to find which input prompt was clicked
    // TODO: make sure we check the right checkbox, we now assume there is only one
    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 (val.is(prompt)) {
    if (cell.metadata.run_control == undefined){cell.metadata.run_control = {}}
    var value = cell.metadata.run_control.breakpoint;
    // toggle breakpoint
    if (value == true) {
    cell.element.find('div.input_prompt').css("background","0");
    cell.metadata.run_control.breakpoint = false ;
    var c = cell.element.find('input');
    c.prop("checked",false)
    } else {
    cell.element.find('div.input_prompt').css("background",breakpointColor);
    cell.metadata.run_control.breakpoint = true ;
    var c = cell.element.find('input');
    c.prop("checked",true)
    }
    }
    }
    }
    };

    var init_breakpoint = function(){

    // Add button
    // Add run control buttons to toolbar
    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 : 'run_until_break',
    label : 'Run Until Breakpoint',
    icon : 'ui-icon-bullet',
    callback : run_breakpoint
    }
    },
    {
    id : 'interrupt_b',
    label : 'Interrupt',
    icon : 'ui-icon-stop',
    callback : function () {
    IPython.notebook.kernel.interrupt();
    }
    }
    ]);

    // Mark cells with breakpoint enabled
    // Mark cells with breakpoint enabled and add callback function to set/remove breakpoints
    // by clicking on the input prompt
    var cells = IPython.notebook.get_cells();
    for(var i in cells){
    var cell = cells[i];
    if ((cell instanceof IPython.CodeCell)) {
    if ((cell instanceof IPython.CodeCell)) {
    cell.element.find('div.input_prompt').click( function() {clickPrompt($(this)); } )
    if (cell.metadata.run_control != undefined) {
    if (cell.metadata.run_control.breakpoint == true) {
    cell.element.find('div.input_prompt').css("background","#f7f7f7");
    cell.element.find('div.input_prompt').css("background",breakpointColor);
    }
    }
    }
    }
    }
    };

    $([IPython.events]).on('notebook_loaded.Notebook',init_breakpoint);

    console.log("Breakpoint extension loaded correctly")
  3. juhasch revised this gist Mar 26, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion breakpoint.js
    Original file line number Diff line number Diff line change
    @@ -57,7 +57,6 @@ var run_breakpoint = function () {
    break;
    }
    }
    console.log("exec");
    IPython.notebook.execute_selected_cell({add_new:false});
    }
    }
  4. juhasch revised this gist Mar 26, 2013. 1 changed file with 13 additions and 8 deletions.
    21 changes: 13 additions & 8 deletions breakpoint.js
    Original file line number Diff line number Diff line change
    @@ -7,14 +7,14 @@

    //============================================================================
    // 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
    // Execute notebook cells until a breakpoint in encountered
    // If a breakpoint is set at the currently selected cell, run cell anyway,
    // allowing to step through the notebook
    //============================================================================

    var CellToolbar= IPython.CellToolbar;

    var breakpoint = CellToolbar.utils.checkbox_ui_generator('Breakpoint',
    var helper_breakpoint = CellToolbar.utils.checkbox_ui_generator('Breakpoint',
    // setter
    function(cell, value){
    if (cell.metadata.run_control == undefined){cell.metadata.run_control = {}}
    @@ -34,6 +34,12 @@ var breakpoint = CellToolbar.utils.checkbox_ui_generator('Breakpoint',
    }
    );

    var breakpoint = function(div, cell){
    if ((cell instanceof IPython.CodeCell)) {
    return helper_breakpoint(div,cell)
    } //else do nothing
    }

    CellToolbar.register_callback('breakpoint.chkb', breakpoint);

    CellToolbar.register_preset('Breakpoint', ['breakpoint.chkb'])
    @@ -50,14 +56,13 @@ var run_breakpoint = function () {
    if (cell.metadata.run_control.breakpoint == true && i > start) {
    break;
    }
    } else {
    IPython.notebook.execute_selected_cell({add_new:false});
    }
    }
    console.log("exec");
    IPython.notebook.execute_selected_cell({add_new:false});
    }
    }
    };


    var init_breakpoint = function(){

    // Add button
  5. juhasch revised this gist Mar 25, 2013. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions breakpoint.js
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,9 @@

    //============================================================================
    // Breakpoint extension
    // Allow running of notebooks until a breakpoint in encountered
    // If breakpoint is on currently selected cell, run cell anyway, allowing to step through notebook
    // 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;
  6. juhasch revised this gist Mar 25, 2013. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions breakpoint.js
    Original file line number Diff line number Diff line change
    @@ -41,14 +41,12 @@ var run_breakpoint = function () {

    var start = IPython.notebook.get_selected_index();
    var end = IPython.notebook.ncells()
    console.log("run",start,end)
    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) {
    console.log("Breakpoint found");
    break;
    }
    } else {
  7. juhasch created this gist Mar 25, 2013.
    90 changes: 90 additions & 0 deletions breakpoint.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    //----------------------------------------------------------------------------
    // 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 of notebooks until a breakpoint in encountered
    // If breakpoint is on currently selected cell, run cell anyway, allowing to step through 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()
    console.log("run",start,end)
    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) {
    console.log("Breakpoint found");
    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")