Good day,
I have two figures with image plots that I am attempting to update with a periodic_callback on a bokeh.client session. I update the data_sources of the plots and verified that the data in the model is changing, however the changes are not rendered in the browser unless I refresh the page (or interestingly, if i open another tab and navigate to the same session URL, the image plots in both browser tabs are updated one time). Alternatively, if I generate a new image for each figure after updating the data sources, the plots in the browser are refreshed correctly at each interval, however I don’t believe this is proper or necessary.
Does anyone have any thoughts on what I am doing wrong? Below is the source code.
Regards,
Tom
from bokeh.client import push_session
from bokeh.models import ColumnDataSource, LinearColorMapper
from bokeh.plotting import figure, Document
from bokeh.layouts import row
import math
import random
mem_size = 1024 * 1024
disk_size = 1024 * 1024 * 8
mem_chunks = math.ceil(mem_size / 4096)
disk_chunks = math.ceil(disk_size / 4096)
scaling = 1
mem_ydim = int(math.ceil(math.sqrt(mem_chunks) / scaling))
mem_xdim = int(math.ceil(math.sqrt(mem_chunks) * scaling))
disk_ydim = int(math.ceil(math.sqrt(disk_chunks) / scaling))
disk_xdim = int(math.ceil(math.sqrt(disk_chunks) * scaling))
cmap = ['#bababa', '#404040', '#f4a582', '#ca0020']
mapper = LinearColorMapper(palette=cmap, low=1, high=4)
TOOLS = "wheel_zoom,save"
doc = Document()
a = [[1] * mem_xdim for i in range(mem_ydim)]
b = [[1] * disk_xdim for i in range(disk_ydim)]
p = figure(title="Memory Snapshot ({0}MB - {1} 4KB chunks)".format(mem_size / (1024*1024), mem_chunks),
x_range=(0,mem_xdim), y_range=(0,mem_ydim), tools=TOOLS, toolbar_location='above')
p.axis.major_tick_line_color = None
p.axis.axis_line_color = None
p.xaxis.ticker = []
p.yaxis.ticker = []
p2 = figure(title="Disk Snapshot ({0}MB - {1} 4KB chunks)".format(disk_size / (1024*1024), disk_chunks),
x_range=(0,disk_xdim), y_range=(0,disk_ydim), tools=TOOLS, toolbar_location='above')
p2.axis.major_tick_line_color = None
p2.axis.axis_line_color = None
p2.xaxis.ticker = []
p2.yaxis.ticker = []
session = push_session(doc, session_id='bokehtest')
def update():
x = random.randint(0, int(math.ceil(math.sqrt(mem_chunks) / scaling)))
y = random.randint(0, int(math.ceil(scaling * math.sqrt(mem_chunks))))
a[x - 1][y - 1] += 1
img.data_source.data.update({'columna': [a]})
#p.image(image='columna', x=0, y=0, dw=mem_xdim, dh=mem_ydim, color_mapper=mapper, source=source)
x = random.randint(0, int(math.ceil(math.sqrt(disk_chunks) / scaling)))
y = random.randint(0, int(math.ceil(scaling * math.sqrt(disk_chunks))))
b[x - 1][y - 1] += 1
source2.data = dict(columnb=[b])
#p2.image(image='columnb', x=0, y=0, dw=disk_xdim, dh=disk_ydim, color_mapper=mapper, source=source2)
doc.add_root(row(p, p2))
source = ColumnDataSource({'columna': [a]})
source2 = ColumnDataSource({'columnb': [b]})
img = p.image(name='img',image='columna', x=0, y=0, dw=mem_xdim, dh=mem_ydim, color_mapper=mapper,source=source)
img2 = p2.image(name='img2',image='columnb', x=0, y=0, dw=disk_xdim, dh=disk_ydim, color_mapper=mapper,source=source2)
doc.add_periodic_callback(update, 100)
session.show() # open the document in a browser
if __name__ == "__main__":
doc.validate()
session.loop_until_closed()