如何从选择散景中更改数据源

问题描述 投票:1回答:1

我正在使用Bokeh和Python 2.7

我试图更新数据源以根据选择框更改绘图。但我无法更新情节。我究竟做错了什么?或者,还有更好的方法?

码:

from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, output_file, show, output_notebook
from bokeh.models.widgets import Select
from bokeh.io import curdoc
from bokeh.layouts import column, row
from bokeh.io import output_file, show
from bokeh import models
import pandas as pd

d1 = dict(x=  [10,4,6,4], y =  [6,2,8,10])

d2 = dict(x=  [23,12,50,30], y =  [5,10,23,18,12])

source = ColumnDataSource(data=d1)

p = figure()

select = Select(title="Select d",  options=['d1', 'd2'])

def update_plot(attrname, old, new):
    if new == 'd1':
        newSource = d1

    if new == 'd2':
        newSource = d2


    source.data =  newSource

p.line(x='x', y='y',source = source)

select.on_change('value', update_plot)
layout = column(row(select, width=400), p)
curdoc().add_root(layout)
show(layout)
python plot datasource bokeh
1个回答
1
投票

您需要使用散景服务器启动散景,如下所示:

bokeh serve myscript.py

然后在浏览器中打开localhost:5006

如果您在没有服务器的情况下启动散景,那么它只会创建一个静态html文件,并且您无法使页面调用您的函数(这就是您没有看到prints的原因)或者在您的python代码之后更改页面初始负荷。来自the docs

Bokeh的体系结构使得高级“模型对象”(表示绘图,范围,轴,字形等等)在Python中创建,然后转换为客户端库使用的JSON格式,BokehJS 。 [...]但是,如果可以保持python和浏览器中的“模型对象”彼此同步,那么更多[您还可以]响应在浏览器中使用计算生成的UI和工具事件查询使用python的全部功能

© www.soinside.com 2019 - 2024. All rights reserved.