In [1]:
from IPython.html import widgets # Widget definitions
from IPython.display import display # Used to display widgets in the notebook
Example of a button:
In [2]:
button = widgets.ButtonWidget(description="Click Me!")
display(button)
def on_button_clicked(b):
print("Button clicked.")
button.on_click(on_button_clicked)
Create and display a slider
In [3]:
mywidget = widgets.FloatSliderWidget()
display(mywidget)
This is how to access to the widget value
In [7]:
mywidget.value
Out[7]:
Example of displaying a function chaging the parameters with the sliders
In [8]:
import numpy as np
import matplotlib.pylab as plt
In [9]:
%matplotlib inline
Function parameters
In [31]:
widget_k = widgets.FloatSliderWidget()
widget_k.min = 0.1
widget_k.max = 10
widget_k.description = "k"
widget_A = widgets.FloatSliderWidget()
widget_A.min = 0.1
widget_A.max = 3
widget_A.description = "A"
display(widget_k)
display(widget_A)
Draw the function
In [29]:
def draw_sine():
k = widget_k.value
A = widget_A.value
x = np.linspace(0, 2*k*np.pi, 201)
plt.plot(x, A* np.sin(x), linewidth = 2)
plt.title('Sin(x) plotting example')
plt.xlabel('Angle [rad]')
plt.ylabel('sin(x)')
plt.axis([0,2 *k*np.pi, -1.1, 1.1])
plt.grid()
plt.show()
button = widgets.ButtonWidget(description="Draw!")
display(button)
def on_button_clicked(b):
draw_sine()
print("Button clicked.")
button.on_click(on_button_clicked)
In [ ]: