Jupyter notebook run all cells on open

  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.
  • nteractnteract: 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!


I just found a way to do this quite easily. If you install the nbextensions package (https://github.com/ipython-contrib/jupyter_contrib_nbextensions), one of the extensions is called "Initialization cells" and allows you to mark certain cells to run automatically when the notebook is loaded.


I don't believe this is possible.

ipython does not execute code unless it is 1) intentional and 2) trusted. Otherwise you'll run into situations where you load up notebooks that contain malicious code.

You can check details of ipythons security model here: https://ipython.org/ipython-doc/dev/notebook/security.html . Specifically the section that talks about code execution upon notebook opening: "The security problem we need to solve is that no code should execute just because a user has opened a notebook that they did not write"

While you can set explicit trust on a notebook, I'm not sure if this will then also allow automatic code execution as well. I haven't seen anything of the sort, but maybe I just haven't been looking hard enough. I've seen elsewhere that automatic code execution isn't something that's available in the core ipython package though. Check this issue here: https://github.com/ivanov/ipython-trainingwheels/issues/35

Beyond trust, another reason I suspect this isn't possible is because 1) automatic code execution will replace any existing output that is currently saved in the notebook, which may not be ideal, and 2) some notebooks may contain complex code that is computationally expensive, which you wouldn't want to be running every time you opened the notebook.