17

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.

5
  • Not sure I follow the argument: As far as I understand, the Python code is run on the server hosting the notebook. How can it execute something on the client side (apart from Javascript, which could be embedded in any site you visit)? Aug 13, 2015 at 10:17
  • So then is OK if someone put os.system("rm -rf *") in the automatic running notebook? Is not OK for me, I use Linux desktop. If it is on server, and not as root, it will still delete files on server that someone may need or cause maintenance issue. I suppose if you use throwaway VMs it might be OK, and indeed the jupyter-based berkeley spark course was run that way (throwaway VM, not autorunning script).
    – Paul
    Aug 13, 2015 at 10:32
  • 1
    note: there is such a thing as a "trusted" ipython notebook, marked from the command line with ipython trust filename.ipynb or a trust command in the website file menu.
    – Paul
    Aug 13, 2015 at 10:53
  • 2
    Don't see how that would be different from someone manually entering os.system("rm -rf *") and running it. Both a notebook hosted on a server and a notebook hosted on a server that executes automatically can execute arbitrary code on the server. Aug 13, 2015 at 11:35
  • I agree with @MarkusSchanta, this is not a valid argument not to answer this SO question: Either a user is not allowed to enter commands (or the notebook is not marked as "trusted" yet), or she is allowed to run arbitrary commands, and she can cause "harm" either interactively, or at a later stage, when auto-running the notebook.
    – ankostis
    Aug 8, 2016 at 10:49

4 Answers 4

26
  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!

6

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.

1
3

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.

6
  • 1
    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. Aug 13, 2015 at 9:47
  • 1
    When I open the Julia notebook on that site, the plots appear within the notebook, but the status at the top says "Kernel starting, please wait", which makes me think the plots are just saved output and the cells are not actually being run on load. Maybe I'm missing something, but how is what you're wanting to achieve different from just saving a notebook with output?
    – Simon
    Aug 13, 2015 at 9:53
  • 1
    Some of my cells contain widgets and those are not displayed until the cell is run. Simple example: from IPython.display import display from IPython.html.widgets import IntSlider w = IntSlider() display(w) Aug 13, 2015 at 10:08
  • 1
    You may wish to improve your answer by citing portions of this: ipython.org/ipython-doc/dev/notebook/security.html like this part "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. Like any other program, once a user decides to execute code in a notebook, it is considered trusted, and should be allowed to do anything."
    – Paul
    Aug 13, 2015 at 10:46
  • 1
    As far as I understand, the Python code is run on the server hosting the notebook. I don't think that executing arbitrary code on the server would be a security concern for the client therefore. For the server side it does not matter from a security point of view whether the code is executed upon opening or manually. Aug 13, 2015 at 11:43
2

The Scenes Jupyterlab extension worked very well for me.

Briefly, a "scene" is a collection of cells grouped together. One of the scenes can be declared an "init" scene. Its cells will be run automatically when the notebook is opened, and this is exactly what I needed.

Additionally, the user can switch between scenes, activating/deactivating a bunch of notebook cells.

The concept comes from Mathematica. For more details please refer to the link above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.