Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use ipywidget interactive() with option manual=True

I am updating a jupyter notebooks with interactive widgets to a more recent version (ipywidgets 7.2.1).

It used to have interactive() functions that were executed manually by clicking on a button (feature __manual=True). However now I cannot reproduce the same behavior.

Here is a minimal example:

from ipywidgets import interactive, interact_manual
import ipywidgets as widgets

def do_sth(x):
    #do sth with the argument passed
    print("done "+str(x))

nb = widgets.BoundedIntText(description='Number:')

#Interaction in accordion nested in a tab
tab = widgets.Tab()
tab.set_title(0, 'Page 1')

#old method 
#problem: it is not manual anymore
w = interactive(do_sth, x=nb, __manual=True)

#new solution 1
#problem: the widget appears also outside the tab/accordion
w1 = interact_manual(do_sth, x=nb)
w1.widget.children[1].description = 'do sth' #seems a bit of a hack

#new solution 2
w2 = interactive(do_sth, x=nb, manual=True) #does no pass the manual option
#if I set it manually with:
#w2.manual = True 
#It generates an error (AttributeError: 'interactive' object has no attribute 'manual_button')

accordion = widgets.Accordion(children=[w, w1.widget, w2])
accordion.set_title(0, 'old interaction 0')
accordion.set_title(1, 'new interaction 1')
accordion.set_title(2, 'new interaction 2')

tab.children = [accordion]

tab

Is it possible to use solution 1 and prevent the widget from appearing twice? Otherwise, is there another way to do this?

like image 518
Ely Avatar asked Aug 29 '18 19:08

Ely


People also ask

How do Ipywidgets work?

Ipywidgets provides a list of functions that can let us create widgets UI for any of our existing functions. It'll create widgets by itself by looking at the parameters of functions and creating widgets UI with all parameters represented as one widget. It's a very good way to start using ipywidgets.


1 Answers

Seems they may have moved it into a dict when adding the ability to relabel the button.

Try

w2 = interactive(do_sth, {'manual' : True, 'manual_name' : 'Do Something'}, x=nb)

like image 92
Dekamundo Avatar answered Oct 20 '22 09:10

Dekamundo