Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

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

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)
question from:https://stackoverflow.com/questions/65644242/panel-in-python-how-to-set-the-order-that-events-are-called

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...