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


True

In [4]:
interact(f, x=[1, 3]); # include a semi colon to suppress out cells in jupyter


3

In [5]:
interact(f, x=10);


-10

In [6]:
interact(f, x='hello');


u'hello'

In [10]:
@interact(x=True, y=1.0)
def g(x,y):
    return (x,y)


(True, 1.0)

In [11]:
def h(p,q):
    return (p,q)

In [12]:
interact(h, p=5,q=fixed(30))


(9, 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);


0

In [21]:
interact(f, x=('apple', 'orange'));


u'orange'

In [25]:
interact(f, x=(0.0, 8, 0.1));


2.0

In [28]:
@interact(x=(0.0,10.0,0.01))
def h(x=5.5):
    return x


5.5

In [31]:
interact(f, x={'one':1, 'two':2});


1

In [4]:
def multi(x):
    return x**2

In [5]:
interact(multi, x={'one':1, 'two':2, 'three':3, 'four':4});


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]:
ipywidgets.widgets.widget_box.Box

In [9]:
w.children


Out[9]:
(<ipywidgets.widgets.widget_int.IntSlider at 0x10c981c90>,
 <ipywidgets.widgets.widget_int.IntSlider at 0x10c901850>)

In [16]:
from IPython.display import display as dis
dis(w)


30

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]:
1000

In [67]:
interact(max_val, arr=list([94, 390, 8762, 980]));


u'8'

In [19]:
def greeting(label):
    return 'Hello, {w}'.format(w=label)

In [74]:
interact(greeting, x='world');


'Hello, world'

In [20]:
@interact(x='Neighbor')
def greeting_mild(x):
    return 'Would you like to be my {n}'.format(n=x)


'Would you like to be my Neighbor'

In [22]:
greeting_mild("")


Out[22]:
'Would you like to be my '

In [ ]: