I am very new to bokeh, and I hit some obstacle while trying to use streaming data and bokeh server.
What I want to achieve is to have the server code keep generating some data and serve the same data to all browser sessions that connects to this. Reading the documentation, I thought this can achieve what I want to do:
import numpy as np
from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure# Set up data
N = 100
data_source = ColumnDataSource(data={
ātimeā : np.zeros(0,dtype=np.float32),
ātemperatureā : np.zeros(0,dtype=np.float32),
ācurrentā : np.zeros(0,dtype=np.float32)
})# Set up plot
plot_temperature = figure(plot_height=400, plot_width=400, title=ātemperatureā,
tools=ācrosshair,pan,reset,save,wheel_zoomā,
y_range=[20, 30])
plot_temperature.line(ātimeā, ātemperatureā, source=data_source, line_width=3, line_alpha=0.6)plot_current = figure(plot_height=400, plot_width=400, title=āCurrentā,
tools=ācrosshair,pan,reset,save,wheel_zoomā,
y_range=[20, 30])
plot_current.line(ātimeā, ācurrentā, source=data_source, line_width=3, line_alpha=0.6)index=0
def update_data():
global index
new_data={
ātimeā : np.array([index]),
ātemperatureā : np.random.uniform(20,30,1),
ācurrentā : np.random.uniform(20,30,1)
}
data_source.stream(new_data,rollover=N)
index+=1
print(āupdate:ā,index)# Set up layouts and add to document
plot_area = column(plot_temperature,plot_current,width=500)curdoc().add_periodic_callback(update_data, 50)
curdoc().add_root(plot_area)
curdoc().title = āEnvironment Logā
Here is what I am confused
First, I want the server generating data even there is no browser connected to it. That is not the case. It only begins as one connection is made.
Second, if I only have one connection and keeps it forever, it works fine. But once I open a differnet connection, the data is not updated properly.
I suspects I am misunderstanding the concept of Document in bokeh. But I do not know how to achieve what I want to achieve.
I also checked this example https://github.com/bokeh/bokeh/tree/2.1.1/examples/app/ohlc and it seems to have the same behavior: it only works with the first opened session.