我想以编程方式更改DataTable对象行选择(不使用JS,只需python)。我试图使用底层ColumnsSource的selected
属性,但没有成功。怎么办呢?
答案 0 :(得分:2)
查看示例应用程序(需要散景服务才能运行),按下按钮可更改所选行,然后更新表格和绘图。这是您需要的所有功能吗?
顺便说一句,你可以在JS中完成它而不需要使用散景服务器,但如果你有更多的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]))
答案 1 :(得分:0)
为了清楚起见,您必须完全替换source.selected属性以触发更改。所以重要的一点是:
source.selected = {'0d': {'glyph': None, 'indices': []},
'1d': {'indices': inds}, '2d': {}}
单独设置source.selected中的项目不起作用
source.selected['1d']['indices'] = inds # Doesn't work
答案 2 :(得分:0)
您可以通过以下方式在python中以编程方式选择class Image < ApplicationRecord
has_attached_file :item_image, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :item_image, content_type: /\Aimage\/.*\z/
def item_image_url
self.item_image.url(:original)
end
end
行:
DataTable
其中source.selected.indices = [list of indices to be selected]
是source
的{{1}}。如果您在ColumnDataSource
处有任何回调,请记住只有在注册了回调之后才选择行,这样它们才能被调用。