问题描述
在jupyter笔记本中,我想在python中定义一个函数,该函数在被调用时会执行以下操作:
In a jupyter notebook I would like to define a function in python, which, when called, does the following:
- 向用户发出警报
- 重新启动笔记本内核
- 将已执行的单元格标记为已完成(即,没有星号的
[ ]
) - 焦点在下一个单元格上
- Gives out an alert to the user
- Restarts the notebook kernel
- Marks the executed cell as done (i.e. without a star,
[ ]
) - Focus is on the next cell
或者作为替代:
- 向用户发出警报
- 重新启动笔记本内核
- 清除整个笔记本的所有输出
- 焦点再次位于第一个单元格上(就像浏览器选项卡的F5重新加载一样).
我尝试了以下代码
from IPython.display import display, HTML
def reload():
display(HTML(
'''
<script>
alert('This notebook needs to be restarted!');
IPython.notebook.kernel.restart();
IPython.display.clear_output();
window.location.reload();
</script>
'''
))
reload()
但出现错误
AttributeError: 'function' object has no attribute 'clear_output'
尽管此文档一个>.
删除行时
IPython.display.clear_output();
然后重新启动内核,但是我收到2(!)警报,看起来好像执行了下一个单元格.另外,未清除单元格,当前单元格的括号([*]
)中仍带有星号.
then the kernel is restarted, but I get 2(!) alerts and it looks like that the execution of the next cell is performed. Also, the cells are not cleared , the current cell still does have the star in the brackets ([*]
).
如何正确执行?
推荐答案
此代码实现了您所描述的替代选项,另外在重新加载之前保存了检查点,从而使输出保持清除状态:
This code does what you described as the alternative option, with the addition of saving a checkpoint before reloading so the output stays cleared:
from IPython.display import Javascript, display
def reload():
display(
Javascript('''
// clear all output (based on: https://stackoverflow.com/a/47315713/9504155)
// 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();
}
// save checkpoint so output stays cleared after reload
IPython.notebook.save_checkpoint();
IPython.notebook.kernel.restart();
alert('This notebook needs to be restarted!');
window.location.reload(false);
'''))
reload()
如果您喜欢第一个选项(无需重新加载页面,而是专注于下一个单元格),只需删除用于保存检查点并重新加载的行,并在JavaScript部分之后使用IPython.display.clear_output()
函数将当前单元格的输出清除为好吧:
If you prefer the first option (without reloading the page, focusing on the next cell instead) just remove the lines for saving a checkpoint and reloading and use the IPython.display.clear_output()
function after the JavaScript part to clear the current cell's output as well:
from IPython.display import Javascript, clear_output, display
def reload():
display(
Javascript('''
// clear all output (based on: https://stackoverflow.com/a/47315713/9504155)
// 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();
}
IPython.notebook.kernel.restart();
alert('Notebook output cleared!');
'''))
clear_output()
IPython.display.clear_output();
在您的代码中不起作用,因为它在HTML <script>
标记中作为JavaScript代码而不是在Python中被调用.
IPython.display.clear_output();
was not working in your code because it was called in the HTML <script>
tag as JavaScript code instead of in Python.
这篇关于内核重启后如何重新加载jupyter笔记本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!