require(['base/js/namespace', 'base/js/events'],
function (Jupyter, events) {
// save a reference to the cell we're currently executing inside of,
// to avoid clearing it later (which would remove this js)
var this_cell = $(element).closest('.cell').data('cell');
function clear_other_cells () {
Jupyter.notebook.get_cells().forEach(function (cell) {
if (cell.cell_type === 'code' && cell !== this_cell) {
cell.clear_output();
}
Jupyter.notebook.set_dirty(true);
});
}
if (Jupyter.notebook._fully_loaded) {
// notebook has already been fully loaded, so clear now
clear_other_cells();
}
// Also clear on any future load
// (e.g. when notebook finishes loading, or when a checkpoint is reloaded)
events.on('notebook_loaded.Notebook', clear_other_cells);
});
1 回答
对于受信任的笔记本电脑(有关受信任/不受信任的笔记本电脑的详细信息,请参阅http://jupyter-notebook.readthedocs.io/en/stable/security.html#Our-security-model,但简而言之,相关的一点是您在计算机上创建的任何内容都应该是可信任的),您可以在开头使用javascript单元格有类似的东西:
这将无法在不受信任的笔记本中运行,javascript输出将被清理,但如果您正在创建笔记本,它应该可以正常运行 . 如果你不想在每个笔记本中都有单元格,你甚至可以将整个东西包裹起来 .
<shameless plug>
有关nbextensions的示例,请参阅https://github.com/ipython-contrib/jupyter_contrib_nbextensions,或在那里提出问题建议添加类似这样的内容</shameless plug>