Bokeh DataTable won't update after trigger('change') without clicking on header

余生颓废 提交于 2019-12-01 08:36:14

only s2 change is triggered in the CustomJS, so it's normal that dt doesn't change.

this will do the job, dt moved above the JS, dt is passed in the JS, and dt is triggered :

dt = DataTable(source=s2, columns=columns, width=300, height=300 )
source.callback = CustomJS(args=dict(s2=s2, dt=dt), code="""
        var inds = cb_obj.get('selected')['1d'].indices;
        var d1 = cb_obj.get('data');
        var d2 = s2.get('data');
        d2['x'] = []
        d2['y0'] = []
        for (i = 0; i < inds.length; i++) {
            d2['x'].push(d1['x'][inds[i]])
            d2['y0'].push(d1['y0'][inds[i]])
        }
        console.log(dt);
        s2.trigger('change');
        dt.trigger('change');
    """)

If you only care about updating the table, then you don't actually need to pass both the data source and the "data table". This is because the "data table" already has the source as an attribute. Here is the code in its entirety (note that only "dt" is passed):

from bokeh.io import output_notebook, show
from bokeh.plotting import figure
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.models.widgets import DataTable, TableColumn
from bokeh.io import vform

output_notebook()

x = list(range(-20, 21))
y0 = [abs(xx) for xx in x]

# create a column data source for the plots to share
source = ColumnDataSource(data=dict(x=x, y0=y0))
s2 = ColumnDataSource(data=dict(x=[1],y0=[2]))

# create DataTable

columns = [
    TableColumn(field="x", title="x"),
    TableColumn(field="y0", title="y0"),
]
dt = DataTable(source=s2, columns=columns, width=300, height=300 )

# create a new plot and add a renderer
TOOLS = "box_select,lasso_select,help"
left = figure(tools=TOOLS, width=300, height=300)
left.circle('x', 'y0', source=source)

source.callback = CustomJS(args=dict(mytable=dt), code="""
var inds = cb_obj.get('selected')['1d'].indices;
var d1 = cb_obj.get('data');
var d2 = mytable.get('source').get('data');
d2['x'] = []
d2['y0'] = []
for (i = 0; i < inds.length; i++) {
d2['x'].push(d1['x'][inds[i]])
d2['y0'].push(d1['y0'][inds[i]])
}
mytable.trigger('change');
""")

show(vform(left,dt))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!