Hello there,
I’m new to bokeh and already very found of it, so before anything else: Thanks a lot for this great tool! Now back to business: It took me quite a while to find ways to successfully live update plot data, and now that I managed I have at least two option, where I don’t really know which one to prefer and what are the technical differences. In both ways I am using a thread to read out the data (which changes depending on user input through a slider), but before I say anything wrong, here is the code (methods in a class), method 1:
def __update1(self, update_interval):
Benchmarking average on 200 runs:
only self.session.store_document(self.document): 0.134s, with few values for y: 0.087s
while True:
# getting the new data
d2d.simu()
y = d2d.get_x()
for i in range(len(y)):
self.source.data['p'+str(i)] = y[i]
self.session.store_document(self.document)
time.sleep(update_interval)
``
And method 2:
def __update2(self,update_interval):
self.p is the figure
my_renderer = self.p.select(dict(type=GlyphRenderer))
data_src = my_renderer[0].data_source
Benchmark average on 200 runs: only cursession.store_objects: 0.091s, with few values for y: 0.046s
while True:
# getting the new data
d2d.simu()
y = d2d.get_x()
for i in range(len(y)):
data_src.data["p"+str(i)] = y[i]
cursession().store_objects(data_src)
time.sleep(update_interval)
``
I’ve included the short benchmark test, because it might be interesting to see that the second method is noticeably faster. Although both methods work, there a some differences in user experience:
Method 1 doesn’t allow to zoom/move the graph - yes, theoretically it works, but with every store_document call, the plot resets to match all the data - therefor the user can’t zoom in etc. This is of course not a wanted behavior, are there ways to stop it from resetting?
Method 2 doesn’t have this problem, but when I open the website (embedded in Flask) on a second computer, the data will be updated for both if one of the users changes the data. Well, that’s not too bad considering that the app crashes with two users on method 1, but still not a wanted behavior. But this is probably a topic for itself
I just found a third option to update the plot, with the AjaxDataSource from bokeh.models.sources, but I didn’t look to much into it yet - it seems a bit risky doing so much on the client side, but if you tell me that’s the only true way to do it, I’m happy to try it out.
Thank you very much and I hope you can enlighten me about the relevant differences in the methods and what would be the proper way to live update plot data.
Best regards
Clemens