Skip to content

Commit 53eb1b8

Browse files
authored
Merge pull request #1226 from consideRatio/init_cell_dialog
FIXES #1223 and improves the user experience
2 parents a698bbf + 00c058f commit 53eb1b8

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ New features and bugfixes:
118118
- `code_prettify` Update `code_prettify.yaml`
119119
[#1162](https://github.com/ipython-contrib/jupyter_contrib_nbextensions/pull/1162)
120120
[@fehiepsi](https://github.com/fehiepsi)
121+
- `init_cell`
122+
* Warning dialog now always relevant, fixes [#1223].(https://github.com/ipython-contrib/jupyter_contrib_nbextensions/issues/1223)
123+
[#1226](https://github.com/ipython-contrib/jupyter_contrib_nbextensions/pull/1226)
124+
[@consideratio](https://github.com/consideratio)
121125

122126

123127
0.3.3

src/jupyter_contrib_nbextensions/nbextensions/init_cell/main.js

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ define([
4040
}
4141
);
4242

43+
function count_init_cells () {
44+
console.log(log_prefix, 'counting initialization cells');
45+
var num = 0;
46+
var cells = Jupyter.notebook.get_cells();
47+
for (var ii = 0; ii < cells.length; ii++) {
48+
var cell = cells[ii];
49+
if ((cell instanceof codecell.CodeCell) && cell.metadata.init_cell === true ) {
50+
num++;
51+
}
52+
}
53+
console.log(log_prefix, 'found ' + num + ' initialization cell' + (num !== 1 ? 's' : ''));
54+
return num
55+
}
56+
4357
function run_init_cells () {
4458
console.log(log_prefix, 'running all initialization cells');
4559
var num = 0;
@@ -103,24 +117,38 @@ define([
103117
}
104118

105119
if (options.run_on_kernel_ready) {
106-
if (!Jupyter.notebook.trusted) {
107-
dialog.modal({
108-
title : 'Initialization cells in untrusted notebook',
109-
body : 'This notebook is not trusted, so initialization cells will not be automatically run on kernel load. You can still run them manually, though.',
110-
buttons: {'OK': {'class' : 'btn-primary'}},
111-
notebook: Jupyter.notebook,
112-
keyboard_manager: Jupyter.keyboard_manager,
113-
});
114-
return;
120+
var num = count_init_cells();
121+
122+
if (num) {
123+
if (Jupyter.notebook.trusted) {
124+
run_init_cells_asap()
125+
}
126+
else {
127+
dialog.modal({
128+
title : 'Untrusted notebook with initialization code',
129+
body : num + ' initialization code cell' + (num !== 1 ? 's' : '') + ' was found but not run since this notebook is untrusted.',
130+
buttons: {
131+
'Trust notebook': {
132+
'class' : 'btn-danger',
133+
'click' : () => Jupyter.notebook.trust_notebook()
134+
},
135+
'Do nothing': {'class' : 'btn-primary'}
136+
},
137+
notebook: Jupyter.notebook,
138+
keyboard_manager: Jupyter.keyboard_manager,
139+
});
140+
}
115141
}
142+
}
143+
}
116144

117-
if (Jupyter.notebook && Jupyter.notebook.kernel && Jupyter.notebook.kernel.info_reply.status === 'ok') {
118-
// kernel is already ready
119-
run_init_cells();
120-
}
121-
// whenever a (new) kernel becomes ready, run all initialization cells
122-
events.on('kernel_ready.Kernel', run_init_cells);
145+
function run_init_cells_asap () {
146+
if (Jupyter.notebook && Jupyter.notebook.kernel && Jupyter.notebook.kernel.info_reply.status === 'ok') {
147+
// kernel is already ready
148+
run_init_cells();
123149
}
150+
// whenever a (new) kernel becomes ready, run all initialization cells
151+
events.on('kernel_ready.Kernel', run_init_cells);
124152
}
125153

126154
return {

0 commit comments

Comments
 (0)