python - 在 Bokeh 中按下按钮时如何更改数据框?

标签 python bokeh

我正在尝试为我的数据框设计一个前端显示。我想放一个按钮,选择哪个过滤器来过滤我的数据框。我已经尝试阅读文档和示例示例,但直到现在我还没有成功。 这是我的代码供引用:

source = ColumnDataSource(data=dict())
act=""

def act_good():
    global act
    act='Good'

def update():
    current = data[data['Activity']==act]
    source.data = {
        'Tag'             :current.Tag,
        'UpdateQuality'   : current.UpdateQuality,
        'Activity' : current.Activity
    }


checkbox_act_good = RadioButtonGroup(
        labels=["Activity Good"])
checkbox_act_good.on_click(act_good())


columns = [
    TableColumn(field="Tag", title="Tag"),
    TableColumn(field="UpdateQuality", title="Quality"),
    TableColumn(field="Activity", title="Activity")
]

data_table = DataTable(source=source, columns=columns, width=800)
controls = widgetbox(slider,button,checkbox_group,but)
table = widgetbox(data_table)
curdoc().add_root(row(controls, table))
update()

这是我第一次使用 Bokeh 。

最佳答案

您可以通过不同的方式进行更新。例如,您可以创建一个新的 ColumnDataSource 并像这样更新 DataTable 源:

from bokeh.models import ColumnDataSource
from bokeh.models.widgets.tables import DataTable, TableColumn
from bokeh.models.widgets.buttons import Button
from bokeh.layouts import column
from bokeh.io import curdoc


columns = [
    TableColumn(field="x", title="X"),
    TableColumn(field="y", title="Y"),
]
init_source = ColumnDataSource(data=dict(x=[''],y=['']))
table = DataTable(
    source=init_source,
    columns=columns,
    reorderable=False,
)

def update_table():
    new_source = ColumnDataSource(dict(
        x=[1, 2, 3, 4, 5, 6],
        y=[1, 2, 3, 4, 5, 6],
    ))
    table.source.data = new_source.data

bt = Button(
    label="Update Table",
    button_type="success",
    width=50
)

bt.on_click(update_table)

curdoc().add_root(column(children=[table, bt]))

使用 bokeh serve --show filepath/file.py 启动这个文件。您可以像这样使用 DataFrame 作为 CDS 参数:ColumnDataSource(df) 以创建 CDS。

关于python - 在 Bokeh 中按下按钮时如何更改数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51309023/

相关文章:

python - Bokeh 图未在 nbviewer 中显示

javascript - 如何使用外部 JavaScript 代码访问和更新 Bokeh 图或小部件?

python - 运行时错误: You must setup() the GPIO channel first

python - 为 python 2.7 编译 libdnet

python - Cython:加速简单代码

python-3.x - 在 Bokeh 中,如何向时间序列图表(悬停工具)添加工具提示?

python - tf.constant 和 tf.convert_to_tensor 有什么区别

python - Django登录部分

python - 如何并排比较两个交互式 Bokeh 图?

python - 在 Bokeh 中的饼图上添加自定义工具提示并正确显示