gpt4 book ai didi

javascript - 如何在单击时获取 Bokeh DataTable 单元格内容?

转载 作者:行者123 更新时间:2023-11-28 20:38:03 25 4
gpt4 key购买 nike

你知道如何通过点击获取 Bokeh DataTable 单元格的值吗?

如果我使用:

data = dict(
items=bokehItems,
values0=bokehValues0,
values1=bokehValues1,
values2=bokehValues2
)

source = ColumnDataSource(data)

columns = [
TableColumn(field="items", title="Item"),
TableColumn(field="values0", title="Value"),
TableColumn(field="values1", title="Cluster"),
TableColumn(field="values2", title="Interaction"),
]



data_table_worst_cases = DataTable(source=source, columns=columns, height=280,
row_headers=False, fit_columns=True)


source.callback = CustomJS(args=dict(source=source), code="""
console.log( cb_obj.get('data'));
""")

当我在表格中单击时,我总是获得完整表格的内容,而不是特定单元格的内容。

最佳答案

此版本使用 Python 回调(针对 Bokeh v1.0.4 进行了更新)。运行方式:bokeh serve --show app.py

from random import randint
from datetime import date
from bokeh.models import ColumnDataSource, TableColumn, DateFormatter, DataTable
from bokeh.layouts import column
from bokeh.models.widgets import TextInput
from bokeh.plotting import curdoc

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

source = ColumnDataSource(data)

columns = [TableColumn(field = "dates", title = "Date",
formatter = DateFormatter()),
TableColumn(field = "downloads", title = "Downloads")]

data_table = DataTable(source = source, columns = columns, width = 280, height = 280, editable = True)
table_row = TextInput(value = '', title = "Row index:")
table_cell_column_1 = TextInput(value = '', title = "Date:")
table_cell_column_2 = TextInput(value = '', title = "Downloads:")

def function_source(attr, old, new):
try:
selected_index = source.selected.indices[0]
table_row.value = str(selected_index)
table_cell_column_1.value = str(source.data["dates"][selected_index])
table_cell_column_2.value = str(source.data["downloads"][selected_index])
except IndexError:
pass

source.selected.on_change('indices', function_source)
curdoc().add_root(column(data_table, table_row, table_cell_column_1, table_cell_column_2))

结果:

enter image description here

关于javascript - 如何在单击时获取 Bokeh DataTable 单元格内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41746902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com