Skip to content

Commit

Permalink
Fix for reinit_live
Browse files Browse the repository at this point in the history
This is a parallel fix to commit 7862b7f. It fixes an issue with live
updates for reinitialization that caused multiple callbacks to be
scheduled when the reset button is used.
  • Loading branch information
teald committed Oct 11, 2023
1 parent 225ce21 commit d9b4d69
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
1 change: 0 additions & 1 deletion geminidr/interactive/fit/aperture.py
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,6 @@ def function():

widgets = self.make_widgets_from_parameters(
self.ui_params,
reinit_live=False,
slider_width=256,
add_spacer=True,
hide_textbox=["max_separation"],
Expand Down
5 changes: 2 additions & 3 deletions geminidr/interactive/fit/fit1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,7 @@ def __init__(
ui_params=None,
turbo_tabs=False,
panel_class=Fit1DPanel,

**kwargs,
):
"""Initializes the Fit1DVisualizer and its parent class.
Expand Down Expand Up @@ -1642,9 +1643,7 @@ def __init__(
# Make the panel with widgets to control the creation of (x, y) arrays

# Create left panel
reinit_widgets = self.make_widgets_from_parameters(
ui_params, reinit_live=modal_message is None
)
reinit_widgets = self.make_widgets_from_parameters(ui_params)

if reinit_widgets:
# This should really go in the parent class, like submit_button
Expand Down
58 changes: 36 additions & 22 deletions geminidr/interactive/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def __init__(
template=None,
help_text=None,
ui_params=None,
reinit_live=False,
):
"""
Initialize a visualizer.
Expand All @@ -114,14 +115,22 @@ def __init__(
----------
title : str
Title fo the primitive for display, currently not used
primitive_name : str
Name of the primitive function related to this UI, used in the
title bar
filename_info : str
Information about the file being operated on
template : str
Optional path to an html template to render against, if
customization is desired
reinit_live : bool
If True, recalculate points on any change (default False). This is
set in __init__ to PrimitiveVisualizer.reinit_live, but can be
overridden by subclasses.
"""
global _visualizer
_visualizer = self
Expand All @@ -133,6 +142,9 @@ def __init__(
# Placeholders for attrs that will be set by subclasses.
self.tabs = None

# Live Reinitialization attr
self.reinit_live = reinit_live

# set help to default, subclasses should override this with something
# specific to them
self.help_text = help_text if help_text else DEFAULT_HELP
Expand Down Expand Up @@ -287,10 +299,20 @@ def build_reset_button(self, extra_handler_fn=None):
)

def reset_dialog_handler(result):
if result:
self.reset_reinit_panel()
if extra_handler_fn:
extra_handler_fn()
# Turning off reinitialization so the calculation does not occur
# many times. The handler restores the value of the flag after
# recalculation has finished.
reinit_live = self.reinit_live
self.reinit_live = False

try:
if result:
self.reset_reinit_panel()
if extra_handler_fn:
extra_handler_fn()

finally:
self.reinit_live = reinit_live

self.make_ok_cancel_dialog(
btn=reset_reinit_button,
Expand Down Expand Up @@ -740,7 +762,6 @@ def show_user_message(self, message):
def make_widgets_from_parameters(
self,
params,
reinit_live: bool = True,
slider_width: int = 256,
add_spacer=False,
hide_textbox=None,
Expand All @@ -754,14 +775,13 @@ def make_widgets_from_parameters(
----------
params : :class:`UIParameters`
Parameters to make widgets for
reinit_live : bool
True if recalcuating points is cheap, in which case we don't need a
button and do it on any change. Currently only viable for
text-slider style inputs.
slider_width : int
Width of the sliders
add_spacer : bool
If True, add a spacer between sliders and their text-boxes
hide_textbox : list
If set, a list of range field names for which we don't want a
textbox
Expand All @@ -782,9 +802,7 @@ def make_widgets_from_parameters(
is_float = field.dtype is not int
step = 0.1 if is_float else 1

slider_handler = self.slider_handler_factory(
key, reinit_live=reinit_live
)
slider_handler = self.slider_handler_factory(key)

widget = build_text_slider(
params.titles[key],
Expand Down Expand Up @@ -823,7 +841,7 @@ def make_widgets_from_parameters(

def _select_handler(attr, old, new):
self.extras[key] = new
if reinit_live:
if self.reinit_live:
self.reconstruct_points()

widget.on_change("value", _select_handler)
Expand Down Expand Up @@ -852,7 +870,7 @@ def _select_handler(attr, old, new):

def _cb_handler(attr, old, new):
self.extras[key] = True if len(new) else False
if reinit_live:
if self.reinit_live:
self.reconstruct_points()

widget.on_change("active", _cb_handler)
Expand Down Expand Up @@ -893,16 +911,14 @@ def _cb_handler(attr, old, new):

return widgets

def slider_handler_factory(self, key, reinit_live=False):
def slider_handler_factory(self, key):
"""
Returns a function that updates the `extras` attribute.
Parameters
----------
key : str
The parameter name to be updated.
reinit_live : bool, optional
Update the reconstructed points on "real time".
Returns
-------
Expand All @@ -911,21 +927,19 @@ def slider_handler_factory(self, key, reinit_live=False):

def handler(val):
self.extras[key] = val
if reinit_live:
if self.reinit_live:
self.reconstruct_points()

return handler

def select_handler_factory(self, key, reinit_live=False):
def select_handler_factory(self, key):
"""
Returns a function that updates the `extras` attribute.
Parameters
----------
key : str
The parameter name to be updated.
reinit_live : bool, optional
Update the reconstructed points on "real time".
Returns
-------
Expand All @@ -934,7 +948,7 @@ def select_handler_factory(self, key, reinit_live=False):

def handler(val):
self.extras[key] = val
if reinit_live:
if self.reinit_live:
self.reconstruct_points()

return handler
Expand Down

0 comments on commit d9b4d69

Please sign in to comment.