内核重启后如何重新加载Jupyter Notebook?

时间:2019-11-05 12:30:41

标签: python jupyter-notebook

在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(!)警报,看起来好像执行了下一个单元格。此外,未清除单元格,当前单元格的括号([*]中仍带有星号。

如何正确执行?

2 个答案:

答案 0 :(得分: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 <script>标记中作为JavaScript代码而不是在Python中被调用。

答案 1 :(得分:-1)

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

IPython.display.clear_output();

在我的jupyter笔记本中成功运行而没有问题,您确定您的jupyter笔记本版本是最新版本还是与documentation中的版本不同?不足为奇的是,不同的版本可能具有不同的功能名称,甚至整个结构都可能有所不同。