<commit_before>/*
 *  Copyright 2017 TWO SIGMA OPEN SOURCE, LLC
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

// This file contains the javascript that is run when the notebook is loaded.
// It contains some requirejs configuration and the `load_ipython_extension`
// which is required for any notebook extension.

// Configure requirejs
if (window.require) {
    window.require.config({
        map: {
            "*" : {
                "beakerx": "nbextensions/beakerx/index",
                "jupyter-js-widgets": "nbextensions/jupyter-js-widgets/extension"
            }
        }
    });
}

require('./../src/plot/bko-combinedplot.css');
require('./../src/plot/bko-plot.css');
require('jquery-contextmenu/dist/jquery.contextMenu.min.css');

define([
  'services/config',
  'services/kernels/comm',
  'base/js/dialog',
  'base/js/utils',
  'base/js/namespace',
  'base/js/events',
  'notebook/js/celltoolbar',
  'notebook/js/codecell',
  'require',
  'underscore',

  './htmlOutput/htmlOutput',

  // Plot JS API
  './plot/plotApi',
  './shared/bkCoreManager',
  'big.js'
], function(
  configmod,
  comm,
  dialog,
  utils,
  Jupyter,
  events,
  celltoolbar,
  codecell,
  require,
  _,
  htmlOutput,
  plotApi,
  bkCoreManager,
  big
) {
  "use strict";

  window.Big = big;

  var base_url = utils.get_body_data('baseUrl');
  var config = new configmod.ConfigSection('notebook', {base_url: base_url});
  var comm;
  var kernel_info = undefined;
  
  config.loaded.then(function() {
    console.log('beaker extension loaded');
  });

  Jupyter.notebook.events.on('kernel_ready.Kernel', function() {
    var kernel = Jupyter.notebook.kernel;
    if (!window.beaker) {
      window.beaker = {};
    }
    kernel.comm_manager.register_target('beaker.getcodecells',
      function(comm, msg) {
        comm.on_msg(function(msg) {
          if(msg.content.data.name == "CodeCells"){
            sendJupyterCodeCells(JSON.parse(msg.content.data.value));
          }
          window.beaker[msg.content.data.name] = JSON.parse(msg.content.data.value);
        });
      });
    kernel.comm_manager.register_target('beaker.autotranslation',
      function(comm, msg) {
        comm.on_msg(function(msg) {
          window.beaker[msg.content.data.name] = JSON.parse(msg.content.data.value);
        });
      });
    kernel.comm_manager.register_target('beaker.tag.run',
        function(comm, msg) {
          comm.on_msg(function(msg) {
            if(msg.content.data.runByTag != undefined){
              var notebook = Jupyter.notebook;
            	var cells = Jupyter.notebook.get_cells();
              var indexList = cells.reduce(function(acc, cell, index) {
                if (cell._metadata.tags && cell._metadata.tags.includes(msg.content.data.runByTag)) {
                  acc.push(index);
                }
                return acc;
              }, []);

              notebook.execute_cells(indexList);
            }
          	
          });
        });
    
    setBeakerxKernelParameters();
  });

  Jupyter.notebook.events.on('kernel_interrupting.Kernel', function() {
    interrupt();
  });

  function sendJupyterCodeCells(filter) {
    var comm = Jupyter.notebook.kernel.comm_manager.new_comm("beaker.getcodecells",
        null, null, null, utils.uuid());
    var data = {};
    data.code_cells = Jupyter.notebook.get_cells().filter(function (cell) {
      if (cell._metadata.tags) {
        return cell.cell_type == 'code' && cell._metadata.tags.includes(filter);
      }
      return false;
    });
    comm.send(data);
    comm.close();
  }
  
  function getKernelInfo(callBack){
    if (!kernel_info) {
      Jupyter.notebook.kernel.kernel_info(function(result) {
        kernel_info = result.content;
        console.log("kernel_info received:");
        console.log(kernel_info);
        callBack(kernel_info);
       });
    } else {
      callBack(kernel_info);
    }
  }

  function interrupt() {
    getKernelInfo(function(info) {
      if (info.beakerx) {
        interruptToKernel();
      }
    });
  }

  function interruptToKernel() {
    var kernel = Jupyter.notebook.kernel;
    var kernel_control_target_name = "kernel.control.channel";
    var comm = Jupyter.notebook.kernel.comm_manager.new_comm(kernel_control_target_name, 
                                                             null, null, null, utils.uuid());
    var data = {};
    data.kernel_interrupt = true;
    comm.send(data);
    comm.close();
  }

  function setBeakerxKernelParameters() {
    getKernelInfo(function(info) {
      if (info.beakerx) {
        setBeakerxKernelParametersToKernel();
      }
    });
  }

  function setBeakerxKernelParametersToKernel() {
    var kernel_control_target_name = "kernel.control.channel";
    var comm = Jupyter.notebook.kernel.comm_manager.new_comm(kernel_control_target_name, 
                                                             null, null, null, utils.uuid());

    var newNotebook = undefined == Jupyter.notebook.metadata.beakerx_kernel_parameters;

    if (newNotebook) {
      comm.on_msg(function(resp) {
        if (undefined != resp.content.data.kernel_control_response) {
          if ("OK" === resp.content.data.kernel_control_response) {
          } else if (undefined != resp.content.data.kernel_control_response.beakerx_kernel_parameters) {
            Jupyter.notebook.metadata.beakerx_kernel_parameters = resp.content.data.kernel_control_response.beakerx_kernel_parameters;

            var theData = {};
            if (Jupyter.notebook && Jupyter.notebook.metadata) {
              theData.beakerx_kernel_parameters = Jupyter.notebook.metadata.beakerx_kernel_parameters;
            }
            comm.send(theData);
            comm.close();
          }
        }
      });

      var data = {};
      data.get_default_shell = true;
      comm.send(data);
    } else {
      var data = {};
      if (Jupyter.notebook && Jupyter.notebook.metadata) {
        data.beakerx_kernel_parameters = Jupyter.notebook.metadata.beakerx_kernel_parameters;
      }
      comm.send(data);
      comm.close();
    }
  }

  // ________ init cell extension code
  var CellToolbar = celltoolbar.CellToolbar;

  var mod_name = 'init_cell';
  var log_prefix = '[' + mod_name + ']';
  var options = { // updated from server's config & nb metadata
    run_on_kernel_ready: true,
  };

  var toolbar_preset_name = 'Initialization Cell';
  var init_cell_ui_callback = CellToolbar.utils.checkbox_ui_generator(
    toolbar_preset_name,
    function setter (cell, value) {
      if (value) {
        cell.metadata.init_cell = true;
      }
      else {
        delete cell.metadata.init_cell;
      }
    },
    function getter (cell) {
      // if init_cell is undefined, it'll be interpreted as false anyway
      return cell.metadata.init_cell;
    }
  );

  function run_init_cells () {
    console.log(log_prefix, 'running all initialization cells');
    var num = 0;
    var cells = Jupyter.notebook.get_cells();
    for (var ii = 0; ii < cells.length; ii++) {
      var cell = cells[ii];
      if ((cell instanceof codecell.CodeCell) && cell.metadata.init_cell === true ) {
        cell.execute();
        num++;
      }
    }
    console.log(log_prefix, 'finished running ' + num + ' initialization cell' + (num !== 1 ? 's' : ''));
  }

  function callback_notebook_loaded () {
    // update from metadata
    var md_opts = Jupyter.notebook.metadata[mod_name];
    if (md_opts !== undefined) {
      console.log(log_prefix, 'updating options from notebook metadata:', md_opts);
      $.extend(true, options, md_opts);
    }

    // register celltoolbar presets if they haven't been already
    if (CellToolbar.list_presets().indexOf(toolbar_preset_name) < 0) {
      // Register a callback to create a UI element for a cell toolbar.
      CellToolbar.register_callback('init_cell.is_init_cell', init_cell_ui_callback, 'code');
      // Register a preset of UI elements forming a cell toolbar.
      CellToolbar.register_preset(toolbar_preset_name, ['init_cell.is_init_cell'], Jupyter.notebook);
    }

    if (options.run_on_kernel_ready) {
      if (!Jupyter.notebook.trusted) {
        dialog.modal({
          title : 'Initialization cells in untrusted notebook',
          body : 'This notebook is not trusted, so initialization cells will not be automatically run on kernel load. You can still run them manually, though.',
          buttons: {'OK': {'class' : 'btn-primary'}},
          notebook: Jupyter.notebook,
          keyboard_manager: Jupyter.keyboard_manager,
        });
        return;
      }

      if (Jupyter.notebook && Jupyter.notebook.kernel && Jupyter.notebook.kernel.info_reply.status === 'ok') {
        // kernel is already ready
        run_init_cells();
      }
      // whenever a (new) kernel  becomes ready, run all initialization cells
      events.on('kernel_ready.Kernel', run_init_cells);
    }<commit_msg>#5449 fix init cells modal run to ofen (#5451)<commit_after>/*
 *  Copyright 2017 TWO SIGMA OPEN SOURCE, LLC
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

// This file contains the javascript that is run when the notebook is loaded.
// It contains some requirejs configuration and the `load_ipython_extension`
// which is required for any notebook extension.

// Configure requirejs
if (window.require) {
    window.require.config({
        map: {
            "*" : {
                "beakerx": "nbextensions/beakerx/index",
                "jupyter-js-widgets": "nbextensions/jupyter-js-widgets/extension"
            }
        }
    });
}

require('./../src/plot/bko-combinedplot.css');
require('./../src/plot/bko-plot.css');
require('jquery-contextmenu/dist/jquery.contextMenu.min.css');

define([
  'services/config',
  'services/kernels/comm',
  'base/js/dialog',
  'base/js/utils',
  'base/js/namespace',
  'base/js/events',
  'notebook/js/celltoolbar',
  'notebook/js/codecell',
  'require',
  'underscore',

  './htmlOutput/htmlOutput',

  // Plot JS API
  './plot/plotApi',
  './shared/bkCoreManager',
  'big.js'
], function(
  configmod,
  comm,
  dialog,
  utils,
  Jupyter,
  events,
  celltoolbar,
  codecell,
  require,
  _,
  htmlOutput,
  plotApi,
  bkCoreManager,
  big
) {
  "use strict";

  window.Big = big;

  var base_url = utils.get_body_data('baseUrl');
  var config = new configmod.ConfigSection('notebook', {base_url: base_url});
  var comm;
  var kernel_info = undefined;
  
  config.loaded.then(function() {
    console.log('beaker extension loaded');
  });

  Jupyter.notebook.events.on('kernel_ready.Kernel', function() {
    var kernel = Jupyter.notebook.kernel;
    if (!window.beaker) {
      window.beaker = {};
    }
    kernel.comm_manager.register_target('beaker.getcodecells',
      function(comm, msg) {
        comm.on_msg(function(msg) {
          if(msg.content.data.name == "CodeCells"){
            sendJupyterCodeCells(JSON.parse(msg.content.data.value));
          }
          window.beaker[msg.content.data.name] = JSON.parse(msg.content.data.value);
        });
      });
    kernel.comm_manager.register_target('beaker.autotranslation',
      function(comm, msg) {
        comm.on_msg(function(msg) {
          window.beaker[msg.content.data.name] = JSON.parse(msg.content.data.value);
        });
      });
    kernel.comm_manager.register_target('beaker.tag.run',
        function(comm, msg) {
          comm.on_msg(function(msg) {
            if(msg.content.data.runByTag != undefined){
              var notebook = Jupyter.notebook;
            	var cells = Jupyter.notebook.get_cells();
              var indexList = cells.reduce(function(acc, cell, index) {
                if (cell._metadata.tags && cell._metadata.tags.includes(msg.content.data.runByTag)) {
                  acc.push(index);
                }
                return acc;
              }, []);

              notebook.execute_cells(indexList);
            }
          	
          });
        });
    
    setBeakerxKernelParameters();
  });

  Jupyter.notebook.events.on('kernel_interrupting.Kernel', function() {
    interrupt();
  });

  function sendJupyterCodeCells(filter) {
    var comm = Jupyter.notebook.kernel.comm_manager.new_comm("beaker.getcodecells",
        null, null, null, utils.uuid());
    var data = {};
    data.code_cells = Jupyter.notebook.get_cells().filter(function (cell) {
      if (cell._metadata.tags) {
        return cell.cell_type == 'code' && cell._metadata.tags.includes(filter);
      }
      return false;
    });
    comm.send(data);
    comm.close();
  }
  
  function getKernelInfo(callBack){
    if (!kernel_info) {
      Jupyter.notebook.kernel.kernel_info(function(result) {
        kernel_info = result.content;
        console.log("kernel_info received:");
        console.log(kernel_info);
        callBack(kernel_info);
       });
    } else {
      callBack(kernel_info);
    }
  }

  function interrupt() {
    getKernelInfo(function(info) {
      if (info.beakerx) {
        interruptToKernel();
      }
    });
  }

  function interruptToKernel() {
    var kernel = Jupyter.notebook.kernel;
    var kernel_control_target_name = "kernel.control.channel";
    var comm = Jupyter.notebook.kernel.comm_manager.new_comm(kernel_control_target_name, 
                                                             null, null, null, utils.uuid());
    var data = {};
    data.kernel_interrupt = true;
    comm.send(data);
    comm.close();
  }

  function setBeakerxKernelParameters() {
    getKernelInfo(function(info) {
      if (info.beakerx) {
        setBeakerxKernelParametersToKernel();
      }
    });
  }

  function setBeakerxKernelParametersToKernel() {
    var kernel_control_target_name = "kernel.control.channel";
    var comm = Jupyter.notebook.kernel.comm_manager.new_comm(kernel_control_target_name, 
                                                             null, null, null, utils.uuid());

    var newNotebook = undefined == Jupyter.notebook.metadata.beakerx_kernel_parameters;

    if (newNotebook) {
      comm.on_msg(function(resp) {
        if (undefined != resp.content.data.kernel_control_response) {
          if ("OK" === resp.content.data.kernel_control_response) {
          } else if (undefined != resp.content.data.kernel_control_response.beakerx_kernel_parameters) {
            Jupyter.notebook.metadata.beakerx_kernel_parameters = resp.content.data.kernel_control_response.beakerx_kernel_parameters;

            var theData = {};
            if (Jupyter.notebook && Jupyter.notebook.metadata) {
              theData.beakerx_kernel_parameters = Jupyter.notebook.metadata.beakerx_kernel_parameters;
            }
            comm.send(theData);
            comm.close();
          }
        }
      });

      var data = {};
      data.get_default_shell = true;
      comm.send(data);
    } else {
      var data = {};
      if (Jupyter.notebook && Jupyter.notebook.metadata) {
        data.beakerx_kernel_parameters = Jupyter.notebook.metadata.beakerx_kernel_parameters;
      }
      comm.send(data);
      comm.close();
    }
  }

  // ________ init cell extension code
  var CellToolbar = celltoolbar.CellToolbar;

  var mod_name = 'init_cell';
  var log_prefix = '[' + mod_name + ']';
  var options = { // updated from server's config & nb metadata
    run_on_kernel_ready: true,
  };

  var toolbar_preset_name = 'Initialization Cell';
  var init_cell_ui_callback = CellToolbar.utils.checkbox_ui_generator(
    toolbar_preset_name,
    function setter (cell, value) {
      if (value) {
        cell.metadata.init_cell = true;
      }
      else {
        delete cell.metadata.init_cell;
      }
    },
    function getter (cell) {
      // if init_cell is undefined, it'll be interpreted as false anyway
      return cell.metadata.init_cell;
    }
  );

  function run_init_cells () {
    console.log(log_prefix, 'running all initialization cells');
    var num = 0;
    var cells = get_init_cells();

    for (var ii = 0; ii < cells.length; ii++) {
      cells[ii].execute();
      num++;
    }
    console.log(log_prefix, 'finished running ' + num + ' initialization cell' + (num !== 1 ? 's' : ''));
  }

  function get_init_cells () {
    var cells = Jupyter.notebook.get_cells();

    return cells.filter(function(cell) {
      return ((cell instanceof codecell.CodeCell) && cell.metadata.init_cell === true );
    });
  }

  function callback_notebook_loaded () {
    var initCells = get_init_cells();
    // update from metadata
    var md_opts = Jupyter.notebook.metadata[mod_name];
    if (md_opts !== undefined) {
      console.log(log_prefix, 'updating options from notebook metadata:', md_opts);
      $.extend(true, options, md_opts);
    }

    // register celltoolbar presets if they haven't been already
    if (CellToolbar.list_presets().indexOf(toolbar_preset_name) < 0) {
      // Register a callback to create a UI element for a cell toolbar.
      CellToolbar.register_callback('init_cell.is_init_cell', init_cell_ui_callback, 'code');
      // Register a preset of UI elements forming a cell toolbar.
      CellToolbar.register_preset(toolbar_preset_name, ['init_cell.is_init_cell'], Jupyter.notebook);
    }

    if (options.run_on_kernel_ready && initCells.length) {
      if (!Jupyter.notebook.trusted) {
        dialog.modal({
          title : 'Initialization cells in untrusted notebook',
          body : 'This notebook is not trusted, so initialization cells will not be automatically run on kernel load. You can still run them manually, though.',
          buttons: {'OK': {'class' : 'btn-primary'}},
          notebook: Jupyter.notebook,
          keyboard_manager: Jupyter.keyboard_manager,
        });
        return;
      }

      if (Jupyter.notebook && Jupyter.notebook.kernel && Jupyter.notebook.kernel.info_reply.status === 'ok') {
        // kernel is already ready
        run_init_cells();
      }
      // whenever a (new) kernel  becomes ready, run all initialization cells
      events.on('kernel_ready.Kernel', run_init_cells);
    }
{'fraction_non_alphanumeric': 0.1213393513173087, 'fraction_numerical': 0.001679437388474861, 'mean_word_length': 2.5851364063969897, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1046574', 'n_tokens_mistral': 5953, 'n_tokens_neox': 5833, 'n_words': 1176}