from ipywidgets import interact, interactive, fixed, interact_manual
And here some standard python packages to show the usage:
import matplotlib.pyplot as plt
import numpy as np
interact
¶The simplest possibility is to use the interact()
-function, with a passed function. Arguments are automatically adjusted to interaction widgets:
def sum_func(a=1.,b=3.):
return a+b
interact(sum_func)
interactive(children=(FloatSlider(value=1.0, description='a', max=3.0, min=-1.0), FloatSlider(value=3.0, descr…
<function __main__.sum_func(a=1.0, b=3.0)>
To define the ranges:
interact(sum_func, a=(1,5,0.1), b=(20,30,.05))
interactive(children=(FloatSlider(value=1.0, description='a', max=5.0, min=1.0), FloatSlider(value=20.0, descr…
<function __main__.sum_func(a=1.0, b=3.0)>
Booleans are transfered to check boxes:
def plot_data(show_points=False, show_line=False):
xvals = np.arange(-5,5.01,.5)
yvals = xvals ** 2
plt.figure()
plt.xlim([-5,5])
plt.ylim([0,25])
if show_line:
plt.plot(xvals, yvals)
if show_points:
plt.plot(xvals, yvals, 'r.')
plt.grid()
plt.show()
interact(plot_data)
interactive(children=(Checkbox(value=False, description='show_points'), Checkbox(value=False, description='sho…
<function __main__.plot_data(show_points=False, show_line=False)>
from IPython.display import HTML
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')