我试图在类中使用面板小部件以更改绘图。下面的示例只输出一次绘图,但是当我从小部件中选择其他变量时,它不会更新绘图。
import pandas as pd
import param
import holoviews as hv
hv.extension('bokeh')
import panel as pn
pn.extension()
df = pd.DataFrame({'index':[1,2,3,4,5],
'signal1':[1,2,3,4,5],
'signal2':[5,4,3,2,1]}).set_index('index')
class Plot(param.Parameterized):
def __init__(self, df):
self.multi_select = pn.widgets.MultiSelect(name='Vars', value=['signal1'],
options=['signal1', 'signal2'])
self.df = df
self.watcher = self.multi_select.param.watch(self.dashboard, 'value')
super().__init__()
def dashboard(self, *events, **kwargs):
self.plt = hv.Curve(self.df, [self.df.index.name, self.multi_select.value[0]])
return self.plt
a = Plot(df)
pn.Row(a.multi_select,a.dashboard)
但是,如果我使用param而不是面板小部件,它将按预期工作。
class Plot2(param.Parameterized):
multi_select = param.ListSelector(default=['signal1'],
objects=['signal1', 'signal2'])
df = df
def dashboard(self, **kwargs):
self.plt = hv.Curve(self.df, [self.df.index.name, self.multi_select[0]])
return self.plt
b = Plot2()
pn.Row(b.param,b.dashboard)
我是在做错什么还是根本不可能? 谢谢
答案 0 :(得分:0)
首先,我对您的代码进行了一些修改。主要变化是:我添加了
Plot.dashboard()
中的try-except子句,其中包含有关事件的打印语句和变量self.multi_select
view = pn.Row(a.multi_select,a.dashboard)
和view.app()
的外部bokeh服务器,以从try-except-clause中获取照片。完整的Plot():
class Plot(param.Parameterized):
def __init__(self, df, **params):
super(Plot, self).__init__(**params)
self.multi_select = pn.widgets.MultiSelect(name='Vars', value=['signal1'],
options=['signal1', 'signal2'])
self.df = df
self.watcher = self.multi_select.param.watch(self.dashboard, 'value')
def dashboard(self, *events, **kwargs):
try:
event = events[0] # get first event in events
select = events[0].obj # obj is our multi_select
# see, what event looks like, and if select and self.multi_select are the same
print(event, '\n', select, '\n', self.multi_select)
except IndexError: pass
self.plt = hv.Curve(self.df, [self.df.index.name, self.multi_select.value[0]])
return self.plt
a = Plot(df)
view = pn.Row(a.multi_select,a.dashboard)
view.app()
因此您的代码可以按预期工作,但是更改不会显示。
第二:
将曲线图与多选和四处戏弄分开,表明只有在方法view
依赖于已更改的参数(此处是用于图的列的名称)的情况下,曲线才被更改:
class Curve_(param.Parameterized):
col = param.String()
def __init__(self, df, col, **params):
super(Curve_, self).__init__(**params)
self.df = df
self.col = col
@param.depends('col', watch=True)
def view(self):
return hv.Curve(self.df, [self.df.index.name, self.col])
t=Curve_(df, 'signal1')
pn.Column(t.view)
结合类Plot
和Curve_
并使用参数dashboard_trigger
来保持仪表板的更新,一切现在都按预期工作。
class Plot(param.Parameterized):
dashboard_trigger = param.Number(1)
def __init__(self, df, **params):
super(Plot, self).__init__(**params)
self.multi_select = pn.widgets.MultiSelect(name='Vars', value=['signal1'],
options=['signal1', 'signal2'])
self.curve = Curve_(df, self.multi_select.value[0])
self.watcher = self.multi_select.param.watch(self.set_column, 'value')
def set_column(self, *events):
self.curve.col = events[0].obj.value[0]
self.dashboard_trigger = self.dashboard_trigger + 1
@param.depends('dashboard_trigger', watch=True)
def dashboard(self, *events, **kwargs):
return self.curve.view
a = Plot(df)
pn.Row(a.multi_select,a.dashboard)