我有一个问题,如何在笔记本完成加载后(当内核准备好时)以及用户本人手动执行代码之前,以编程方式清除\清理 Jupyter 笔记本中所有单元格的输出。基本上,我希望笔记本在加载完成后看起来很干净,并且我希望自动完成。
我怎样才能强加这样的命令clear_output()
从单个初始化单元到笔记本中的所有其他单元?
谢谢。
对于受信任的笔记本(请参阅http://jupyter-notebook.readthedocs.io/en/stable/security.html#Our-security-model http://jupyter-notebook.readthedocs.io/en/stable/security.html#Our-security-model有关受信任/不受信任笔记本的详细信息,但简而言之,为此目的,相关位是任何内容你已经在你的机器上创建了应该已经被信任了),你可以在开始时使用 JavaScript 单元,如下所示:
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);
});
这在不受信任的笔记本中不起作用,因为 JavaScript 输出已被清理,但如果您正在创建笔记本,它应该可以正常工作。如果您不想在每个笔记本中都有该单元,您甚至可以将整个内容包装到 nbextension 中。
<shameless plug>
See https://github.com/ipython-contrib/jupyter_contrib_nbextensions https://github.com/ipython-contrib/jupyter_contrib_nbextensions有关 nbextensions 的示例,或在那里提出问题以建议添加类似的内容</shameless plug>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)