Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIXES #1223 and improves the user experience #1226

Merged
merged 2 commits into from
Feb 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ New features and bugfixes:
- `code_prettify` Update `code_prettify.yaml`
[#1162](https://github.com/ipython-contrib/jupyter_contrib_nbextensions/pull/1162)
[@fehiepsi](https://github.com/fehiepsi)
- `init_cell`
* Warning dialog now always relevant, fixes [#1223].(https://github.com/ipython-contrib/jupyter_contrib_nbextensions/issues/1223)
[#1226](https://github.com/ipython-contrib/jupyter_contrib_nbextensions/pull/1226)
[@consideratio](https://github.com/consideratio)


0.3.3
Expand Down
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