将代码从 Bokeh 0.10.0 迁移到 0.11.0

Migrating code from Bokeh 0.10.0 to 0.11.0

我有以下来自 reddit 线程的 Bokeh 0.10.0 实时流式传输示例。

import time
from random import shuffle
from bokeh.plotting import figure, output_server, cursession, show

# prepare output to server
output_server("animated_line")

p = figure(plot_width=400, plot_height=400)
p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], name='ex_line')
show(p)

# create some simple animation..
# first get our figure example data source
renderer = p.select(dict(name="ex_line"))
ds = renderer[0].data_source

while True:
    # Update y data of the source object
    shuffle(ds.data["y"])

    # store the updated source on the server
    cursession().store_objects(ds)
    time.sleep(0.5)

我知道0.11.0版本没有cursession了。 Bokeh 0.11.0 中的代码会是什么样子?这是我的尝试。我错过了什么吗?基本上,我想要下面的代码做的是 运行 作为一个应用程序,这样当我提供实时流数据时,我可以更新源并实时绘制它。

from bokeh.models import ColumnDataSource, HoverTool, HBox, VBoxForm
from bokeh.plotting import Figure, output_file, save
from bokeh.embed import file_html
from bokeh.models import DatetimeTickFormatter, HoverTool, PreText
from bokeh.io import curdoc
from bokeh.palettes import OrRd9, Greens9

plot = Figure(logo=None, plot_height=400, plot_width=700, title="",
       tools=["resize,crosshair"])

source = ColumnDataSource(data=dict(x=[], y=[]))
plot.line([1,2,3], [10,20,30], source=source, legend='Price', line_width=1, line_color=OrRd9[0])

curdoc().add_root(HBox(plot, width=1100))

您可能想要添加一个定期回调,例如:

def update():
    ds.data["y"] = shuffle(y)

curdoc().add_periodic_callback(update, 500)

但实际上您还需要将数据放入列数据源中,并告诉 line 您要使用的列,而不是将列表文字传递给 figure:

source = ColumnDataSource(data=dict(x=[1,2,3], y=[10,20,30]))
plot.line('x', 'y', source=source, legend='Price', 
          line_width=1, line_color=OrRd9[0])