def create_settings(box): "Creates a widget Container for settings and info of a particular slider." sl_enable, slider, sl_units = box.children enable = widgets.Checkbox(value=box.visible) link((box, 'visible'), (enable, 'value')) def slider_link(obj, attr): "Function to link one object to an attr of the slider." # pylint: disable=unused-argument def link_fn(name, new_value): "How to update the object's value given min/max on the slider. " if new_value >= slider.max: slider.max = new_value # if any value is greater than the max, the max slides up # however, this is not held true for the minimum, because # during typing the max or value will grow, and we don't want # to permanently anchor the minimum to unfinished typing if attr is "max" and new_value <= slider.value: if slider.max >= slider.min: slider.value = new_value else: pass # bounds nonsensical, probably because we picked up # a small value during user typing. elif attr is "min" and new_value >= slider.value: slider.value = new_value setattr(slider, attr, new_value) slider.step = (slider.max - slider.min)/24.0 obj.on_trait_change(link_fn, "value") link((slider, attr), (obj, "value")) text_html = "<span class='form-control' style='width: auto;'>" setvalue = widgets.FloatText(value=slider.value, description=slider.description) slider_link(setvalue, "value") fromlabel = widgets.HTML(text_html + "from") setmin = widgets.FloatText(value=slider.min) slider_link(setmin, "min") tolabel = widgets.HTML(text_html + "to") setmax = widgets.FloatText(value=slider.max) slider_link(setmax, "max") units = widgets.Latex(value="") units.width = "6ex" units.font_size = "1.165em" link((sl_units, 'value'), (units, 'value')) descr = widgets.HTML(text_html + slider.varkey.descr.get("label", "")) descr.width = "40ex" return widgets.HBox(children=[enable, setvalue, units, descr, fromlabel, setmin, tolabel, setmax])
def __init__(self): self._top = widgets.Tab(visible=False) self._data = None # hdu.data self._png_image = None # ndarray_to_png(self._data) self._header = '' self._image_box = widgets.VBox() self._image = widgets.Image() self._image_title = widgets.Latex() self._image_box.children = [self._image, self._image_title] self._header_box = widgets.VBox() self._header_display = widgets.Textarea(disabled=True) self._header_box.children = [self._header_display] self._top.children = [self._image_box, self._header_box]
def modelcontrolpanel(model, *args, **kwargs): """Easy model control in IPython / Jupyter Like interact(), but with the ability to control sliders and their ranges live. args and kwargs are passed on to interact() """ sliders = model.interact(*args, **kwargs) sliderboxes = [] for sl in sliders.children: cb = widgets.Checkbox(value=True) unit_latex = sl.varkey.unitstr if unit_latex: unit_latex = "$\scriptsize"+unit_latex+"$" units = widgets.Latex(value=unit_latex) units.font_size = "1.16em" box = widgets.HBox(children=[cb, sl, units]) link((box, 'visible'), (cb, 'value')) sliderboxes.append(box) widgets_css = widgets.HTML("""<style> [style="font-size: 1.16em;"] { padding-top: 0.25em; } [style="width: 3ex; font-size: 1.165em;"] { padding-top: 0.2em; } .widget-numeric-text { width: auto; } .widget-numeric-text .widget-label { width: 20ex; } .widget-numeric-text .form-control { background: #fbfbfb; width: 8.5ex; } .widget-slider .widget-label { width: 20ex; } .widget-checkbox .widget-label { width: 15ex; } .form-control { border: none; box-shadow: none; } </style>""") settings = [widgets_css] for sliderbox in sliderboxes: settings.append(create_settings(sliderbox)) # model_latex = "$"+model.latex(show_subs=False)+"$" # model_eq = widgets.Latex(model_latex) tabs = widgets.Tab(children=[widgets.Box(children=sliderboxes, padding="1.25ex"), widgets.Box(children=settings, padding="1.25ex")]) # TODO: fix model equation display # widgets.Box(children=[model_eq], # padding="1.25ex")]) tabs.set_title(0, 'Variable Sliders') tabs.set_title(1, 'Slider Settings') #tabs.set_title(2, 'Model Equations') return tabs
def __init__(self, has_groundtruth, n_iters, render_function=None, displacements_function=None, errors_function=None, costs_function=None, style='minimal', tabs_style='minimal'): # Initialise default options dictionary default_options = {'groups': ['final'], 'mode': 'result', 'render_image': True, 'subplots_enabled': True} # Assign properties self.has_groundtruth = None self.n_iters = None self.displacements_function = displacements_function self.errors_function = errors_function self.costs_function = costs_function # Create children self.mode = ipywidgets.RadioButtons( description='Figure mode:', options={'Single': False, 'Multiple': True}, value=default_options['subplots_enabled']) self.render_image = ipywidgets.Checkbox( description='Render image', value=default_options['render_image']) self.mode_render_image_box = ipywidgets.VBox( children=[self.mode, self.render_image], margin='0.2cm') self.shape_selection = [ ipywidgets.Latex(value='Shape:', margin='0.2cm'), ipywidgets.ToggleButton(description='Initial', value=False), ipywidgets.ToggleButton(description='Final', value=True)] if has_groundtruth: self.shape_selection.append(ipywidgets.ToggleButton( description='Groundtruth', value=False)) self.result_box = ipywidgets.HBox(children=self.shape_selection, align='center', margin='0.2cm', padding='0.2cm') self.iterations_mode = ipywidgets.RadioButtons( options={'Animation': 'animation', 'Static': 'static'}, value='animation', description='Iterations:', margin='0.15cm') index = {'min': 0, 'max': n_iters - 1, 'step': 1, 'index': 0} self.index_animation = AnimationOptionsWidget( index, description='', index_style='slider', loop_enabled=False, interval=0.2) self.index_slider = ipywidgets.IntRangeSlider( min=0, max=n_iters - 1, step=1, value=(0, 0), description='', margin='0.15cm', width='6cm', visible=False, continuous_udate=False) self.plot_errors_button = ipywidgets.Button( description='Errors', margin='0.1cm', visible=has_groundtruth and errors_function is not None) self.plot_displacements_button = ipywidgets.Button( description='Displacements', margin='0.1cm', visible=displacements_function is not None) self.plot_costs_button = ipywidgets.Button( description='Costs', margin='0.1cm', visible=costs_function is not None) self.buttons_box = ipywidgets.HBox( children=[self.plot_errors_button, self.plot_costs_button, self.plot_displacements_button]) self.sliders_buttons_box = ipywidgets.VBox( children=[self.index_animation, self.index_slider, self.buttons_box]) self.iterations_box = ipywidgets.HBox( children=[self.iterations_mode, self.sliders_buttons_box], margin='0.2cm', padding='0.2cm') self.result_iterations_tab = ipywidgets.Tab( children=[self.result_box, self.iterations_box], margin='0.2cm') self.result_iterations_tab.set_title(0, 'Result') self.result_iterations_tab.set_title(1, 'Iterations') # Create final widget children = [self.mode_render_image_box, self.result_iterations_tab] super(FittingResultOptionsWidget, self).__init__( children, Dict, default_options, render_function=render_function, orientation='horizontal', align='start') # Set callbacks if displacements_function is not None: self.plot_displacements_button.on_click(displacements_function) if errors_function is not None: self.plot_errors_button.on_click(errors_function) if costs_function is not None: self.plot_costs_button.on_click(costs_function) # Set values self.set_widget_state(has_groundtruth, n_iters, allow_callback=False) # Set style self.predefined_style(style, tabs_style)