I am trying to use Bokeh to plot a streaming dataset within a Jupyter notebook. Here is what I have so far.
From the command line I start the bokeh server by running the command
$> bokeh server
Here is the code from my Jupyter notebook
import numpy as np
from IPython.display import clear_output
# ------------------- new cell ---------------------#
from bokeh.models.sources import ColumnDataSource
from bokeh.client import push_session
from bokeh.driving import linear
from bokeh.plotting import figure
from bokeh.io import curdoc, output_notebook, show
# ------------------- new cell ---------------------#
output_notebook()
# ------------------- new cell ---------------------#
my_figure = figure(plot_width=800, plot_height=400)
test_data = ColumnDataSource(data=dict(x=[0], y=[0]))
linea = my_figure.line("x", "y", source=test_data)
# ------------------- new cell ---------------------#
new_data=dict(x=[0], y=[0])
x = []
y = []
step_size = 0.1 # increment for increasing step
@linear(m=step_size, b=0)
def update(step):
x.append(step)
y.append(np.random.rand())
new_data['x'] = x
new_data['y'] = y
test_data.stream(new_data, 10)
clear_output()
show(my_figure)
if step > 10:
session.close()
# ------------------- new cell ---------------------#
# open a session to keep our local document in sync with server
session = push_session(curdoc())
period = 100 # in ms
curdoc().add_periodic_callback(update, period)
session.show() # open a new browser tab with the updating plot
session.loop_until_closed()
Currently, the result I get is a flashing plot within the Jupyter notebook and also a nicely updating plot in a new browser tab. I would like either of the following
- a nicely updating plot in Jupyter, without the flashing
- just the plot in the new browser tab
I tried removing show(my_figure)
but each update opened a new tab. I also tried reducing the refresh rate to 10 ms, period = 10
; session.show()
works great but the notebook eventually crashes because it cannot refresh that fast.
How do I get a good refresh rate of the bokeh plot in Jupyter? Or how do I turn off the Jupyter plot and only have one tab showing the updating plot?
push_notebook or, with 0.12.5, [bokeh server app embedded in notebook](https://github.com/bokeh/bokeh/blob/master/examples/howto/server_embed/notebook_embed.ipynb). You are currently recreating or reshowing every plot in its entirety, which will give poor results. Also, using
bokeh.client` doubles the amount of network traffic. – Steven C. Howell