Python bokeh CustomJS callback update DataTable widget

时光毁灭记忆、已成空白 提交于 2020-01-24 00:45:08

问题


How can I update the values of my DataTable widget using Select widget? Here is my sample code:

import pandas as pd
from bokeh.io import show
from bokeh.layout import column
from bokeh.models import ColumnDataSource, CustomJS, Select
from bokeh.models.widgets import DataTable, TableColumn

df = pd.DataFrame({'a': range(10,50), 'b': range(110,150)})

source_foo = ColumnDataSource(data=df.loc[df['a'] < 25])
source_bar = ColumnDataSource(data=df.loc[df['a'] > 25])
source_fill = ColumnDataSource(data=df.loc[df['a'] < 25])

table_columns = [TableColumn(field=i, title=i) for i in ['a', 'b']]

select = Select(title='Selected value:', value='foo', options=['foo', 'bar'])

update = CustomJS(args=dict(source_fill=source_fill, source_foo=source_foo,
        source_bar=source_bar), code="""

    var data_foo = source_foo.data;
    var data_bar = source_bar.data;
    var data_fill = source_fill.data;
    var f = cb_obj.value;
    var list = ['a', 'b']

    if (f == 'foo') {
        for(var i = 0, size = list.length; i < size ; i++) {
            var e = list[i];
            delete data_fill[e];
            data_fill[e] = data_foo[e];
        }
    }
    if (f == 'bar') {
        for(var i = 0, size = list.length; i < size ; i++) {
            var e = list[i];
            delete data_fill[e];
            data_fill[e] = data_bar[e];
        }
    }

    source_fill.change.emit();
    """)

select.js_on_change('value', update)

data_table = DataTable(source=source_fill, columns=table_columns, width=150,
    height=300, row_headers=False, selectable=False)

layout = column(select, data_table)

bio.show(layout)

Here the data values are not changing if selectable=False. If I set selectable=True then the first row is refreshed. If I reorder one of the columns of DataTable (regardless of selectable) then the values are refreshed. How can refreshing be forced automatically?

Thank you!


回答1:


You can just make the source_fill.data pointer to new data:

if (f == 'foo') { source_fill.data = source_foo.data; } if (f == 'bar') { source_fill.data = source_bar.data; }



来源:https://stackoverflow.com/questions/47517922/python-bokeh-customjs-callback-update-datatable-widget

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