我正在使用参数化类通过Panel Holoviz构建仪表板。
在本课程中,我希望有一个按钮,当按下该按钮可以开始训练模型,而当模型结束训练时,它需要显示基于该模型的图形。
如何使用Class在Panel中建立此类依赖关系?
答案 0 :(得分:1)
下面的示例显示了按下按钮时如何触发“按钮”,后者触发方法train_model(),完成后触发方法update_graph()。
关键在于 lambda x:x.param.trigger('button')和 @ param.depends('button',watch = True):
import numpy as np
import pandas as pd
import holoviews as hv
import param
import panel as pn
hv.extension('bokeh')
class ActionExample(param.Parameterized):
# create a button that when pushed triggers 'button'
button = param.Action(lambda x: x.param.trigger('button'), label='Start training model!')
model_trained = None
# method keeps on watching whether button is triggered
@param.depends('button', watch=True)
def train_model(self):
self.model_df = pd.DataFrame(np.random.normal(size=[50, 2]), columns=['col1', 'col2'])
self.model_trained = True
# method is watching whether model_trained is updated
@param.depends('model_trained', watch=True)
def update_graph(self):
if self.model_trained:
return hv.Points(self.model_df)
else:
return "Model not trained yet"
action_example = ActionExample()
pn.Row(action_example.param, action_example.update_graph)
“操作”按钮上的有用文档:
https://panel.pyviz.org/gallery/param/action_button.html#gallery-action-button
Action参数的其他有用示例:
https://github.com/pyviz/panel/issues/239