diff --git a/src/jupyter_contrib_nbextensions/nbextensions/runtools/main.js b/src/jupyter_contrib_nbextensions/nbextensions/runtools/main.js index 79b4cdbbe..427a8eaa6 100644 --- a/src/jupyter_contrib_nbextensions/nbextensions/runtools/main.js +++ b/src/jupyter_contrib_nbextensions/nbextensions/runtools/main.js @@ -11,10 +11,6 @@ define([ ], function(Jupyter, $, require, events, configmod, utils, codecell) { "use strict"; - var base_url = utils.get_body_data("baseUrl"); - var config = new configmod.ConfigSection('notebook', { - base_url: base_url - }); var run_list = []; /* list of cells to be run */ // define default config parameter values @@ -33,15 +29,6 @@ define([ run_color: '#f30a2d' }; - // updates default params with any specified in the provided config data - var update_params = function(config_data) { - for (var key in params) { - if (config_data.hasOwnProperty(key)) { - params[key] = config_data[key]; - } - } - }; - /** * Add event if user clicks on codemirror gutter * @@ -53,14 +40,11 @@ define([ var cell = cells[i]; if ((cell.cell_type === "code")) { cell.code_mirror.on("gutterClick", changeEvent); - - if (cell.metadata.run_control !== undefined) { - if (cell.metadata.run_control.marked === true) { - var g = cell.code_mirror.getGutterElement(); - $(g).css({ - "background-color": params.marked_color - }); - } + if (is_marked(cell)) { + var g = cell.code_mirror.getGutterElement(); + $(g).css({ + "background-color": params.marked_color + }); } } } @@ -70,9 +54,7 @@ define([ * Initialize toolbar and gutter after config was loaded */ function initialize() { - if (Jupyter.notebook.config.data.hasOwnProperty('runtools')) { - update_params(Jupyter.notebook.config.data.runtools) - } + $.extend(true, params, Jupyter.notebook.config.data.runtools); add_gutter_events(); @@ -93,11 +75,7 @@ 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; } }; @@ -105,10 +83,7 @@ define([ 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; } }; @@ -149,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); @@ -199,19 +172,23 @@ define([ } } + function _show_input_output_of_marked(show, char) { + var cells = Jupyter.notebook.get_cells(); + var ncells = cells.length; + for (var i = 0; i < ncells; i++) { + var _cell = cells[i]; + if (is_marked(_cell)) + showCell(_cell, char, show); + } + } + /** * Hide or show input of all marked code cells * * @param show {Boolean} show (true) or hide (false) code cells */ function show_input(show) { - var ncells = Jupyter.notebook.ncells(); - var cells = Jupyter.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) - showCell(_cell, 'i', show); - } + _show_input_output_of_marked(show, 'i'); } /** @@ -220,13 +197,7 @@ define([ * @param {Boolean} show show (true) or hide (false) */ function show_output(show) { - var ncells = Jupyter.notebook.ncells(); - var cells = Jupyter.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) - showCell(_cell, 'o', show); - } + _show_input_output_of_marked(show, 'o'); } @@ -235,18 +206,18 @@ define([ * */ function execute_next_marked_cell() { + var cells = Jupyter.notebook.get_cells(); + var end = cells.length; 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 === 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; } } @@ -254,18 +225,40 @@ define([ } } + 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 * */ function run_marked_cells() { - var end = IPython.notebook.ncells(); + var cells = Jupyter.notebook.get_cells(); + var end = cells.length; run_list = []; - /* Mark all selected cells as scheduled to be run with new gutter background color */ + /* Show all marked 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) { + var cell = cells[i]; + if (cell instanceof codecell.CodeCell) { var last_line = cell.code_mirror.lastLine(); var cell_empty = ( last_line === 0 && cell.code_mirror.getLine(last_line) === ""); if (cell.metadata.run_control !== undefined && cell_empty === false) { @@ -290,15 +283,11 @@ define([ 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": params.marked_color - }); - } - } + if (is_marked(cell)) { + var g = cell.code_mirror.getGutterElement(); + $(g).css({ + "background-color": params.marked_color + }); } execute_next_marked_cell(); }; @@ -309,6 +298,7 @@ define([ * @param value */ function setCell(cell, value) { + if (!(cell instanceof codecell.CodeCell)) return; if (cell.metadata.run_control === undefined) cell.metadata.run_control = {}; if (cell.metadata.run_control.marked === undefined) cell.metadata.run_control.marked = false; if (value === undefined) value = !cell.metadata.run_control.marked; @@ -326,41 +316,32 @@ define([ } } + function setCellsMarked(cells, value) { + var ncells = cells.length; + for (var i = 0; i < ncells; i++) { + setCell(cells[i], value); + } + } + /** * Toggle code cell marker */ function toggle_marker() { - var cell = Jupyter.notebook.get_selected_cell(); - setCell(cell, undefined); - cell.focus_cell(); + setCellsMarked(Jupyter.notebook.get_selected_cells(), undefined); } /** * */ function mark_all() { - var cell = Jupyter.notebook.get_selected_cell(); - var ncells = Jupyter.notebook.ncells(); - var cells = Jupyter.notebook.get_cells(); - for (var i = 0; i < ncells; i++) { - var _cell = cells[i]; - setCell(_cell, true); - } - cell.focus_cell(); + setCellsMarked(Jupyter.notebook.get_cells(), true); } /** * */ function mark_none() { - var cell = Jupyter.notebook.get_selected_cell(); - var ncells = Jupyter.notebook.ncells(); - var cells = Jupyter.notebook.get_cells(); - for (var i = 0; i < ncells; i++) { - var _cell = cells[i]; - setCell(_cell, false); - } - cell.focus_cell(); + setCellsMarked(Jupyter.notebook.get_cells(), false); } /** @@ -385,21 +366,10 @@ define([ */ function changeEvent(cm, line, gutter) { var cmline = cm.doc.children[0].lines[line]; - var cell = Jupyter.notebook.get_selected_cell(); if (cmline === undefined) { return; } - - if (cell.code_mirror !== cm) { - var ncells = Jupyter.notebook.ncells(); - var cells = Jupyter.notebook.get_cells(); - for (var i = 0; i < ncells; i++) { - cell = cells[i]; - if (cell.code_mirror === cm) { - break; - } - } - } + var cell = $(cm.element).closest('.cell').data('cell'); if (cell.metadata.run_control === undefined) cell.metadata.run_control = {}; setCell(cell, !cell.metadata.run_control.marked); @@ -411,12 +381,9 @@ define([ * @returns {boolean} true if marked */ var is_marked = function(cell) { - if (cell.metadata.run_control !== undefined) { - if (cell.metadata.run_control.marked === true) { - return true; - } - } - return false; + return (cell instanceof codecell.CodeCell) && + cell.metadata.run_control !== undefined && + cell.metadata.run_control.marked; }; /** @@ -458,11 +425,7 @@ define([ * */ var run_all_cells_ignore_errors = function() { - for (var i = 0; i < Jupyter.notebook.ncells(); i++) { - Jupyter.notebook.select(i); - var cell = Jupyter.notebook.get_selected_cell(); - cell.execute(false); - } + execute_all_cells(false); }; /** @@ -512,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: { @@ -522,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: { @@ -533,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: { @@ -543,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: { @@ -730,8 +693,8 @@ define([ * Initialize all cells with new gutter */ var initGutter = function() { - var ncells = Jupyter.notebook.ncells(); var cells = Jupyter.notebook.get_cells(); + var ncells = cells.length; for (var i = 0; i < ncells; i++) { var cell = cells[i]; if (cell instanceof codecell.CodeCell) { @@ -741,12 +704,9 @@ define([ cell.code_mirror.setOption('gutters', gutters); } } - } - /** - * Restore hide/show status after reload - */ - for (i = 0; i < ncells; i++) { - var cell = cells[i]; + /** + * Restore hide/show status after reload + */ if (cell.metadata.hasOwnProperty('hide_input') && cell.metadata.hide_input === true) showCell(cell, 'i', false); if (cell.metadata.hasOwnProperty('hide_output') && cell.metadata.hide_output === true) @@ -773,11 +733,9 @@ define([ events.one('notebook_loaded.Notebook', initGutter); } }); - config.load() + Jupyter.notebook.config.loaded.then(initialize); }; - Jupyter.notebook.config.loaded.then(initialize); - return { load_jupyter_extension: load_extension, load_ipython_extension: load_extension