The notebook comes alive with the interactive widgets
9*9
def f(x):
print(x * x)
f(9)
from ipywidgets import *
interact(f, x=(0, 100));
A Python widget is an object that represents a control on the front end, like a slider. A single control can be displayed multiple times - they all represent the same python object.
slider = FloatSlider(
value=7.5,
min=5.0,
max=10.0,
step=0.1,
description='Input:',
)
slider
slider
The control attributes, like its value, are automatically synced between the frontend and the kernel.
slider.value
slider.value = 8
You can trigger actions in the kernel when a control value changes by "observing" the value. Here we set a global variable when the slider value changes.
square = slider.value * slider.value
def handle_change(change):
global square
square = change.new * change.new
slider.observe(handle_change, 'value')
square
You can link control attributes and lay them out together.
text = FloatText(description='Value')
link((slider, 'value'), (text, 'value'))
VBox([slider, text])
Jupyter widgets forms a framework for representing python objects interactively. Some large open-source interactive controls based on Jupyter widgets include: