gpt4 book ai didi

python - 如何从选择 Bokeh 更改数据源

转载 作者:太空宇宙 更新时间:2023-11-03 14:22:07 26 4
gpt4 key购买 nike

我正在使用 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)

最佳答案

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

bokeh serve myscript.py

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

如果您在没有服务器的情况下启动 Bokeh ,那么它只会创建一个静态 html 文件,并且您无法让页面调用您的函数(这就是为什么您看不到 print )或在初始加载后使用 python 代码更改页面。来自 the docs :

The architecture of Bokeh is such that high-level “model objects” (representing things like plots, ranges, axes, glyphs, etc.) are created in Python, and then converted to a JSON format that is consumed by the client library, BokehJS. [...] However, if it were possible to keep the “model objects” in python and in the browser in sync with one another, then more [you could also] respond to UI and tool events generated in a browser with computations or queries using the full power of python

关于python - 如何从选择 Bokeh 更改数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47871398/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com