def __init__(self, plots):
"""
Compiles non-axis coordinates into sliders, the values from which are
used to reconstruct plots upon movement of slider.
Args:
* plot
cube_browser plot instance to display with slider.
"""
if not isinstance(plots, Iterable):
plots = [plots]
self.plots = plots
# Mapping of coordinate/alias name to axis.
self._axis_by_name = {}
# Mapping of cube-id to shared cache.
self._cache_by_cube_id = {}
# Mapping of plot-id to coordinate/alias name.
self._names_by_plot_id = {}
# Mapping of coordinate/alias name to plots.
self._plots_by_name = {}
self._build_mappings()
self._slider_by_name = {}
self._name_by_slider_id = {}
if self._axis_by_name:
name_len = max([len(name) for name in self._axis_by_name])
children = []
for axis in self._axis_by_name.values():
if hasattr(axis, 'coord') and axis.coord.units.is_time_reference():
pairs = [(axis.coord.units.num2date(axis.coord.points[i]), i)
for i in range(axis.size)]
elif hasattr(axis, 'coord'):
pairs = [(axis.coord.points[i], i) for i in range(axis.size)]
else:
pairs = [(i, i) for i in range(axis.size)]
options = OrderedDict(pairs)
slider = ipywidgets.SelectionSlider(options=options)
slider.observe(self.on_change, names='value')
self._slider_by_name[axis.name] = slider
self._name_by_slider_id[id(slider)] = axis.name
# Explicitly control the slider label in order to avoid
# fix width widget description label wrapping issues.
# XXX: Adjust px/em scale-factor dynamically based on font-size.
scale_factor = .65
width = u'{}em'.format(int(name_len * scale_factor))
label = ipywidgets.Label(axis.name, padding=u'0.3em', width=width)
hbox = ipywidgets.HBox(children=[label, slider])
children.append(hbox)
# Layout the sliders in a consitent order.
self.form = ipywidgets.VBox()
key = lambda hbox: hbox.children[0].value
self.form.children = sorted(children, key=key)
评论列表
文章目录