Amelio Vazquez-Reina
Amelio Vazquez-Reina

Reputation: 96264

Bokeh apps with dynamic updates of data sources

Consider the sliders_app.py example in Bokeh. I would like to have the ability to update the plot at regular time intervals. For example, say we want to time-shift the plot every 10 seconds. How can I do this in the context of an app?

To illustrate the goal, I would like to add the following extremely simple logic to the app that simply shifts X and Y cyclically.

Note that there is no easy way to insert this loop into the original sliders_app.py (where would it go?).

  while True:
    N = 200
    # Get the current slider values
    a = self.amplitude.value
    b = self.offset.value
    w = self.phase.value
    k = self.freq.value
    
    # Circularly time-shift X and Y 
    x = self.source.data["x"]
    x = np.roll(x,1)
    y = a*np.sin(k*x + w) + b 

    # Update the data container for the plot
    self.source.data = dict(x=x, y=y)

    # Sleep until the next update
    time.sleep(0.1)

Is there any way to do this in Bokeh? Does Bokeh perhaps have any timer widgets to which one could hook up a timer callback to update data sources?

If not, is there a plan to incorporate this functionality some time down the road?

Update

It looks like spectrogram.py uses threading to handle this type of updates. For anyone interested, this may be the way to pull it off.

Upvotes: 3

Views: 3145

Answers (1)

bigreddot
bigreddot

Reputation: 34568

There are a few options. The AjaxDataSource can cause the client to pull from a REST endpoint directly, on a periodic basis. Here is an example that shows its use:

https://github.com/bokeh/bokeh/blob/master/examples/plotting/file/ajax_source_realtime.py

Note that the spectrogram will probably be rewritten soon to use this, and reduce the amount of JS that is written by hand. (The spectrogram is fairly sophisticated and has some custom JS, we are always trying to reduce that amount over time)

Also it is worth mentioning that the threading in the spectrogram has to do with the server side of things, it does not really have anything to do with Bokeh, per se, or with getting updates into Bokeh.

If you are running an app in the Bokeh server, you can simply update the data source model however often you like, and the plot will respond.

Upvotes: 5

Related Questions