Context :
I have multiple widgets of type Select (Dropdown menu with one possible selection).
Each widget contains several options, retrieved from a dataframe where each column corresponds to a Select widget.
Not all possibles combinaisons are presents in the dataframe. Hence, when a selection is made in one widget, I want the other widgets to be updated so that all the selected values are a combinaison of value present in the dataframe.
Code details
I have a watcher function on all the widget which updates the options of the widget every time a selection is made in one of the widget so that the options match a possible combinaison of values in the dataframe.
@param.depends("countries", "suppliers", "classes", "categories", "brands", watch=True)
def update_fields(self):
"""
Update the different filters possible fields on selection
"""
self.loading = True
filters = self.get_filters()
set_new_filters = set(filters.items())
set_current_filters = set(self.current_filters.items())
dif_filters = dict(set_new_filters ^ set_current_filters)
if len(dif_filters) > 0:
for key, input in dif_filters.items():
df = self.distinct_fields[self.distinct_fields[key] == filters[key]]
else:
df = self.distinct_fields
self.build_distinct_fields(df)
self.current_filters = filters
self.loading = False
return True
def build_distinct_fields(self, df, first=False):
"""
Initialize the possible fields for each filter
:param df: pandas dataframe
dataframe containing the possible fields for each filter
"""
self.param.countries.objects = list(df[CRF_COUNTRY].unique())
self.param.suppliers.objects = list(df[CRF_SUPPLIER].unique())
self.param.classes.objects = list(df[CRF_CLASS].unique())
self.param.categories.objects = list(df[CRF_CATEGORY].unique())
self.param.brands.objects = list(df[CRF_BRAND].unique())
Problem
The build_distinct_fields
function doesn’t update the value of the selector.
More precisely, If the initial values of the selectors are A, B, C.
Then if we changed B->D then the update_fields
function could update the selections in A->E, so that we have E, D, C to match a possible combinaison of values on the dataframe.
But when I try to retrieve the value of the selections, I get A, D, C.
Is it possible to update the value of the selectors without hard scpecifying its value with something like : selector = value
?
Because when doing this, the watcher is called infinitely.