首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在重新启动内核后重新加载jupyter笔记本?

如何在重新启动内核后重新加载jupyter笔记本?
EN

Stack Overflow用户
提问于 2019-11-05 12:30:41
回答 2查看 2.6K关注 0票数 1

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

  • 向用户发出警报
  • 重新启动笔记本内核
  • 标记已执行的单元格(即没有星号,[ ])
  • 焦点集中在下一个细胞上

或者作为另一种选择:

  • 向用户发出警报
  • 重新启动笔记本内核
  • 清除整个笔记本的所有输出。
  • 焦点再次放在第一个单元格上(比如浏览器选项卡的F5重新加载)。

我尝试了以下代码

代码语言:javascript
复制
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()

但它给出了一个错误

代码语言:javascript
复制
AttributeError: 'function' object has no attribute 'clear_output'

尽管这份文件

当我移除线时

代码语言:javascript
复制
IPython.display.clear_output();

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

怎样才能做好呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-11-12 10:03:27

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

代码语言:javascript
复制
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部件之后使用JavaScript函数来清除当前单元格的输出:

代码语言:javascript
复制
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标记中被调用为JavaScript代码,而不是在JavaScript中。

票数 2
EN

Stack Overflow用户

发布于 2019-11-05 13:11:20

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

代码语言:javascript
复制
IPython.display.clear_output();

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

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58711404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档