How to batch a rendering of multiple lines


I am experimenting with the following code. We plot 10 lines with their own datasources on one plot. The streaming happens periodically every second for all lines. Each line has a different amount of points generated within this one second.

from bokeh.layouts import column
from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource, Dropdown

import numpy as np
import time

class Line:
	def __init__(self, doc, plot, waiting_time):
		self._current_x = time.monotonic()
		self._current_y = 0
		self._new_points = dict(x=[], y=[])
		self._source = ColumnDataSource(data=dict(x=[], y=[]))

		doc.add_periodic_callback(self._generate_point, waiting_time)

	def _generate_point(self):
		self._current_x = time.monotonic()
		self._current_y += np.random.normal()


	def stream(self):


doc = curdoc()

p = figure(width=300, height=300, tools="pan,reset,save", y_axis_location="right")
lines = []
for i in range(10):
	lines.append(Line(doc, p, 100*i))

def stream_point():
	for line in lines:

menu = [("Item 1", "item_1"), ("Item 2", "item_2"), ("Item 3", "item_3")]
dropdown = Dropdown(label="Dropdown button", menu=menu)

doc.add_periodic_callback(stream_point, 1000)
doc.add_root(column(p, dropdown))

The problem here is that after some time the browser spends so much time rendering the plot that the dropdown button becomes unusable.

Taking a look at the profiling tools that Firefox offers I can spot that for every time the plot receives a websocket message it redraws the whole plot.You can see on the picture the black line (websocket message) followed by a brown line (rendering).

What would be really great and would solve my problem here is to trigger rendering only after the last websocket message. Since my update loop is self contained I think the simplest solution would be to give me a possibility to trigger the rendering myself.

Like so


def stream_point():
	for line in lines:

Is anything close to that available in bokeh? If not then does this interface make sense? I am happy to make a PR to bokeh if there are no design problems with it.

I do understand that usually all lines should be coming from the same ColumnDataSource and then all lines are updated at the same time but here the hard requirement is that each data source has a different length.

Thank you in advance!

The closest thing would be hold / unhold on curdoc(). I suppose worth a quick try, but I actually don’t think they would prevent multiple renders in this specific case.

I am happy to make a PR to bokeh if there are no design problems with it.

Better to start with a GitHub Issue to start discussion with more of the core team. There’s a few potential approaches I can imagine, but not all of them would be acceptable. We need to get agreement on scope and approach and also input from other folks that may have interest (e.g. the holoviz team)

Thanks for a reply!

I have created a Github issue right here