我正在使用 Parameterized Class 构建带有 Panel Holoviz 的仪表板。
在本课程中,我想要一个按钮,当按下该按钮时开始训练模型,当模型完成训练时,它需要显示基于该模型的图表。
如何使用类在面板中构建此类依赖关系?
下面的示例显示了按下按钮时如何触发“按钮”,从而触发方法 train_model(),完成后又触发方法 update_graph()。
关键在于lambda x: x.param.trigger('按钮') and @param.depends('按钮', 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
BEFORE pushing button:
AFTER pushing button:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)