In [1]:
from IPython import display
from ipywidgets import widgets
In [6]:
#slider
slider = widgets.IntSlider(min=0, max=10, value=3)
display.display(slider)
In [8]:
#slider callback
slider = widgets.IntSlider()
display.display(slider)

def on_change(change):
    display.clear_output()
    print(change.new)

slider.observe(on_change, 'value')
28
In [10]:
#programmatical change
slider = widgets.IntSlider()
display.display(slider)

slider.value = 90
In [15]:
#buttons
button = widgets.Button(description="Click Me!")
button.width = "100px"
button.height = "40px"
button.background_color = 'red'
button.color = "red"
display.display(button)
In [21]:
#range slider
slider = widgets.IntRangeSlider(min=0, max=60)
display.display(slider)
slider.value = (10,40)

def on_change(change):
    display.clear_output()
    print(change.new)
    
slider.observe(on_change, 'value')
(14, 41)
In [23]:
#inputs
intInput = widgets.IntText()
display.display(intInput)

floatInput = widgets.BoundedFloatText(min=-15,max=300)
display.display(floatInput)

def callback(change):
    display.clear_output()
    print(change['new'])

intInput.observe(callback, 'value')
floatInput.observe(callback, 'value')
0.0
In [24]:
#text input
text = widgets.Text()
display.display(text)

def on_text_submit(textWidget):
    print("text confirmed: " + textWidget.value)

def on_text_change(change):
    print("text edited in: " + change['new'])

text.on_submit(on_text_submit)
text.observe(on_text_change, 'value')
text edited in: c
text edited in: ci
text edited in: cia
text edited in: ciao
text confirmed: ciao
In [28]:
#checkbox
ck = widgets.Checkbox(value = True)
display.display(ck)

def on_change(change):
    display.clear_output()
    print(change.new)
    
ck.observe(on_change, 'value')
True
In [29]:
#click button
button = widgets.Button(description="Click Me!")
button.width = "100px"
button.background_color = "#000000"
button.color = "#ffffff"
display.display(button)

def on_button_clicked(b):
    print("Clicked button")
    
button.on_click(on_button_clicked)
Clicked button
Clicked button
Clicked button
Clicked button
In [30]:
#selection
sel = widgets.RadioButtons(options = ["pizza","pasta","coffee"])
display.display(sel)

sel = widgets.Dropdown(options = ["pizza","pasta","coffee"])
display.display(sel)
In [32]:
#multiple select
sel = widgets.SelectMultiple(options = ["pizza","pasta","coffee"], width="150px")
display.display(sel)

def on_change(change):
    display.clear_output()
    print(change.new)
    
sel.observe(on_change, 'value')
()
In [33]:
#containers
bt1 = widgets.Button(description = "Button 1")
bt2 = widgets.Button(description = "Button 2")
bt3 = widgets.Button(description = "Button 3")
ta = widgets.Textarea(width = "300px", margin = "5px")
slider = widgets.IntSlider(margin = "5px")


cont1 = widgets.HBox([bt1, bt2, bt3])
cont2 = widgets.VBox([cont1, slider])
cont3 = widgets.HBox([ta, cont2])

display.display(cont3)
In [35]:
#exercise
import matplotlib.pyplot as plt

N = 5 # number of leds

def draw_led(selLed):
    for i in range(0,N):
        if i == selLed:
            color = 'r'
        else:
            color = 'w'    
        display.clear_output(wait=True)
        plt.scatter([i],[0], s=800, c=color)
    plt.axis([-1,N,-1,1])
    plt.show()

def on_change(change):
    draw_led(int(change['new']))

opts = {}
for i in range(N):
    opts["led " + str(i)] = i

tb = widgets.ToggleButtons(options = opts)
display.display(tb)
tb.observe(on_change, 'value')
draw_led(0)
In [ ]: