【问题标题】:How to reload a jupyter notebook after a kernel restart?内核重启后如何重新加载jupyter笔记本?
【发布时间】:2019-11-05 12:30:41
【问题描述】:

在一个 jupyter 笔记本中,我想在 python 中定义一个函数,当调用它时,它会执行以下操作:

  • 向用户发出警报
  • 重启笔记本内核
  • 将执行的单元格标记为已完成(即没有星号,[ ]
  • 焦点在下一个单元格

或者作为替代:

  • 向用户发出警报
  • 重启笔记本内核
  • 清除整个笔记本的所有输出
  • 焦点再次位于第一个单元格上(就像 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'

尽管this documentation

当我删除线时

IPython.display.clear_output();

然后内核重新启动,但我收到 2(!)个警报,看起来执行下一个单元格。此外,单元格没有被清除,当前单元格的括号中仍然有星号([*])。

如何正确做?

【问题讨论】:

    标签: python jupyter-notebook


    【解决方案1】:

    此代码执行您所描述的替代选项,并在重新加载之前添加了一个检查点,以便输出保持清除:

    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() 函数将当前单元格的输出清除为好吧:

    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 &lt;script&gt; 标记中作为 JavaScript 代码而不是在 Python 中调用的。

    【讨论】:

    • 似乎没有清除笔记本。在调用 reload() 之前,我仍然有其他单元格的输出。
    • 这很奇怪,因为它对我来说很好用。如果您删除最后几行(// save checkpoint so output stays cleared after reload 之后的所有内容),它有什么作用吗?
    • 嗯,这很有趣。似乎要重新启动内核,清除所有输出,然后我在下一个单元格中。也许可以将其集成到解决方案中...
    • 也许我不需要回到第一个单元格(第一个单元格应该检查环境,如果需要安装包并重新启动内核,因此正在使用这些新包)。
    • 看起来您的第二个解决方案是我真正可以用于实际用途的解决方案。非常感谢!
    【解决方案2】:

    我不确定您遇到的其他问题,但代码

    IPython.display.clear_output();
    

    在我的 jupyter notebook 中成功运行,没有问题,您确定您的 jupyter notebook 版本是最新的还是与documentation 中的版本相同?不同的版本可能有不同的函数名称,甚至整个结构可能会有所不同,这并不奇怪。

    【讨论】:

    • 也许该命令有效并清除了代码 - 但这个独立代码对我的实际问题没有帮助。此外,它不起作用(TypeError: 'module' object is not callableAttributeError: 'function' object has no attribute 'clear_output')。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 2016-07-11
    相关资源
    最近更新 更多