Bokeh DataTable 如何设置选定行

2024-10-01 17:21:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我想以编程方式更改DataTable对象行选择(没有JS,只有python)。我尝试过使用底层columnsource的selected属性,但没有成功。如何做到这一点?在


Tags: 对象属性编程方式jsselected底层datatable
3条回答

您可以通过以下方式在python中以编程方式选择DataTable行:

source.selected.indices = [list of indices to be selected]

其中sourceDataTable的{}。如果您对source.selected有任何回调,请记住只有在注册回调之后才选择行,以便调用它们。在

请看一个示例应用程序(需要运行bokeh serve),其中按按钮可以更改选定的行,然后同时更新表和绘图。这就是你需要的全部功能吗?在

顺便说一句,你可以在JS中完成,不需要使用bokeh服务器,但是如果你有更多的python功能,那么我想你需要它。在

from datetime import date
from random import randint

from bokeh.io import output_file, show, curdoc
from bokeh.plotting import figure
from bokeh.layouts import widgetbox, row
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn,Button

output_file("data_table.html")

data = dict(
        dates=[date(2014, 3, i+1) for i in range(10)],
        downloads=[randint(0, 100) for i in range(10)],
    )

def update():
    #set inds to be selected
    inds = [1,2,3,4]
    source.selected = {'0d': {'glyph': None, 'indices': []},
                                '1d': {'indices': inds}, '2d': {}}
    # set plot data
    plot_dates = [data['dates'][i] for i in inds]
    plot_downloads = [data['downloads'][i] for i in inds]
    plot_source.data['dates'] = plot_dates
    plot_source.data['downloads'] = plot_downloads


source = ColumnDataSource(data)
plot_source = ColumnDataSource({'dates':[],'downloads':[]})


table_button = Button(label="Press to set", button_type="success")
table_button.on_click(update)
columns = [
        TableColumn(field="dates", title="Date", formatter=DateFormatter()),
        TableColumn(field="downloads", title="Downloads"),
    ]
data_table = DataTable(source=source, columns=columns, width=400, height=280)

p = figure(plot_width=400, plot_height=400)

# add a circle renderer with a size, color, and alpha
p.circle('dates','downloads',source=plot_source, size=20, color="navy", alpha=0.5)


curdoc().add_root(row([table_button,data_table,p]))

为了清楚起见,你必须更换源.选定属性完全触发更改。所以重要的一点是:

source.selected = {'0d': {'glyph': None, 'indices': []},
                            '1d': {'indices': inds}, '2d': {}}

单独设置项目源.选定不起作用

^{pr2}$

相关问题 更多 >

    热门问题