In [2]:
from ipywidgets import interact, interactive, fixed
import ipywidgets as widgets
In [2]:
def f(x):
return x
In [3]:
interact(f, x=True); # interact will return an interactive element based on the params you pass
In [4]:
interact(f, x=[1, 3]); # include a semi colon to suppress out cells in jupyter
In [5]:
interact(f, x=10);
In [6]:
interact(f, x='hello');
In [10]:
@interact(x=True, y=1.0)
def g(x,y):
return (x,y)
In [11]:
def h(p,q):
return (p,q)
In [12]:
interact(h, p=5,q=fixed(30))
In [16]:
# this is specifying which widget is going to be used
# previously, we let the input type determine what would be returned
# in this case, we specifically say to use the widgets.IntSlider
# and to have a min/max value, along with a step and a starting value
interact(f, x = widgets.IntSlider(min=0, max=20, step=2), value = 0);
In [21]:
interact(f, x=('apple', 'orange'));
In [25]:
interact(f, x=(0.0, 8, 0.1));
In [28]:
@interact(x=(0.0,10.0,0.01))
def h(x=5.5):
return x
In [31]:
interact(f, x={'one':1, 'two':2});
In [4]:
def multi(x):
return x**2
In [5]:
interact(multi, x={'one':1, 'two':2, 'three':3, 'four':4});
In [6]:
def gef(a,b):
return a+b
In [7]:
w = interactive(gef,a=10, b=20)
In [8]:
type(w)
Out[8]:
In [9]:
w.children
Out[9]:
In [16]:
from IPython.display import display as dis
dis(w)
In [17]:
def max_val(arr):
return reduce(lambda x,y: x if x>y else y, arr)
In [18]:
lst = [234, 232, 12, 51, 59, 1000]
max_val(lst)
Out[18]:
In [67]:
interact(max_val, arr=list([94, 390, 8762, 980]));
In [19]:
def greeting(label):
return 'Hello, {w}'.format(w=label)
In [74]:
interact(greeting, x='world');
In [20]:
@interact(x='Neighbor')
def greeting_mild(x):
return 'Would you like to be my {n}'.format(n=x)
In [22]:
greeting_mild("")
Out[22]:
In [ ]: