Repository: https://github.com/jupyter-widgets/ipywidgets

Installation:

conda install -c conda-forge ipywidgets

Simple slider for driving an integer value


In [ ]:
from ipywidgets import IntSlider

In [ ]:
slider = IntSlider()

In [ ]:
slider

In [ ]:
slider.value

In [ ]:
slider.value = 20

In [ ]:
slider

Widgets protocol

Drive a boolean value


In [ ]:
from ipywidgets import Checkbox

In [ ]:
checkbox = Checkbox(description='Check me')
checkbox

In [ ]:
checkbox.value

In [ ]:
checkbox.value = False

In [ ]:
from ipywidgets import IntText, IntSlider, link, HBox

In [ ]:
text = IntText()

slider = IntSlider()

link((text, 'value'), (slider, 'value'))
HBox([text, slider])

Observe changes on the widget model


In [ ]:
from ipywidgets import ToggleButton

In [ ]:
button = ToggleButton(description='Click me!', button_style='danger')

def update_style(change):
    button.button_style = 'info' if change['new'] else 'danger'

button.observe(update_style, 'value')
button

Variety of widgets in the core library


In [ ]:
from ipywidgets import ColorPicker, DatePicker, IntProgress, Play, VBox, link

In [ ]:
progress = IntProgress()
play = Play()
link((play, 'value'), (progress, 'value'))

VBox([ColorPicker(value='red'), DatePicker(), progress, play])

Media widgets

Image widget


In [ ]:
from ipywidgets import Image

import PIL.Image
import io
import numpy as np
from skimage.filters import sobel
from skimage.color.adapt_rgb import adapt_rgb, each_channel
from skimage import filters

In [ ]:
image = Image.from_file("src/marie.png")
image

In [ ]:
im_in = PIL.Image.open(io.BytesIO(image.value))
im_array = np.array(im_in)[...,:3]
im_array

In [ ]:
im_array_edges = adapt_rgb(each_channel)(sobel)(im_array)
im_array_edges = ((1-im_array_edges) * 255).astype(np.uint8)
im_out = PIL.Image.fromarray(im_array_edges)
f = io.BytesIO()
im_out.save(f, format='png')
image.value = f.getvalue()

Video widget


In [ ]:
from ipywidgets import Video, Image
from IPython.display import display
import numpy as np
import cv2
import base64

In [ ]:
video = Video.from_file('src/Big.Buck.Bunny.mp4')
video

In [ ]:
cap = cv2.VideoCapture('src/Big.Buck.Bunny.mp4')

frames = []

while(1):
    try:
        _, frame = cap.read()

        fgmask = cv2.Canny(frame, 100, 100)

        mask = fgmask > 100
        frame[mask, :] = 0

        frames.append(frame)
    except Exception:
        break

width = int(cap.get(3))
height = int(cap.get(4))

filename = 'src/output.mp4'

fourcc = cv2.VideoWriter_fourcc(*'avc1')
writer = cv2.VideoWriter(filename, fourcc, 25, (width, height))

for frame in frames:
    writer.write(frame)

cap.release()
writer.release()

with open(filename, 'rb') as f:
    video.value = f.read()

Clean


In [ ]:
from ipywidgets import Widget
Widget.close_all()