In [20]:
from __future__ import print_function
from ipywidgets import interact, interactive, fixed
import ipywidgets as widgets
In [21]:
def f(x):
return x
In [22]:
interact(f, x=10);
In [23]:
interact(f, x=True);
In [24]:
interact(f, x='Hi there!');
In [25]:
@interact(x=True, y=1.0)
def g(x, y):
return (x, y)
In [26]:
def h(p, q):
return (p, q)
In [27]:
interact(h, p=5, q=fixed(20));
In [28]:
widgets.IntSlider(min=-10,max=30,step=1,value=10);
In [29]:
interact(f, x=widgets.IntSlider(min=-10,max=30,step=1,value=10));
In [30]:
interact(f, x=(0,4));
In [31]:
interact(f, x=(0,8,2));
In [32]:
interact(f, x=(0.0,10.0));
In [33]:
interact(f, x=(0.0,10.0,0.01));
In [34]:
@interact(x=(0.0,20.0,0.5))
def h(x=5.5):
return x
In [35]:
interact(f, x=['apples','oranges']);
In [36]:
interact(f, x={'one': 10, 'two': 20});
In [37]:
def anno_func(ham: str, eggs: str = 'eggs') -> str:
print("Annotations:", anno_func.__annotations__)
print("Arguments:", ham, eggs)
return ham + ' and ' + eggs
In [38]:
anno_func('lulu')
Out[38]:
In [39]:
anno_func.__annotations__
Out[39]:
In [40]:
from IPython.utils.py3compat import annotate
In [41]:
@annotate(x=True)
def f(x):
return x
In [42]:
interact(f);
In [43]:
def f(a, b):
return a+b
In [44]:
w = interactive(f, a=10, b=20)
In [45]:
type(w)
Out[45]:
In [46]:
w.children
Out[46]:
In [47]:
from IPython.display import display
display(w)
In [48]:
w.kwargs
Out[48]:
In [49]:
def slow_function(i):
print(int(i),list(x for x in range(int(i)) if
str(x)==str(x)[::-1] and
str(x**2)==str(x**2)[::-1]))
return
In [50]:
%%time
slow_function(1e6)
In [51]:
from ipywidgets import FloatSlider
interact(slow_function,i=FloatSlider(min=1e5, max=1e7, step=1e5))
In [52]:
# %%writefile -a test.py
interact(slow_function,i=FloatSlider(min=1e5, max=1e7, step=1e5),__manual=True)
In [53]:
# %%writefile -a test.py
interact(slow_function,i=FloatSlider(min=1e5, max=1e7, step=1e5,continuous_update=False))
In [54]:
# %%writefile -a test.py
x_widget = FloatSlider(min=0.0, max=10.0, step=0.05)
y_widget = FloatSlider(min=0.5, max=10.0, step=0.05, value=5.0)
# When y_widget's value changes,
# call the update_x function
# set the x_widget's max value to be two times the y_widget's value
def update_x_range(*args):
x_widget.max = 2.0 * y_widget.value
y_widget.observe(update_x_range, 'value')
def printer(x, y):
print(x, y)
interact(printer,x=x_widget, y=y_widget)
In [ ]: