Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function monkeypatch() {
// patch Cell.execute and OutputArea.append_error for missing functionality
// find the first code cell
console.log("patching execute");
// create and destroy code cell at end
var nb = Jupyter.notebook;
var ncells = nb.ncells();
var cell = nb.insert_cell_at_index("code", ncells);
var CodeCell = Object.getPrototypeOf(cell);
var OutputArea = Object.getPrototypeOf(cell.output_area);
Jupyter.notebook.delete_cells([ncells]);
if (CodeCell.original_execute) {
// already patched
return;
}
CodeCell.original_execute = CodeCell.execute;
CodeCell.execute = my_execute;
// patch OutputArea.append_error because there is no event for errors
OutputArea.original_append_error = OutputArea.append_error;
SparkMonitor.prototype.startComm = function () {
if (this.comm) {
this.comm.close()
}
console.log('SparkMonitor: Starting Comm with kernel.')
var that = this;
if (Jupyter.notebook.kernel) {
this.comm = Jupyter.notebook.kernel.comm_manager.new_comm('SparkMonitor',
{ 'msgtype': 'openfromfrontend' });
// Register a message handler
this.comm.on_msg($.proxy(that.on_comm_msg, that));
this.comm.on_close($.proxy(that.on_comm_close, that));
}
else {
console.log("SparkMonitor: No communication established, kernel null");
}
}
async saveWorkingCopyAndGetContent() {
Jupyter.notebook.save_checkpoint();
// The function below returns the (unsaved) working copy.
return Jupyter.notebook.toJSON();
}
function show_conda_view($view) {
var d = dialog.modal({
title: 'Conda Packages',
body: $view,
open: function() {
$('#searchbox').focus();
},
keyboard_manager: Jupyter.notebook.keyboard_manager
});
d.on('hide.bs.modal', function() {
// detach the conda view so it isn't destroyed with the dialog box
$view.detach();
});
d.find('.modal-dialog').css({width: "80vw"});
}
function handle_cell_creation(evt, data) {
// the cell below the created one should be invalidated
var idx = data.index;
var cell = data.cell;
var nb = Jupyter.notebook;
var cells = Jupyter.notebook.get_cells();
var ncells = nb.ncells();
if (idx + 1 == ncells) return;
if (cells[idx + 1].element.hasClass("inorder-executed")) {
cell.element.addClass("inorder-executed");
invalidate_below(idx);
}
}
function set_notebook_metadata(config) { Jupyter.notebook.metadata.require = config; }
() => {
const segments = this.model.get('segments');
if (segments) {
const msgId = Jupyter.notebook.kernel.last_msg_id;
const cell = Jupyter.notebook.get_msg_cell(msgId);
const cellIndex = Jupyter.notebook.find_cell_index(cell);
const newCell = Jupyter.notebook.insert_cell_below('code', cellIndex);
newCell.set_text(`segments = ${segments}`);
newCell.execute();
}
},
this
function get_display_cells() {
let cells = Jupyter.notebook.get_cells();
return cells.filter(
( c ) => {
return c.cell_type === 'code' &&
c.output_area.outputs &&
c.output_area.outputs.some( d => d.output_type === 'display_data' );
} );
}
metadata(update){
let md = Jupyter.notebook.metadata;
if(update){
md.nbpresent = {
slides: this.slides.serialize()
};
}else{
return md.nbpresent || {
slides: {}
}
}
}
window.addEventListener('error', event => {
const msgId = Jupyter.notebook.kernel.last_msg_id;
const cell = Jupyter.notebook.get_msg_cell(msgId);
if (cell) {
const {error} = event;
cell.output_area.append_output({
output_type: 'error',
ename: error.name,
evalue: error.message,
traceback: ['Javascript Error', error.stack],
});
cell.output_area.element
.find('.output_subarea.output_error')
.css({'background-color': '#ff9999'});
}
});