简体   繁体   English

Python3:从新数据源更新散景数据表

[英]Python3: Update bokeh DataTable from new DataSource

I'm trying to write a bokeh application, in which a datasource for a DataTable is selected by clicking on a point in a mapplot.我正在尝试编写一个散景应用程序,其中通过单击地图中的一个点来选择 DataTable 的数据源。 A callback function of this mapplot selects the data, which then should be plotted in a DataTable.此 mapplot 的回调 function 选择数据,然后应将其绘制在 DataTable 中。

I iniciate the DataTable with "dummy-data" to get it displayed correctly.我用“dummy-data”启动 DataTable 以使其正确显示。 After clicking on a point (station) the callback function of the mapplot should update the table's datasource, so the selected data is displayed.单击一个点(站)后,mapplot 的回调 function 应更新表的数据源,因此显示所选数据。 But I struggle with updating the table's datasource, there's only one row updated/displayed.但是我在更新表格的数据源时遇到了困难,只有一行更新/显示。

Here's some simplified code from my app:这是我的应用程序中的一些简化代码:

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)

Note: you can't run this in jupyter notebook, it has to be run as bokeh serve xxx.py --show注意:你不能在 jupyter notebook 中运行它,它必须作为bokeh serve xxx.py --show运行

How do I update my DataTable to display the whole data?如何更新我的 DataTable 以显示整个数据?

(Please no JsCallback functions, as I have a different logic how the whole callback is done, this is just a simplified example) (请不要使用 JsCallback 函数,因为我对整个回调的完成方式有不同的逻辑,这只是一个简化的示例)

You should not replace the ColumnDataSource .您不应该替换ColumnDataSource Instead, you should update the CDS, by assigning to its .data property:相反,您应该通过分配给它的.data属性来更新CDS:

data_table.source.data = new_data

Note that.data should generally be a plain Python dict .请注意,.data 通常应该是普通的 Python dict You can use ColumnDataSource.from_df(...) to convert a DatFrame to the expected format.您可以使用ColumnDataSource.from_df(...)将 DatFrame 转换为预期的格式。 In future versions these kinds of things will issue explicit errors.在未来的版本中,这些类型的东西会发出明确的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM