File size: 5,736 Bytes
fdaf774 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
define([
'jquery',
'base/js/dialog',
'base/js/events',
'base/js/namespace',
'notebook/js/celltoolbar',
'notebook/js/codecell',
], function (
$,
dialog,
events,
Jupyter,
celltoolbar,
codecell
) {
"use strict";
var CellToolbar = celltoolbar.CellToolbar;
var mod_name = 'init_cell';
var log_prefix = '[' + mod_name + ']';
var options = { // updated from server's config & nb metadata
run_on_kernel_ready: true,
};
var toolbar_preset_name = 'Initialization Cell';
var init_cell_ui_callback = CellToolbar.utils.checkbox_ui_generator(
toolbar_preset_name,
function setter (cell, value) {
if (value) {
cell.metadata.init_cell = true;
}
else {
delete cell.metadata.init_cell;
}
},
function getter (cell) {
// if init_cell is undefined, it'll be interpreted as false anyway
return cell.metadata.init_cell;
}
);
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;
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 ) {
cell.execute();
num++;
}
}
console.log(log_prefix, 'finished running ' + num + ' initialization cell' + (num !== 1 ? 's' : ''));
}
var load_ipython_extension = function() {
// register action
var prefix = 'auto';
var action_name = 'run-initialization-cells';
var action = {
icon: 'fa-calculator',
help: 'Run all initialization cells',
help_index : 'zz',
handler : run_init_cells
};
var action_full_name = Jupyter.notebook.keyboard_manager.actions.register(action, action_name, prefix);
// add toolbar button
Jupyter.toolbar.add_buttons_group([action_full_name]);
// setup things to run on loading config/notebook
Jupyter.notebook.config.loaded
.then(function update_options_from_config () {
$.extend(true, options, Jupyter.notebook.config.data[mod_name]);
}, function (reason) {
console.warn(log_prefix, 'error loading config:', reason);
})
.then(function () {
if (Jupyter.notebook._fully_loaded) {
callback_notebook_loaded();
}
events.on('notebook_loaded.Notebook', callback_notebook_loaded);
}).catch(function (reason) {
console.error(log_prefix, 'unhandled error:', reason);
});
};
function callback_notebook_loaded () {
// update from metadata
var md_opts = Jupyter.notebook.metadata[mod_name];
if (md_opts !== undefined) {
console.log(log_prefix, 'updating options from notebook metadata:', md_opts);
$.extend(true, options, md_opts);
}
// register celltoolbar presets if they haven't been already
if (CellToolbar.list_presets().indexOf(toolbar_preset_name) < 0) {
// Register a callback to create a UI element for a cell toolbar.
CellToolbar.register_callback('init_cell.is_init_cell', init_cell_ui_callback, 'code');
// Register a preset of UI elements forming a cell toolbar.
CellToolbar.register_preset(toolbar_preset_name, ['init_cell.is_init_cell'], Jupyter.notebook);
}
if (options.run_on_kernel_ready) {
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,
});
}
}
}
}
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 {
load_ipython_extension : load_ipython_extension
};
});
|