python-3.x – Bokeh DataTable在触发(‘更改’)后不会更新而不点击标题

散景版:0.10
Python:3.4
木星:4.x

目标:创建一个仅显示从散点图中选择的数据的表

问题:DataTable仅在单击后自行刷新
尽管:s2.trigger(‘改变’).在Bokeh网站上的其他例子之一
plot将使用此技术更新另一个:请参阅http://bokeh.pydata.org/en/latest/docs/user_guide/interaction.html#customjs-for-selections

如果你使用上面提到的版本,下面的代码应该在Jupyter笔记本中运行.

并且,感谢您的帮助.

    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]))

    source.callback = CustomJS(args=dict(s2=s2), 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]])
            }
            s2.trigger('change');
        """)


    # 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)


    show(vform(left,dt))
最佳答案
在CustomJS中只触发s2更改,因此dt不会更改是正常的.

这将完成工作,dt移到JS之上,dt在JS中传递,并且dt被触发:

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');
    """)

转载注明原文:python-3.x – Bokeh DataTable在触发(‘更改’)后不会更新而不点击标题 - 代码日志