Skip to main content
 首页 » 编程设计

python之当用户在 DataTable 中单击时, Bokeh 图被停用,并且没有正确的方法来激活该图

2024年10月01日6lautakyan007

当我在 bokeh 应用程序中使用 DataTable 时,每当我在数据表中单击时,页面上的图表都会消失。 editable=True 和 editable=False 会发生这种情况。

我发现再次激活绘图的唯一方法是单击“重置”工具按钮。

问题:

  • 我在我的 Bokeh 应用程序中做错了什么吗?
  • 有没有其他人遇到过这种情况,并找到了解决方法? (也许是 Javascript 解决方法?)

屏幕截图(注意图中停用的颜色):

完整的 Bokeh 应用程序如下,使用 bokeh serve --show filename.py 运行:

from datetime import datetime, timedelta 
 
import numpy as np 
import pandas as pd 
import bokeh 
from bokeh.layouts import widgetbox, row, layout 
from bokeh.models import ColumnDataSource, Button, DataTable, TableColumn, Paragraph 
from bokeh.models.widgets.tables import DateFormatter, StringEditor 
from bokeh.plotting import curdoc, figure 
 
 
def create_dataframe(number_of_series, number_of_values): 
    t0 = datetime(2017, 1, 1) 
    data = { 
        "timestamp": [t0 + (i * timedelta(seconds=3600*2)) for i in range(number_of_values)] 
    } 
    data.update({ 
        "col{}".format(i): np.random.rand(number_of_values) 
        for i in range(number_of_series) 
    }) 
    return pd.DataFrame(data).set_index("timestamp") 
 
 
source = ColumnDataSource(create_dataframe(10, 1000)) 
 
 
btn = Button(label="Click Me") 
 
 
my_figure = figure(x_axis_type="datetime") 
my_figure.line(y="col0", x="timestamp", source=source) 
 
data_table = DataTable(editable=True, source=source, columns=[ 
    TableColumn(title="Timestamp", field="timestamp", formatter=DateFormatter(format="%Y-%m-%d %H:%M:%S"), editor=StringEditor()), 
    TableColumn(title="col0", field="col0") 
]) 
 
page_layout = layout([ 
    [widgetbox(Paragraph(text="Bokeh version: {}".format(bokeh.__version__)))], 
    [widgetbox(btn), my_figure], 
    [data_table] 
]) 
 
curdoc().add_root(page_layout) 

请您参考如下方法:

这实际上是使用 Bokeh ColumnDataSource 背后的预期行为。当您选择表中的一行时,即在数据源中注册选定属性。看

https://docs.bokeh.org/en/latest/docs/user_guide/data.html#columndatasource

一开始您无法注意到这一点,因为绘图上有大量数据点 - 选择时任何一个都不可见。尝试按住 shift 单击并选择多行,您会看到绘图的各个部分变成深蓝色 - 对应到那些行。

防止该行为的最简单方法是使用单独的数据源。

如果您希望它们相同,则每次更新时都必须恢复 source.selected 字典,这似乎毫无意义。

from datetime import datetime, timedelta 
 
import numpy as np 
import pandas as pd 
import bokeh 
from bokeh.layouts import widgetbox, row, layout 
from bokeh.models import ColumnDataSource, Button, DataTable, TableColumn, Paragraph 
from bokeh.models.widgets.tables import DateFormatter, StringEditor 
from bokeh.plotting import curdoc, figure 
 
 
def create_dataframe(number_of_series, number_of_values): 
    t0 = datetime(2017, 1, 1) 
    data = { 
        "timestamp": [t0 + (i * timedelta(seconds=3600*2)) for i in range(number_of_values)] 
    } 
    data.update({ 
        "col{}".format(i): np.random.rand(number_of_values) 
        for i in range(number_of_series) 
    }) 
    return pd.DataFrame(data).set_index("timestamp") 
 
 
source = ColumnDataSource(create_dataframe(10, 1000)) 
 
sourcetable = ColumnDataSource(create_dataframe(10, 1000)) 
 
btn = Button(label="Click Me") 
 
 
my_figure = figure(x_axis_type="datetime") 
my_figure.line(y="col0", x="timestamp", source=source) 
 
data_table = DataTable(editable=True, reorderable=False, source=sourcetable, columns=[ 
    TableColumn(title="Timestamp", field="timestamp", formatter=DateFormatter(format="%Y-%m-%d %H:%M:%S"), editor=StringEditor()), 
    TableColumn(title="col0", field="col0") 
]) 
 
page_layout = layout([ 
    [widgetbox(Paragraph(text="Bokeh version: {}".format(bokeh.__version__)))], 
    [widgetbox(btn), my_figure], 
    [data_table] 
]) 
 
curdoc().add_root(page_layout)