spectacoular.factory.get_widgets¶
- spectacoular.factory.get_widgets(self, trait_widget_mapper={}, trait_widget_args={})¶
Creates 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 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 specifyingtrait_widget_mapper
andtrait_widget_args
explicitly as function arguments. In this case, the default widget mapping, defined inbokehview
, 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 aBaseSpectacoular
derived instance and an explicit mapping is given. In this case, the instance attributes (self.trait_widget_mapper,self.`trait_widget_args`) will be 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, 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.