Panel in Python - How to set the order that events are called

822 views Asked by At

I am building a dashboard using panel and trying to figure out how to have a change of a control ("threshold" in the below class) fire a process that updates an attribute of the class before any other functions are called that will use that attribute. Basically, a change in the threshold widget should change an attribute self.table and then more than 1 functions will reference it to create tables and plots for the dashboard. How to make this happen? This is the start of the class where the widgets are declared and the class initialized....

class BinaryPerformDashComponents(param.Parameterized):
    
    bins = param.ObjectSelector(default=10, objects=[], label='Number of Bins')
    threshold = param.Number(default=0.5, step=0.01, bounds=(0, 1), allow_None=False)
    
    
    def __init__(self, actual, pred, df, *args, **kwargs):
        
        super(type(self), self).__init__(*args, **kwargs)

        
        self.param.bins.objects =[5,10,20,50,100]  # set the list of objects to select from in the widget
        
        self.df = self.create_df(actual,pred,df)
1

There are 1 answers

0
Sander van den Oord On BEST ANSWER

Here's an example where a change in a parameter threshold, changes the value of a boolean, and because that boolean changes, other updates get triggered after that:

import param
import panel as pn
pn.extension()

class BinaryPerformDashComponents(param.Parameterized):
    
    bins = param.ObjectSelector(default=10, objects=[5,10,20,50,100], label='Number of Bins')
    threshold = param.Number(default=0.5, step=0.01, bounds=(0, 1))
    
    boolean_ = param.Boolean(True)
        
    @param.depends('threshold', watch=True)
    def _update_boolean(self):
        self.boolean_ = not self.boolean_
        
    @param.depends('boolean_', watch=True)
    def _update_bins(self):
        self.bins = 20
        
instance = BinaryPerformDashComponents()

pn.Row(instance)

Here's some other questions + answers using the same mechanism:

Use button to trigger action in Panel with Parameterized Class and when button action is finished have another dependency updated (Holoviz)

How do i automatically update a dropdown selection widget when another selection widget is changed? (Python panel pyviz)