MenpoWidget

class menpowidgets.abstract.MenpoWidget(children, trait, trait_initial_value, render_function=None, orientation='vertical', align='start')[source]

Bases: FlexBox

Base class for defining a Menpo widget.

The widget has a selected_values trait that can be used in order to inspect any changes that occur to its children. It also has functionality for adding, removing, replacing or calling the handler callback function of the selected_values trait.

Parameters:
  • children (list of ipywidgets) – The list of ipywidgets objects to be set as children in the ipywidgets.FlexBox.
  • trait (traitlets.TraitType subclass) – The type of the selected_values object that gets added as a trait in the widget. Possible options from traitlets are {Int, Float, Dict, List, Tuple}.
  • trait_initial_value (int or float or dict or list or tuple) – The initial value of the selected_values trait.
  • render_function (callable or None, optional) –

    The render function that behaves as a callback handler of the selected_values trait for the change event. Its signature can be render_function() or render_function(change), where change is a dict with the following keys:

    • owner : the HasTraits instance
    • old : the old value of the modified trait attribute
    • new : the new value of the modified trait attribute
    • name : the name of the modified trait attribute.
    • type : 'change'

    If None, then nothing is added.

  • orientation ({'horizontal', 'vertical'}, optional) – The orientation of the ipywidgets.FlexBox.
  • align ({'start', 'center', 'end'}, optional) – The alignment of the children of the ipywidgets.FlexBox.
add_render_function(render_function)[source]

Method that adds the provided render_function() as a callback handler to the selected_values trait of the widget. The given function is also stored in self._render_function.

Parameters:render_function (callable or None, optional) –

The render function that behaves as a callback handler of the selected_values trait for the change event. Its signature can be render_function() or render_function(change), where change is a dict with the following keys:

  • owner : the HasTraits instance
  • old : the old value of the modified trait attribute
  • new : the new value of the modified trait attribute
  • name : the name of the modified trait attribute.
  • type : 'change'

If None, then nothing is added.

add_traits(**traits)

Dynamically add trait attributes to the Widget.

call_render_function(old_value, new_value, type_value='change')[source]

Method that calls the existing render_function() callback handler.

Parameters:
  • old_value (int or float or dict or list or tuple) – The old selected_values value.
  • new_value (int or float or dict or list or tuple) – The new selected_values value.
  • type_value (str, optional) – The trait event type.
close()

Close method.

Closes the underlying comm. When the comm is closed, all of the widget views are automatically removed from the front-end.

has_trait(name)

Returns True if the object has a trait with the specified name.

observe(handler, names=traitlets.All, type='change')

Setup a handler to be called when a trait changes.

This is used to setup dynamic notifications of trait changes.

Parameters:
  • handler (callable) – A callable that is called when a trait changes. Its signature should be handler(change), where change```is a dictionary. The change dictionary at least holds a 'type' key. * ``type: the type of notification. Other keys may be passed depending on the value of ‘type’. In the case where type is ‘change’, we also have the following keys: * owner : the HasTraits instance * old : the old value of the modified trait attribute * new : the new value of the modified trait attribute * name : the name of the modified trait attribute.
  • names (list, str, All) – If names is All, 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.
  • type (str, All (default: 'change')) – The type of notification to filter by. If equal to All, then all notifications are passed to the observe handler.
remove_render_function()[source]

Method that removes the current self._render_function() as a callback handler to the selected_values trait of the widget and sets self._render_function = None.

replace_render_function(render_function)[source]

Method that replaces the current self._render_function() with the given render_function() as a callback handler to the selected_values trait of the widget.

Parameters:render_function (callable or None, optional) –

The render function that behaves as a callback handler of the selected_values trait for the change event. Its signature can be render_function() or render_function(change), where change is a dict with the following keys:

  • owner : the HasTraits instance
  • old : the old value of the modified trait attribute
  • new : the new value of the modified trait attribute
  • name : the name of the modified trait attribute.
  • type : 'change'

If None, then nothing is added.

trait_names(**metadata)

Get a list of all the names of this class’ traits.

traits(**metadata)

Get a dict of all the traits of this class. The dictionary is keyed on the name and the values are the TraitType objects.

The TraitTypes returned don’t know anything about the values that the various HasTrait’s instances are holding.

The metadata kwargs allow functions to be passed in which filter traits based on metadata values. The functions should take a single value as an argument and return a boolean. If any function returns False, then the trait is not included in the output. If a metadata key doesn’t exist, None will be passed to the function.

unobserve(handler, names=traitlets.All, type='change')

Remove a trait change handler.

This is used to unregister handlers to trait change notificiations.

Parameters:
  • handler (callable) – The callable called when a trait attribute changes.
  • names (list, str, All (default: All)) – The names of the traits for which the specified handler should be uninstalled. If names is All, the specified handler is uninstalled from the list of notifiers corresponding to all changes.
  • type (str or All (default: 'change')) – The type of notification to filter by. If All, the specified handler is uninstalled from the list of notifiers corresponding to all types.
unobserve_all(name=traitlets.All)

Remove trait change handlers of any type for the specified name. If name is not specified, removes all trait notifiers.