IPython - launch all cells below from a widget

I am trying to use a widget with multiple selections so that users can select from a list of countries and then have a widget button that, when clicked, launches all the cells below.

Displays a 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 the 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 not find a hook to run all the cells below

thanks

+4
source share
2 answers

If I understood correctly, you could do it via js.

See the following code:

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

Fulfills all cells under the active cell, so for you this might 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.

+8
source

, :

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)

, . IPython.notebook.execute_cells_below() , , .

+3

Source: https://habr.com/ru/post/1608467/


All Articles