##// END OF EJS Templates
Added custom widget tutorial examples
Added custom widget tutorial examples

File last commit:

r17492:fc54bd56
r17494:6c208dd7
Show More
Widget Events.ipynb
210 lines | 6.1 KiB | text/plain | TextLexer
In [12]:
from __future__ import print_function

The ButtonWidget isn't used to represent a data type. Instead the button widget is used to handle mouse clicks. The on_click method of the ButtonWidget can be used to register function to be called when the button is clicked. The docstring of the on_click can be seen below.

In [13]:
from IPython.html import widgets
print(widgets.ButtonWidget.on_click.__doc__)
Register a callback to execute when the button is clicked.

        The callback will be called with one argument,
        the clicked button widget instance.

        Parameters
        ----------
        remove : bool (optional)
            Set to true to remove the callback from the list of callbacks.

Button clicks are transmitted from the front-end to the back-end using custom messages. By using the on_click method, a button that prints a message when it has been clicked is shown below.

In [14]:
from IPython.display import display
button = widgets.ButtonWidget(description="Click Me!")
display(button)

def on_button_clicked(b):
    print("Button clicked.")

button.on_click(on_button_clicked)

The TextWidget also has a special on_submit event. The on_submit event fires when the user hits return.

In [15]:
text = widgets.TextWidget()
display(text)

def handle_submit(sender):
    print(text.value)

text.on_submit(handle_submit)

Traitlet Events

Widget properties are IPython traitlets. Traitlets are eventful. To handle changes, the on_trait_change method of the widget can be used to register a callback. The docstring for on_trait_change can be seen below. Both the name and remove properties are optional.

In [16]:
print(widgets.Widget.on_trait_change.__doc__)
Setup a handler to be called when a trait changes.

        This is used to setup dynamic notifications of trait changes.

        Static handlers can be created by creating methods on a HasTraits
        subclass with the naming convention '_[traitname]_changed'.  Thus,
        to create static handler for the trait 'a', create the method
        _a_changed(self, name, old, new) (fewer arguments can be used, see
        below).

        Parameters
        ----------
        handler : callable
            A callable that is called when a trait changes.  Its
            signature can be handler(), handler(name), handler(name, new)
            or handler(name, old, new).
        name : list, str, None
            If None, the handler will apply to all traits.  If a list
            of str, handler will apply to all names in the list.  If a
            str, the handler will apply just to that name.
        remove : bool
            If False (the default), then install the handler.  If True
            then unintall it.
        

Mentioned in the doc string, the callback registered can have 4 possible signatures:

  • callback()
  • callback(trait_name)
  • callback(trait_name, new_value)
  • callback(trait_name, old_value, new_value)

Using this method, an example of how to output an IntSliderWiget's value as it is changed can be seen below.

In [17]:
int_range = widgets.IntSliderWidget()
display(int_range)

def on_value_change(name, value):
    print(value)

int_range.on_trait_change(on_value_change, 'value')

[Next](Widget Styling.ipynb)