For a complete list of the widgets available to you, you can list the classes in the widget namespace (as seen below). Widget
and DOMWidget
, not listed below, are base classes.
import ipywidgets as widgets
[n for n in dir(widgets) if not n.endswith('Widget') and n[0] == n[0].upper() and not n[0] == '_']
There are 8 widgets distributed with IPython that are designed to display numeric values. Widgets exist for displaying integers and floats, both bounded and unbounded. The integer widgets share a similar naming scheme to their floating point counterparts. By replacing Float
with Int
in the widget name, you can find the Integer equivalent.
widgets.FloatSlider(
value=7.5,
min=5.0,
max=10.0,
step=0.1,
description='Test:',
)
Sliders can also be displayed vertically.
widgets.FloatSlider(
value=7.5,
min=5.0,
max=10.0,
step=0.1,
description='Test',
orientation='vertical',
)
widgets.FloatProgress(
value=7.5,
min=5.0,
max=10.0,
step=0.1,
description='Loading:',
)
widgets.BoundedFloatText(
value=7.5,
min=5.0,
max=10.0,
description='Text:',
)
widgets.FloatText(
value=7.5,
description='Any:',
)
There are two widgets that are designed to display a boolean value.
widgets.ToggleButton(
description='Click me',
value=False,
)
widgets.Checkbox(
description='Check me',
value=True,
)
There are four widgets that can be used to display single selection lists, and one that can be used to display multiple selection lists. All inherit from the same base class. You can specify the enumeration of selectable options by passing a list. You can also specify the enumeration as a dictionary, in which case the keys will be used as the item displayed in the list and the corresponding value will be returned when an item is selected.
from IPython.display import display
w = widgets.Dropdown(
options=['1', '2', '3'],
value='2',
description='Number:',
)
display(w)
w.value
The following is also valid:
w = widgets.Dropdown(
options={'One': 1, 'Two': 2, 'Three': 3},
value=2,
description='Number:',
)
display(w)
w.value
widgets.RadioButtons(
description='Pizza topping:',
options=['pepperoni', 'pineapple', 'anchovies'],
)
widgets.Select(
description='OS:',
options=['Linux', 'Windows', 'OSX'],
)
widgets.ToggleButtons(
description='Speed:',
options=['Slow', 'Regular', 'Fast'],
)
Multiple values can be selected with shift and ctrl pressed and mouse clicks or arrow keys.
w = widgets.SelectMultiple(
description="Fruits",
options=['Apples', 'Oranges', 'Pears']
)
display(w)
w.value
There are 4 widgets that can be used to display a string value. Of those, the Text
and Textarea
widgets accept input. The Latex
and HTML
widgets display the string as either Latex or HTML respectively, but do not accept input.
widgets.Text(
description='String:',
value='Hello World',
)
widgets.Textarea(
description='String:',
value='Hello World',
)
widgets.Latex(
value="$$\\frac{n!}{k!(n-k)!} = \\binom{n}{k}$$",
)
widgets.HTML(
value="Hello <b>World</b>"
)
widgets.Button(description='Click me')