##// END OF EJS Templates
Updated basics notebook for pydata2014
Updated basics notebook for pydata2014

File last commit:

r17493:53f19bbe
r17493:53f19bbe
Show More
Widget Basics.ipynb
288 lines | 7.4 KiB | text/plain | TextLexer

Simple widget introduction

What are widgets?

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.
You can also use widgets to synchronize stateful and stateless information between Python and JavaScript.

Using widgets

To use the widget framework, you need to import the widgets from IPython.html.widgets.

In [8]:
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 IntSliderWidget 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.

In [9]:
IntSliderWidget()

You can also explicitly display the widget using display(...).

In [10]:
from IPython.display import display
w = IntSliderWidget()
display(w)

If you display the same widget twice, the displayed instances in the front-end will remain in sync with each other.

In [11]:
display(w)

You can close a widget by calling its close() method.

In [12]:
w.close()

Why does displaying the same widget twice work?

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.

Widget properties

All of the IPython widgets share a similar naming scheme. To read the value of a widget, you can query its value property.

In [31]:
w.value
Out[31]:
41

Similarly, to set a widget's value, you can set its value property.

In [32]:
w.value = 100

In addition to value, most widgets share keys, description, disabled, and visible. To see the entire list of synchronized, stateful properties, of any specific widget, you can query the keys property.

In [33]:
w.keys
Out[33]:
['_view_name',
 'orientation',
 'msg_throttle',
 'min',
 'max',
 '_css',
 'value',
 'readout',
 'disabled',
 'visible',
 'step',
 'description']

Tip: Shorthand for setting the initial values of widget properties

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).

In [34]:
TextWidget(value='Hello World!', disabled=True)

Excercise

Create and display a TextWidget. Change that widget's value and some of it's other properties. Discover the other properties by querying the keys property of the instance.

In [ ]:
 

Linking two similar widgets

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 traitlet link function to link two properties together. Below, the values of three widgets are linked together.

In [51]:
from IPython.utils.traitlets import link
a = FloatTextWidget()
b = FloatSliderWidget()
c = FloatProgressWidget()
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.

In [52]:
mylink.unlink()

[Next](Widget List.ipynb)