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
* FIXES ipython-contrib#1223 - Only shows the dialog about untrusted notebooks if there was any initialization cells in it.
* UX improvement - Adding a button to trust the notebook through the dialog.
  • Loading branch information
consideRatio committed Feb 4, 2018
1 parent a698bbf commit 0c768ed
Showing 1 changed file with 43 additions and 15 deletions.
58 changes: 43 additions & 15 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,24 +117,38 @@ define([
}

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;
var num = count_init_cells();

if (num) {
if (Jupyter.notebook.trusted) {
run_init_cells_asap()
}
else {
dialog.modal({
title : 'Untrusted notebook with initialization code',
body : num + ' initialization code cell' + (num !== 1 ? 's' : '') + ' was found but not run since this notebook is untrusted.',
buttons: {
'Trust notebook': {
'class' : 'btn-danger',
'click' : () => Jupyter.notebook.trust_notebook()
},
'Do nothing': {'class' : 'btn-primary'}
},
notebook: Jupyter.notebook,
keyboard_manager: Jupyter.keyboard_manager,
});
}
}
}
}

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);
function run_init_cells_asap () {
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);
}

return {
Expand Down

0 comments on commit 0c768ed

Please sign in to comment.