【发布时间】:2019-10-19 12:56:25
【问题描述】:
我正在尝试编写一个散景应用程序,其中通过单击地图中的一个点来选择 DataTable 的数据源。此 mapplot 的回调函数选择数据,然后应将其绘制在 DataTable 中。
我使用“dummy-data”启动 DataTable 以使其正确显示。单击一个点(站)后,mapplot 的回调函数应该更新表的数据源,因此显示选定的数据。但我在更新表的数据源时遇到了困难,只有一行更新/显示。
这是我的应用程序中的一些简化代码:
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
from bokeh.events import Tap
from bokeh.models import TapTool, DateFormatter
from bokeh.tile_providers import get_provider, Vendors
from bokeh.models.widgets import DataTable, TableColumn
from bokeh.layouts import layout, row
from bokeh.io import curdoc
def callback(event):
"""
Callback function for tap event in mapplot
"""
# change data for table
df = ColumnDataSource(data=dict(index=[50000000,55000000,60000000], LT=[13.5,20.2,-3.1]))
data_table.source = df
dfm = ColumnDataSource(data=dict(x=[1196199], y=[5907860])) # samplestation for mapplot
df = ColumnDataSource(data=dict(index=[0], LT=['NaN'])) # initial data for datatable
#### Mapplot
tile_provider = get_provider(Vendors.CARTODBPOSITRON)
tools_to_show_p1 = 'box_zoom,pan,save,reset,tap,wheel_zoom'
p1 = figure(x_range=(1043319, 1471393), y_range=(5684768, 6176606),
x_axis_type="mercator", y_axis_type="mercator",
tools=tools_to_show_p1, sizing_mode="scale_both")
p1.add_tile(tile_provider)
p1.circle(x="x", y="y", size=15, fill_color="blue", fill_alpha=0.4, source=dfm)
#### Datatable
datefmt = DateFormatter(format="%m/%d/%Y %H:%M:%S")
columns = [
TableColumn(field="index", title="date", formatter=datefmt),
TableColumn(field="LT", title="LT"),
]
data_table = DataTable(source=df, columns=columns, width=300, height=600, fit_columns=True, editable=True)
#### Events
taptool = p1.select(type=TapTool)
p1.on_event(Tap, callback)
doc_layout = layout(children=[row(p1, data_table)], sizing_mode='fixed')
curdoc().add_root(doc_layout)
注意:你不能在 jupyter notebook 中运行它,它必须以bokeh serve xxx.py --show 运行
如何更新我的 DataTable 以显示整个数据?
(请不要使用 JsCallback 函数,因为我有不同的逻辑如何完成整个回调,这只是一个简化的示例)
【问题讨论】:
标签: python python-3.x datatable bokeh