Widgets are elements that exists in both the front-end and the back-end.
You can use widgets to build interactive GUIs for your notebooks.
To use the widget framework, you need to import
from IPython.html.widgets import *
Widgets have their own display
repr which allows them to be displayed using IPython's display framework. Constructing and returning an
IntSlider automatically displays the widget (as seen below). Widgets are displayed inside the
widget area, which sits between the code cell and output. You can hide all of the widgets in the
widget area by clicking the grey x in the margin.
You can also explicitly display the widget using
from IPython.display import display w = IntSlider() display(w)
If you display the same widget twice, the displayed instances in the front-end will remain in sync with each other.
Widgets are represented in the back-end by a single object. Each time a widget is displayed, a new representation of that same object is created in the front-end. These representations are called views.
You can close a widget by calling its
All of the IPython widgets share a similar naming scheme. To read the value of a widget, you can query its
w = IntSlider() display(w)
Similarly, to set a widget's value, you can set its
w.value = 100
In addition to
value, most widgets share
visible. To see the entire list of synchronized, stateful properties, of any specific widget, you can query the
While creating a widget, you can set some or all of the initial values of that widget by defining them as keyword arguments in the widget's constructor (as seen below).
Text(value='Hello World!', disabled=True)
If you need to display the same value two different ways, you'll have to use two different widgets. Instead of attempting to manually synchronize the values of the two widgets, you can use the
link function to link two properties together. Below, the values of three widgets are linked together.
from IPython.utils.traitlets import link a = FloatText() b = FloatSlider() c = FloatProgress() display(a,b,c) mylink = link((a, 'value'), (b, 'value'), (c, 'value'))
Unlinking the widgets is simple. All you have to do is call
.unlink on the link object.