散景数据表过滤不一致

时间:2019-03-10 15:14:46

标签: python bokeh

抱歉,由于这个问题很不一致,我无法为此问题制作任何可重复的代码。

所以我有一个bokeh数据表,并且正在使用4个下拉框对其进行过滤。数据表根据下拉框的值进行更新,并且这些更新是用JS编写的。过滤可以按预期工作,但是对于下拉列表的某些非常特定的组合而言,奇怪的是,它在数据表中不显示任何内容。我想知道我的数据是否有问题,但是我将所有内容都强制转换为字符串,但这仍然给我带来了同样的问题。

更新写在这里:

combined_callback_code = """
var data = source.data;

var original_data = original_source.data;

var origin = origin_select_obj.value;

var classification = classification_select_obj.value;

var currency = currency_select_obj.value;

var grade = grade_select_obj.value;

for (var key in original_data) {
    data[key] = [];
    for (var i = 0; i < original_data['Origin'].length; ++i) {
        if ((origin === "ALL" || original_data['Origin'][i] === origin) &&
            (classification === "ALL" || original_data['Classification'][i] === classification) &&
            (currency === "ALL" || original_data['Currency'][i] === currency) &&
            (grade === "ALL" || original_data['BrokenPct'][i] === grade)){
            data[key].push(original_data[key][i]);
            }
    }
    }

target_obj.change.emit();
source.change.emit();
"""



# define the filter widgets, without callbacks for now
origin_list = ['ALL'] + df['Origin'].unique().tolist()
origin_select = Select(title="Origin:", value=origin_list[0], options=origin_list)

classification_list = ['ALL'] + df['Classification'].unique().tolist()
classification_select = Select(title="Classification:", value=classification_list[0], options=classification_list)

currency_list = ['ALL'] + df['Currency'].unique().tolist()
currency_select = Select(title = "Currency:", value=currency_list[0], options = currency_list)

grade_list = ["ALL"] + df['BrokenPct'].unique().tolist()
grade_select = Select(title = "Grade:", value = grade_list[0], options = grade_list)

# now define the callback objects now that the filter widgets exist
generic_callback = CustomJS(
    args=dict(source=source, 
              original_source=original_source, 
              origin_select_obj=origin_select, 
              classification_select_obj=classification_select,
              currency_select_obj = currency_select,
              grade_select_obj = grade_select,
              target_obj=data_table),
    code=combined_callback_code
)


# finally, connect the callbacks to the filter widgets
origin_select.js_on_change('value', generic_callback)
classification_select.js_on_change('value', generic_callback)
currency_select.js_on_change('value', generic_callback)
grade_select.js_on_change('value', generic_callback)

1 个答案:

答案 0 :(得分:1)

在JS回调中更新表数据的正确方法是:

var data = {};
//build your data here
source.data = data;

其中source是您的DataTable的Bokeh ColumnDataSource。 您不需要使用:

source.change.emit();

只有在仅替换部分数据(例如,一个表列。 并且如果data_table是您的Bokeh DataTable对象,那么也可以跳过:

target_obj.change.emit();

表日期在您更新其ColumnDataSource时自动更新。 看这个简单的例子:

from bokeh.io import show
from bokeh.layouts import widgetbox
from bokeh.models import ColumnDataSource, Slider, DataTable, TableColumn, CustomJS

source = ColumnDataSource(dict(x = list(range(6)), y = [x ** 2 for x in range(6)]))
columns = [TableColumn(field = "x", title = "x"), TableColumn(field = "y", title = "x**2")]
table = DataTable(source = source, columns = columns, width = 320)
slider = Slider(start = 1, end = 20, value = 6, step = 1, title = "i", width = 300)
callback_code = """ i = slider.value;
                    new_data = {"x": [1, 2, 3, 4, 5], "y": [1, 4, 9, 16, 25]}
                    table.source.data = new_data
                    table.width = 320 + i * 25;  """
callback = CustomJS(args = dict(slider = slider, table = table), code = callback_code)
slider.js_on_change('value', callback)
show(widgetbox(slider, table))