问题描述
我正在使用面板构建仪表板并试图弄清楚如何更改控件(下面类中的“阈值”)触发一个进程,在调用任何其他函数之前更新类的属性使用该属性。基本上,阈值小部件的更改应该更改属性 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)
以下是一些其他问题 + 使用相同机制的答案: