justus
justus

Reputation: 43

Why is the Bokeh DataTable object not updated in a periodic callback function?

Below I have the following python code, based on two Bokeh examples:

import numpy as np
from numpy import pi

from bokeh.client import push_session
from bokeh.driving import cosine
from bokeh.plotting import figure, curdoc

from bokeh.models import ColumnDataSource
from bokeh.models.widgets import TableColumn,  DataTable
from bokeh.io import show, vform

x = np.linspace(0, 4*pi, 80)
y = np.sin(x)

p = figure()
r1 = p.line([0, 4*pi], [-1, 1], color="firebrick")
r2 = p.line(x, y, color="navy", line_width=4)

source = ColumnDataSource(data=dict(x=x, y=y))
columns = [TableColumn(field="x", title="X"),TableColumn(field="y", title="Y")]
data_table = DataTable(source=source, columns=columns, width=400, height=280)


session = push_session(curdoc())

@cosine(w=0.03)
def update(step):
    r2.data_source.data["y"] = y * step
    r2.glyph.line_alpha = 1 - 0.8 * abs(step)


layout = vform(data_table)
show(layout)

curdoc().add_periodic_callback(update, 50)
session.show() # open the document in a browser
session.loop_until_closed() 

When you execute the code, you will see an oscillating sinus curve and a static data table. How do I have to modify the code, that the table is dynamically updated with the curve's changing function values? Thanks in advance!

Upvotes: 0

Views: 1102

Answers (1)

birdsarah
birdsarah

Reputation: 1165

In your update code you are only updating the datasource of the line, you need to either:

  1. Update both data sources in your update function
  2. Make your datatable and line share a source

The second is probably more efficient but will depend on your needs.

It would look something like this:

p = figure()

source = ColumnDataSource(data=dict(x=x, y=y))

r1 = p.line([0, 4*pi], [-1, 1], color="firebrick")
# This is the major change - using the same source here as for data_table
r2 = p.line(x='x', y='y', color="navy", line_width=4, source=source)

columns = [TableColumn(field="x", title="X"),TableColumn(field="y", title="Y")]
data_table = DataTable(source=source, columns=columns, width=400, height=280)

I've verified that this causes both to be updated - although I'm not sure it's what you want

Upvotes: 1

Related Questions