Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter notebook run all cells on open

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.

like image 496
Markus Schanta Avatar asked Aug 13 '15 09:08

Markus Schanta


People also ask

How do I run the whole notebook?

You can run the notebook document step-by-step (one cell a time) by pressing shift + enter. You can run the whole notebook in a single step by clicking on the menu Cell -> Run All.

What is the shortcut key to execute cell in jupyter notebook?

There are two other keyboard shortcuts for running code: Alt-Enter runs the current cell and inserts a new one below. Ctrl-Enter run the current cell and enters command mode.

How do I run a jupyter notebook script automatically?

How do I schedule a Jupyter Notebook to run? When your Jupyter notebook is ready for scheduling, open the Schedule runs option inside the Computation tab or access it from the Run menu in Datalore. Then choose the run interval (hourly, daily, weekly, monthly) and the time zone.


2 Answers

  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!

like image 79
ankostis Avatar answered Sep 24 '22 22:09

ankostis


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.

like image 33
thesamovar Avatar answered Sep 24 '22 22:09

thesamovar