How to embed a streaming Bokeh graph in a Flask site?

341 Views Asked by At

How could the following streaming graph be embedded in a flask app?

from bokeh.io import curdoc
from bokeh.models import ColumnDataSource
from bokeh.plotting import Figure

import numpy as np

source = ColumnDataSource(dict(x=[], y=[], avg=[]))

fig = Figure()
fig.line(source=source, x='x', y='y', line_width=2, alpha=0.85, color='red')
fig.line(source=source, x='x', y='avg', line_width=2, alpha=0.85, color='blue')

ct = 0
sine_sum = 0


def update_data():
    global ct, sine_sum
    ct += 1
    sine = np.sin(ct)
    sine_sum += sine
    new_data = dict(x=[ct], y=[sine], avg=[sine_sum / ct])
    print(new_data)
    source.stream(new_data, 100)

curdoc().add_root(fig)
curdoc().add_periodic_callback(update_data, 500)

I could not find an example of this after the bokeh.embed api change was made.

0

There are 0 best solutions below