Veenit Veenit - 6 months ago 68
Python Question

Migrating code from Bokeh 0.10.0 to 0.11.0

I have the below example of real time streaming in Bokeh 0.10.0 from reddit thread.

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)


I know that there is no cursession from 0.11.0 version. What would the code be like in Bokeh 0.11.0? Here is my attempt at it. Am I missing something? Basically, what I want the below code to do is to run as an app so that when I provide live streaming data, I can update the source and plot it realtime.

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))

Answer

you probably want to add a periodic callback, something like:

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

curdoc().add_periodic_callback(update, 500)

But also you actually need to put the data into the column data source, , and tell line the columns you want to use, instead of passing list literals to 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])
Comments