Jupyter notebook 在打开时运行所有单元格 [英] Jupyter notebook run all cells on open

查看:115
本文介绍了Jupyter notebook 在打开时运行所有单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Jupyter 笔记本,我正在尝试以某种方式设置它,以便在打开笔记本时自动运行所有单元格.

此行为与包含小部件的笔记本的保存输出不同.小部件似乎只有在包含它们的单元格运行时才会为我呈现.考虑以下示例:

from IPython.display 导入显示从 IPython.html.widgets 导入 IntSliderw = IntSlider()显示(宽)

在执行单元格之前不会显示滑块.

这是否可以通过 Notebook Metadata 或配置文件来完成?

:构建 Electrona> 基于笔记本的应用程序.

  • 仪表板:允许预配置笔记本网格的官方"努力-单元输出(仪表板"),将它们打包并作为独立的网络应用提供.
  • 您可以在 这篇文章.

    争议

    已经询问了类似的问题 之前其他网站,但在这个googlegroup线程中,有人提交了一个解决方案,群主删除了它(!),显然是为了保护地球上的生命:-)所以,小心点!

    I have a Jupyter noteboook and I'm trying to set it up in a way so that all cells are ran automatically when the notebook is opened.

    This behaviour is different from saved output for notebooks which contain widgets. Widgets only seem to get rendered for me when the cells containing them are run. Consider the following example:

    from IPython.display import display
    from IPython.html.widgets import IntSlider
    
    w = IntSlider()
    display(w)
    

    The slider is not displayed until the cell is executed.

    Is this something that can be accomplished through Notebook Metadata or configuration files?

    EDIT: https://try.jupyter.org/ seems to be doing something like this: Notice that the notebooks are not running when you open the page and display output when they are opened.

    EDIT2: Adding example.

    解决方案

    1. Paste the snippet below in a normal(code) cell,
    2. execute it (hit [Ctrl + Enter]), and
    3. Save the notebook.

    The next time you (re)load it, all cells will run and a checkpoint will be saved with their refreshed outputs.

    %%html
    <script>
        // AUTORUN ALL CELLS ON NOTEBOOK-LOAD!
        require(
            ['base/js/namespace', 'jquery'], 
            function(jupyter, $) {
                $(jupyter.events).on("kernel_ready.Kernel", function () {
                    console.log("Auto-running all cells-below...");
                    jupyter.actions.call('jupyter-notebook:run-all-cells-below');
                    jupyter.actions.call('jupyter-notebook:save-notebook');
                });
            }
        );
    </script>
    

    Note that if you clear the output of the above cell, you have to repeat steps 2 and 3.

    TIP

    You may consider these more appropriate solutions for what you are probably trying to achieve:

    • Jupyer Thebe: embed code-snippets in static pages communicating with ipython-kernels backends.
    • nteract: Build Electron-based applications from notebooks.
    • Dashboards: The "official"efforts to allow to pre-configure a grid of notebook-cell outputs ("dashboards"), package and serve them as standalone web apps.

    You can find a summary of the situation in this article.

    Controversy

    Similar questions has been asked before in other sites, but in this googlegroup thread, someone submitted a solution, and the group moderator erased it(!), obviously to preserve life on earth :-) So, be careful with it!

    这篇关于Jupyter notebook 在打开时运行所有单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆