如何预选Bokeh.widget.DataTable中的行? [英] How do I pre-select rows in a Bokeh.widget.DataTable?

查看:45
本文介绍了如何预选Bokeh.widget.DataTable中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Bokeh可以在数据框中显示数据,如下所示:

Bokeh has the ability to display data in a dataframe as shown here:

http://docs.bokeh .org/en/latest/docs/user_guide/interaction/widgets.html#data-table

我具有以下格式的数据框:

I have a dataframe of the following format:

Index|Location|Value
-----|--------|-----
1    |1       | 10
2    |1       | 20
3    |1       | 30
4    |2       | 20
5    |2       | 30
6    |2       | 40

此数据框可以显示在数据表中,如下所示:

This dataframe can be displayed in a data table like so:

source = ColumnDataSource(data={
    LOCATION_NAME: [],
    VALUE_NAME: []
})

columns = [
    TableColumn(field=LOCATION_NAME, title=LOCATION_NAME),
    TableColumn(field=VALUE_NAME, title=VALUE_NAME)
]

data_table = DataTable(source=source, columns=columns, width=400, height=800)

def update_dt(df):
    """Update the data table. This function is called upon some trigger"""
    source.data = {
        LOCATION_NAME: mt_val_df[LOCATION_NAME],
        VALUE_NAME: mt_val_df[VALUE]}

理想情况下,我希望此数据表驱动一个热图,其中对每个位置进行的选择将导致热图中的值更改.但是,热图不能在一个位置上包含多个值.我也不知道如何预先选择数据表中的项目.

Ideally, I want this datatable to drive a heatmap where selections made for each location will lead to a changed value in the heatmap. But a heatmap cannot have several values for one location. I also do not know how to pre-select items in a datatable.

假设我有第二个数据框:

Assume that I have a second dataframe:

Index|Location|Value
-----|--------|-----
2    |1       | 20
6    |2       | 40

此数据框代表上表的一个子集-也许是上述表的一些自定义选择.

This dataframe represents a subset of the above table - perhaps some custom selection of the above.

在最基本的级别上:我具有所选行的索引.如何根据第二个数据框的行突出显示/预选上面数据表中的行?

At the most basic level: I have the index of my selection of rows. How can I highlight/pre-select rows in the data table above based on the rows of the second dataframe?

更新(2017-07-14):到目前为止,我尝试在数据源python端设置选定的索引.尽管source ['selected'] ['1d'].indices = [我的选择列表]正确设置了索引,但是在Bokeh 0.12.5的前端DataTable上没有看到相应的更新.

Update (2017-07-14): So far I tried setting the selected index on the data source python side. Although source['selected']['1d'].indices = [LIST OF MY SELECTION] does correctly set the indices, I am not seeing a corresponding update on the front-end DataTable in Bokeh 0.12.5.

我也尝试过在前端设置索引.我的问题是我不知道如何通过CustomJS传递与Bokeh不相关的参数.

I have also tried setting the indices on the front-end. My problem there is I don't know how to pass in parameters via CustomJS that are not related to Bokeh.

从更完整的角度来看:数据表中的选择如何驱动热图?

At a more complete level: How can selections in the datatable drive the heatmap?

更新(2017-07-17): 我还没有得到建议的解决方案以在Bokeh应用程序的上下文中工作! ,我目前正在寻找原因,但是为什么最后什么都没选择,要弄些麻烦.我怀疑代码字符串在页面加载开始时就被实例化了.但是,直到以后才计算我的坐标.因此,单击带有回调的按钮将导致没有选择任何内容-即使稍后已经计算了行选择.不断的帮助将不胜感激!

Update (2017-07-17): I have not gotten the proposed solution to work within the context of a Bokeh app! I am currently trying to find the cause but it's a bit tricky to follow why nothing gets selected in the end. My suspicion is that the code string gets instantiated in the beginning when the page loads. My coordinates, however, are not calculated until later. Therefore, hitting a button with the callback leads to the selection of nothing - even if later the row selection has been calculated. Continued help would be appreciated!

推荐答案

由于克莱尔·唐(Claire Tang)和布莱恩·范德文(Bryan Van de ven)的有用评论,我已经找到了上述问题的部分答案. .com/bokeh/bokeh/issues/6616"rel =" nofollow noreferrer>此处.

I have found a partial answer to the above questions thanks to the helpful comments of Claire Tang and Bryan Van de ven here.

据我所知,这可能是两个问题造成的.

This turns out to be caused be two issues as far as I am aware.

1.)如果我在CustomJS中更新了选定的索引列表,则缺少在DataTable中注册更改的信息.

1.) If I updated the selected index list in a CustomJS, I was missing to register the changes in the DataTable.

button.callback = CustomJS(args=dict(source2=source2), code="""
source2.selected['1d'].indices = [1,2,3];
//I did not "emit" my changed selection in the line below.
source2.properties.selected.change.emit();
console.log(source2)
""")  

2.)要注意的另一个重要方面是我使用的是Bokeh版本0.12.5.在此版本中,"source2.properties.selected"是未知属性(可能是因为此函数位于其他地方或尚未实现).因此,只要我保持Bokeh 0.12.5,选择对我来说也会失败.对Bokeh 0.12.6的更新和上述内容使选择项可以显示在DataTable中.

2.) The other important aspect to note is that I was on Bokeh version 0.12.5. In this version "source2.properties.selected" is an unknown property (perhaps because this function is located somehwhere else or not implemented yet). Therefore, selection also failed for me as long as I remained on Bokeh 0.12.5. An update to Bokeh 0.12.6 and the above line enabled selections to appear on the DataTable.

上面的示例显示了如何使用按钮和链接的CustomJS回调来触发对硬编码列表的选择.问题是如何基于一些更动态的计算来提供索引值的列表,因为CustomJS不允许使用与Bokeh不相关的外部参数.关于此主题,由于CustomJS的代码"属性仅接受一个字符串.我尝试了以下方法:

The above example shows how I can use a button and a linked CustomJS callback to trigger selection of hard-coded lists. The question is how to feed in a list of index values based on some more dynamic calculations because CustomJS does not allow for external parameters that are not Bokeh related. On this topic, since CustomJS "code" attribute just takes a string. I tried the following:

dynamically_calculated_index = [1,2,3]
button.callback = CustomJS(args=dict(source1=source1, source2=source2), code="""
source2.selected['1d'].indices = {0};
source2.properties.selected.change.emit();
console.log(source2)
""".format(dynamically_calculated_index)) 

我不确定这是否是最佳做法,因此我欢迎这方面的反馈,但这目前对我而言有效.正如@DuCorey指出的那样,一旦这些更改进入bokeh的主要分支,如他/她所描述的,我的问题的某些排列可以更容易地解决.

I am not sure if this is best practice, and so I welcome feedback in this regard, but this works for me for now. As pointed out by @DuCorey, once these changes are in the main branch of bokeh, some permutations of my issue could be more easily solved as described by him/her.

也:此方法仅适用于Jupyter Notebook,在该Jupyter Notebook中,将再次重新计算整个单元格,并且任何预先计算出的选定索引都将在单元格执行时绑定.我可以添加一个静态列表,该列表适用于该列表,但是如果我要动态计算以上列表,则该列表将不起作用.我仍然需要找到解决方法.

Also: This approach only works for a Jupyter Notebook where the entire cell gets recomputed again, and any pre-computed selected indices get bound at cell execution time. I can add a static list and it works for that, but if I want to dynamically calculate the above list, it will not work. I need to find a workaround still.

现在解决上述问题使我可以集中精力传播对热图选择内容的更改.

Solving the above issues now allows me to concentrate on propagating changes in what is selected to a heatmap.

答案很简单:可以更改所选项目,但是必须通过以下方式完成:

The answer here was rather simple: It is possible to change the selected items, but it has to be done in the following way:

ds.selected = {'0d': {'glyph': None, 'indicices': []},
               '1d': {indices': selected_index_list},
               '2d': {}}

以前,我只尝试替换1d索引,但是由于某些未知原因,我必须替换整个所选字典,以使bokeh应用程序注册所选索引的更改.所以不要只是做:

Previously, I had only tried to replace the 1d indices, but for some unknown reason, I have to actually replace the entire selected dictionary for the change in selected index to be registered by the bokeh app. So don't just do:

ds.selected['1d']['indices'] = selected_index_list

这现在对我有用.不过,应该感谢更多知识渊博的人的解释.

This now works for me. An explanation from someone more knowledgable would be appreciated though.

这篇关于如何预选Bokeh.widget.DataTable中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆