factory#

Implements factory for trait to widget translation.

get_widgets(self[, trait_widget_mapper, ...])

Create a mapping between several class trait attributes and Bokeh widgets.

set_widgets(self, **kwargs)

Set instances of Bokeh widgets to certain trait attributes.

BaseSpectacoular

Base class for any class defined in the SpectAcoular module.

NumericInputMapper(obj, traitname)

Create a NumericInput widget from an Int trait attribute.

ToggleMapper(obj, traitname)

Create a Toggle widget from a Bool trait attribute.

TextInputMapper(obj, traitname)

Create a TextInput widget from a class trait attribute.

SelectMapper(obj, traitname)

Create a Select widget from a class trait attribute.

SliderMapper(obj, traitname)

Create and synchronize a Bokeh Slider widget.

DataTableMapper(obj, traitname)

Create and synchronize a Bokeh DataTable widget.

spectacoular.factory.as_str_list(func)#

Wrap list entries into string type.

spectacoular.factory.validate_mapping_is_allowed(obj, traitname, widget_type)#

Validate that a given trait can be mapped to a given Bokeh widget type.

spectacoular.factory.widget_mapper_factory(obj, traitname, widget_type)#

Return a TraitWidgetMapper instance for the desired widget type.

Parameters:
objclass object

the class object that the trait attribute belongs to.

traitnamestr

name of the class trait attribute to be mapped.

widgetTypecls

type of the widget.

Returns:
None.
Raises:
NotImplementedError

raises error when widget type is not supported.

spectacoular.factory.get_widgets(self, trait_widget_mapper=None, trait_widget_args=None)#

Create a mapping between several class trait attributes and Bokeh widgets.

This function is implemented in all SpectAcoular classes and is added to Acoular’s classes via the bokehview module. For each attribute provided, it builds a corresponding Bokeh widget.

The function handles multiple cases of View construction:

  • Default View: the function is called as a method by a BaseSpectacoular derived instance without specifying trait_widget_mapper and trait_widget_args explicitly as function arguments. In this case, the default widget mapping, defined in bokehview, will be used:

    from spectacoular import RectGrid
    from bokeh.io import show
    from bokeh.layouts import gridplot
    
    grid = RectGrid()
    
    widgets = list(grid.get_widgets().values())
    show(gridplot(widgets, ncols=5, sizing_mode='stretch_both'))
    
  • No Predefined View: get_widgets() is called and a HasTraits derived instance is given as the first argument to the function without any further arguments. In this case, a default mapping is created from all editable traits to create the view.

from acoular import RectGrid
from spectacoular import get_widgets
from bokeh.io import show
from bokeh.layouts import gridplot

grid = RectGrid()

widgets = list(get_widgets(grid).values())
show(gridplot(widgets, ncols=5, sizing_mode='stretch_both'))
  • Custom View: get_widgets() is called by a BaseSpectacoular derived instance and an explicit mapping is given. In this case, the instance attributes (self.trait_widget_mapper, self.trait_widget_args) are superseded.

    from spectacoular import RectGrid
    from bokeh.io import show
    from bokeh.models.widgets import Slider
    from bokeh.layouts import column
    
    grid = RectGrid()
    
    trait_widget_mapper = {'x_min': Slider}
    trait_widget_args = {'x_min': {'title': 'X Min', 'start': -1, 'end': 1, 'step':0.1}}
    
    widgets = list(grid.get_widgets(
          trait_widget_mapper=trait_widget_mapper,
          trait_widget_args=trait_widget_args
    ).values())
    show(column(widgets,sizing_mode='stretch_both'))
    

    The same functionality can also be used with HasTraits-derived classes that are not part of SpectAcoular:

    from acoular import RectGrid
    from spectacoular import get_widgets
    from bokeh.io import show
    from bokeh.models.widgets import Slider
    from bokeh.layouts import column
    
    grid = RectGrid()
    
    trait_widget_mapper = {'x_min': Slider}
    trait_widget_args = {'x_min': {'title': 'X Min', 'start': -1, 'end': 1, 'step':0.1}}
    
    widgets = list(get_widgets(grid,
          trait_widget_mapper=trait_widget_mapper,
          trait_widget_args=trait_widget_args
    ).values())
    show(column(widgets,sizing_mode='stretch_both'))
    
Parameters:
trait_widget_mapperdict, optional

contains the desired mapping of a variable name (dict key) to a Bokeh widget type (dict value), by default {}

trait_widget_argsdict, optional
contains the desired widget kwargs (dict values) for each variable name (dict key),

by default {}

Returns:
dict

A dictionary containing the variable names as the key and the Bokeh widget instance as value.

spectacoular.factory.set_widgets(self, **kwargs)#

Set instances of Bokeh widgets to certain trait attributes.

This function is implemented in all SpectAcoular classes and is added to Acoular’s classes in bokehview.py. It allows to reference an existing widget to a certain class trait attribute. Expects a class traits name as parameter and the widget instance as value.

For example:
>>> from spectacoular import RectGrid
>>> from bokeh.models.widgets import Select
>>>
>>> rg = RectGrid()
>>> sl = Select(value='10.0')
>>> rg.set_widgets(x_max=sl)

The value of the trait attribute changes to the widgets value when it is different.

Parameters:
**kwargs

The name of the class trait attributes. Depends on the class.

Returns:
None.
class spectacoular.factory.BaseSpectacoular#

Bases: HasPrivateTraits

Base class for any class defined in the SpectAcoular module.

It provides the necessary methods to create widgets from trait attributes and to assign Bokeh’s widgets to trait attributes. This class has no real functionality on its own and should not be used directly.

trait_widget_mapper#

dictionary containing the mapping between a class trait attribute and a Bokeh widget. Keys: name of the trait attribute. Values: Bokeh widget.

trait_widget_args#

dictionary containing arguments that belongs to a widget that is created from a trait attribute and should be considered when the widget is built. For example: {“traitname”:{‘disabled’:True,’background_color’:’red’,…}}.

get_widgets(trait_widget_mapper=None, trait_widget_args=None)#

function to create widgets from class trait attributes

set_widgets(**kwargs)#

function to assign widget instances to class trait attributes

class spectacoular.factory.TraitWidgetMapper(obj, traitname)#

Bases: object

Map a trait attribute to a corresponding Bokeh widget.

Derived classes of TraitWidgetMapper create widget instances and keep the widget value synchronized with the mapped trait attribute.

Attributes:
objclass object

The class object that the trait attribute belongs to.

traitnamestr

Name of the class trait attribute to be mapped.

traittypeTraitType

Type of the class trait attribute.

traitvalueany

Value of the class trait attribute.

traitvaluetypetype

Type of the value of the class trait attribute.

traitdescriptionstr

Description of the class trait attribute.

widgetWidget

Instance of the widget.

create_trait_setter_func()#

Create a callback that writes widget values back to the trait.

The callback is invoked every time the widget value changes. If the widget value type differs from the trait value type, the value is cast before it is assigned.

Returns:
callable

Callback that updates the trait from the widget.

create_widget_setter_func(widgetproperty='value')#

Create a callback that writes trait values back to the widget.

Parameters:
widgetpropertystr, optional

Name of the widget property that should be updated.

Returns:
callable

Callback that updates the widget from the trait.

class spectacoular.factory.NumericInputMapper(obj, traitname)#

Bases: TraitWidgetMapper

Create a NumericInput widget from an Int trait attribute.

create_widget(**kwargs)#

Create a Bokeh NumericInput instance.

Parameters:
**kwargsargs of NumericInput

additional arguments of NumericInput widget.

Returns:
instance(NumericInput).
set_widget(widget)#

Connect a Bokeh NumericInput widget instance to a class trait attribute.

Parameters:
widgetinstance(NumericInput)

instance of a NumericInput widget.

Returns:
None.
create_trait_setter_func()#

Create a function that sets the value of a trait on the widget value.

The function is invoked every time the widget value changes. No casting is necessary for NumericInput widgets.

Returns:
callable.
create_widget_setter_func(widgetproperty='value')#

Create a callback that writes trait values back to the widget.

Parameters:
widgetpropertystr, optional

Name of the widget property that should be updated.

Returns:
callable

Callback that updates the widget from the trait.

class spectacoular.factory.ToggleMapper(obj, traitname)#

Bases: NumericInputMapper

Create a Toggle widget from a Bool trait attribute.

create_widget(**kwargs)#

Create a Bokeh Toggle instance.

Parameters:
**kwargsargs of Toggle

additional arguments of Toggle widget.

Returns:
instance(Toggle).
set_widget(widget)#

Connect a Bokeh Toggle widget instance to a class trait attribute.

Parameters:
widgetinstance(Toggle)

instance of a Toggle widget.

Returns:
None.
create_trait_setter_func()#

Create a function that sets the value of a trait on the widget value.

The function is invoked every time the widget value changes. No casting is necessary for NumericInput widgets.

Returns:
callable.
create_widget_setter_func(widgetproperty='value')#

Create a callback that writes trait values back to the widget.

Parameters:
widgetpropertystr, optional

Name of the widget property that should be updated.

Returns:
callable

Callback that updates the widget from the trait.

class spectacoular.factory.TextInputMapper(obj, traitname)#

Bases: TraitWidgetMapper

Create a TextInput widget from a class trait attribute.

create_widget(**kwargs)#

Create a Bokeh TextInput instance.

Parameters:
**kwargsargs of TextInput

additional arguments of TextInput widget.

Returns:
instance(TextInput).
set_widget(widget)#

Connect a Bokeh TextInput widget instance to a class trait attribute.

Parameters:
widgetinstance(TextInput)

instance of a TextInput widget.

Returns:
None.
create_trait_setter_func()#

Create a callback that writes widget values back to the trait.

The callback is invoked every time the widget value changes. If the widget value type differs from the trait value type, the value is cast before it is assigned.

Returns:
callable

Callback that updates the trait from the widget.

create_widget_setter_func(widgetproperty='value')#

Create a callback that writes trait values back to the widget.

Parameters:
widgetpropertystr, optional

Name of the widget property that should be updated.

Returns:
callable

Callback that updates the widget from the trait.

class spectacoular.factory.SelectMapper(obj, traitname)#

Bases: TraitWidgetMapper

Create a Select widget from a class trait attribute.

create_widget(**kwargs)#

Create a Bokeh Select widget instance.

Parameters:
**kwargsargs of Select

additional arguments of Select widget.

Returns:
instance(Select).
set_widget(widget)#

Connect a Bokeh Select widget instance to a class trait attribute.

Parameters:
widgetinstance(Select)

instance of a Select widget.

Returns:
None.
create_trait_setter_func()#

Create a callback that casts Select values back to the trait type.

Select widget values are always strings. This callback converts the selected value back to the mapped trait type before assignment.

Returns:
callable

Callback that updates the trait from the widget.

create_widget_setter_func(widgetproperty='value')#

Create a callback that converts trait values for the widget.

Returns:
callable

Callback that updates the widget from the trait.

class spectacoular.factory.SliderMapper(obj, traitname)#

Bases: TraitWidgetMapper

Create and synchronize a Bokeh Slider widget.

create_widget(**kwargs)#

Create a Bokeh Slider widget for the mapped trait.

The created widget is initialized from the current trait value and then connected to the trait via callbacks. At the moment this mapper is mainly intended for Range traits.

Parameters:
**kwargsargs of Slider

Additional keyword arguments for the Slider widget. The value, start, and end arguments are float-valued.

Returns:
instance(Slider)

Created widget instance.

set_widget(widget)#

Connect a Bokeh Slider widget instance to a class trait attribute.

Parameters:
widgetinstance(Slider)

instance of a Slider widget.

Returns:
None.
create_trait_setter_func()#

Create a callback that casts slider values back to the trait type.

Slider widgets expose numeric values that may still need conversion to the exact mapped trait type before assignment.

Returns:
callable

Callback that updates the trait from the widget.

create_widget_setter_func(widgetproperty='value')#

Create a callback that writes trait values back to the widget.

Parameters:
widgetpropertystr, optional

Name of the widget property that should be updated.

Returns:
callable

Callback that updates the widget from the trait.

class spectacoular.factory.DataTableMapper(obj, traitname)#

Bases: TraitWidgetMapper

Create and synchronize a Bokeh DataTable widget.

create_widget(**kwargs)#

Create a Bokeh DataTable widget for the mapped trait.

For transposed arrays mapped to the DataTable, array data is reshaped and converted to the dictionary format required by ColumnDataSource. For list types, only one-dimensional lists are allowed and the transposed option has no effect.

Parameters:
**kwargsargs of DataTable

Additional keyword arguments for the DataTable widget.

Returns:
instance(DataTable)

Created widget instance.

set_widget(widget)#

Connect an existing DataTable widget to the mapped trait.

The current implementation only handles non-transposed mappings, i.e. columns of the mapped array trait must correspond to columns in the ColumnDataSource rather than rows.

Parameters:
widgetinstance(DataTable)

Instance of a DataTable widget.

Returns:
None.
create_columns()#

Create a single TableColumn and add it to the widget.

create_trait_setter_func()#

Create a callback that maps edited table data back to the trait.

Depending on the mapped trait type, the callback converts edited table values to a list, tuple, or NumPy array before assignment.

Returns:
callable

Callback that updates the trait from edited table data.

create_widget_setter_func(widgetproperty='value')#

Create a callback that writes trait values back to the widget.

Parameters:
widgetpropertystr, optional

Name of the widget property that should be updated.

Returns:
callable

Callback that updates the widget from the trait.

class spectacoular.factory.MultiSelectMapper(obj, traitname)#

Bases: TraitWidgetMapper

Create and synchronize a Bokeh MultiSelect widget.

create_widget(**kwargs)#

Create a Bokeh MultiSelect instance.

Parameters:
**kwargsargs of MultiSelect

additional arguments of MultiSelect widget.

Returns:
instance(MultiSelect).
set_widget(widget)#

Connect a Bokeh MultiSelect widget instance to a class trait attribute.

Parameters:
widgetinstance(MultiSelect)

instance of a MultiSelect widget.

Returns:
None.
create_trait_setter_func()#

Create a callback that writes MultiSelect values to the trait.

The callback is invoked every time the widget value changes. No casting is needed at this stage because conversion happens in _set_traitvalue().

Returns:
callable

Callback that updates the trait from the widget.

create_widget_setter_func(widgetproperty='value')#

Create a callback that writes trait values back to the widget.

Parameters:
widgetpropertystr, optional

Name of the widget property that should be updated.

Returns:
callable

Callback that updates the widget from the trait.