【发布时间】:2018-11-22 10:00:33
【问题描述】:
我正在尝试将数据表与散景中的多选小部件连接起来。我四处搜索并收集到我需要开发一个函数来更新数据表的数据源,但我似乎有两个问题。
- 单击多选对象后,我似乎无法访问它的值。
- 收到更改后,我似乎无法将更改推送到笔记本。
这是我的代码示例:
import pandas as pd
from bokeh.io import push_notebook
from bokeh.plotting import show, output_notebook
from bokeh.layouts import row
from bokeh.models.widgets import MultiSelect, DataTable, TableColumn
from bokeh.models import ColumnDataSource
output_notebook()
df=pd.DataFrame({'year':[2000,2001,2000,2001,2000,2001,2000,2001],
'color':['red','blue','green','red','blue','green','red','blue'],
'value':[ 0,1,2,3,4,5,6,7]})
columns=[TableColumn(field=x, title=x) for x in df.columns]
source=ColumnDataSource(df)
data_table=DataTable(source=source,columns=columns)
years=[2000,2001,2000,2001,2000,2001,2000,2001]
## MultiSelect won't let me store an integer value, so I convert them to strings
multi=MultiSelect(title="Select a Year", value=['2000','2001'],options=[str(y) for y in set(years)])
def update(attr,old, new):
yr=multi.value
yr_vals=[int(y) for y in yr]
new_data=df[df.year.isin(yr_vals)]
source.data=new_data
push_notebook(handle=t)
multi.on_change('value',update)
t=show(row(multi,data_table),notebook_handle=True)
【问题讨论】:
-
恐怕你只能在 Jupyter 笔记本上使用 JavaScript 回调(CustomJS)。如果你想使用 python 回调,那么你需要将 bokeh 作为服务器应用程序运行(使用
bokeh serve) -
我认为从笔记本更新散景图可以正常工作(请参阅documentation),但散景小部件(多选)不起作用。显然您可以使用 jupyter 小部件进行交互 (next point in the same documentation)
-
@ChesuCR 在笔记本中嵌入带有真正 python 回调的完整散景服务器应用程序已经有一段时间了。见:github.com/bokeh/bokeh/blob/master/examples/howto/server_embed/…
标签: python datatable jupyter-notebook bokeh multi-select