繁体   English   中英

Python bokeh CustomJS回调更新DataTable小部件

[英]Python bokeh CustomJS callback update DataTable widget

如何使用“ Select小部件更新DataTable小部件的值? 这是我的示例代码:

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)

如果selectable=False则数据值不会改变。 如果我将selectable=True设置selectable=True则刷新第一行。 如果我对DataTable的列之一重新排序(无论selectable为何),那么将刷新值。 如何自动强制刷新?

谢谢!

您可以仅使source_fill.data指针指向新数据:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM