Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPython - Run all cells below from a widget

I'm trying use a multi select widget to enable users to select from a list of countries, and then have a widget button which, when clicked, runs all the cells below.

This displays the list.

from IPython.display import display
w = widgets.SelectMultiple(

    description="Select up to five countries",
    options=dfCountries['name'].tolist()   
)
display(w)

And I want something like this to run all cells below:

def run_all(button):
    get_ipython().run_cell()

button = widgets.Button(description="Create next input")
button.on_click(run_all)
display(button)

But I can't find the hook to 'run all cells below

Thanks

like image 306
colster Avatar asked Sep 22 '15 10:09

colster


People also ask

How do you run multiple cells in a Jupyter notebook?

First, you will need to install jupyter nbextensions configurator as described here. Then search for "Initialization cells" from inside the search bar of the Jupyter nbextension manager. A check box at the top of every cell will appear. You can select which cells to be marked as initialization cells.

What does %% capture do?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.


3 Answers

If I understood correctly you could do that via js.

See the following code:

from IPython.display import Javascript
Javascript('IPython.notebook.execute_cells_below()')

Will execute all the cells below the active cell so for you button it could be something like:

from IPython.display import Javascript, display
from ipywidgets import widgets

def run_all(ev):
    display(Javascript('IPython.notebook.execute_cells_below()'))

button = widgets.Button(description="Create next input")
button.on_click(run_all)
display(button)

Let me know if this is what you need.

like image 160
kikocorreoso Avatar answered Sep 28 '22 09:09

kikocorreoso


To run all cells below the current cell without executing the cell that has this button:

from IPython.display import Javascript, display
from ipywidgets import widgets

def run_all(ev):
    display(Javascript('IPython.notebook.execute_cell_range(IPython.notebook.get_selected_index()+1, IPython.notebook.ncells())'))

button = widgets.Button(description="Run all below")
button.on_click(run_all)
display(button)

This allows the current cell to also prompt for other inputs and those input values to be preserved. IPython.notebook.execute_cells_below() will execute the current cell and if other inputs are also displayed in this cell they will get their default values.

like image 31
Michael McRoberts Avatar answered Sep 28 '22 11:09

Michael McRoberts


You've already received some excellent suggestions, but I'd just like to mention a pretty flexible option with HTML:

Code:

from IPython.core.display import HTML
HTML('''<script> </script> <form action="javascript:IPython.notebook.execute_cells_below()"><input type="submit" id="toggleButton" value="Refresh"></form>''')

This will produce a button for you that runs all cells below in the notebook

A bonus feature with this approach is that the layout of your button will follow a theme selection if you're using Jupyter Themes from Dunovank on github https://github.com/dunovank/jupyter-themes

I tried to attach a screenshot, but I can't do that yet.

like image 26
Stian Avatar answered Sep 28 '22 10:09

Stian