Python 中的面板 - 如何设置事件的调用顺序

问题描述

我正在使用面板构建仪表板并试图弄清楚如何更改控件(下面类中的“阈值”)触发一个进程,在调用任何其他函数之前更新类的属性使用该属性。基本上,阈值小部件的更改应该更改属性 self.table,然后超过 1 个函数将引用它来为仪表板创建表格和绘图。如何做到这一点?这是小部件声明和类初始化的类的开始......

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,df)

解决方法

这是一个示例,其中参数阈值的更改会更改布尔值的值,并且由于该布尔值发生更改,此后会触发其他更新:

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)

以下是一些其他问题 + 使用相同机制的答案:

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)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...