I have the following code in Bokeh. It displays four dots in red. The user can select another color from the dropdown, click ‘Run’ and the dots are to change color. With this code, the dots do not change color nor does the image_1.png get updated. Any advise on how I could refresh image_1.png and display it correctly? Thanks indeed!
from bokeh.layouts import column, row
from bokeh.models import (Select, Button)
from bokeh.plotting import curdoc, figure
from bokeh.layouts import column, layout
import numpy as np
import matplotlib.pyplot as plt
x_range=(0,100)
y_range=(0,100)
color_1 = ['red','blue','yellow']
N = 4
x = np.random.random(size=N) * 10
y = np.random.random(size=N) * 10
radii = 15#np.random.random(size=N) * 15
line_color = Select(value='red',
title='color:',
width=200,
options=color_1)
p = figure(tools="reset,pan,wheel_zoom,lasso_select")
file_name_1 = "test_sp/static/image_1.png"
def button_callback():
print('bc')
p = figure(tools="reset,pan,wheel_zoom,lasso_select")
layout.children[1] = create_figure()
return p
def create_figure():
c1 = line_color.value
print(c1)
file_name_1 = "test_sp/static/image_1.png"
plt.scatter(x,y,radii,color=c1,alpha=0.6)
plt.savefig(file_name_1)
p = figure(tools="reset,pan,wheel_zoom,lasso_select")
p.image_url(url=[file_name_1],x=x_range[0],y=y_range[1],w=x_range[1]-x_range[0],h=y_range[1]-y_range[0])
return p
button = Button(label='Run', width=100, button_type="success")
button.on_click(button_callback)
selects = column(line_color,button, width=420)
layout=row(selects, create_figure())
curdoc().add_root(layout)
curdoc().title = "color the dots"
The code I posted here is the complete minimal example of a much larger code where I update the Bokeh figure (based on user inputs from the front-end, save it and then refresh the front-end). This is why I have a ‘run’ button in the front-end (to collect the user inputs) and a create figure() in the Bokeh code to allow me to construct a Bokeh figure (which is a .png saved from a matplotlib plot). I still need the ‘run’ button and create_figure(). My issue is that even though the .png gets updated on the server side, I am unable to refresh the front end with this updated .png. Any advise on how to refresh the Bokeh figure…?
(I have also posted this question on stackoverflow: button - How to refresh figure in Bokeh? - Stack Overflow)