Skip to content

Commit

Permalink
FIXES ipython-contrib#1223 and attempts to improve UX
Browse files Browse the repository at this point in the history
  • Loading branch information
consideRatio committed Feb 4, 2018
1 parent a698bbf commit d88b9bb
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions src/jupyter_contrib_nbextensions/nbextensions/init_cell/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ define([
}
);

function count_init_cells () {
console.log(log_prefix, 'counting 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 ) {
num++;
}
}
console.log(log_prefix, 'found ' + num + ' initialization cell' + (num !== 1 ? 's' : ''));
return num
}

function run_init_cells () {
console.log(log_prefix, 'running all initialization cells');
var num = 0;
Expand Down Expand Up @@ -103,23 +117,30 @@ define([
}

if (options.run_on_kernel_ready) {
if (!Jupyter.notebook.trusted) {
var num = count_init_cells();

if (num) {
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'}},
title : 'Untrusted initialization code',
body : num + ' initialization code cell' + (num !== 1 ? 's' : '') + ' was found but this notebook is untrusted.',
buttons: {
'Run code anyway': {
'class' : 'btn-primary',
'click': () => {
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);
}
},
'Perhaps later': {'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);
}
}

Expand Down

0 comments on commit d88b9bb

Please sign in to comment.