Hi,
I have some problems with plotting image with AjaxDataSource.
The first plot here shows the display obtained by save(fig). It works.
The second plot displays the index web page. As you can see the color_mapper is not apply on the first 3 pixels and the figure is not updated by the data from the ‘profile_data’ route.
The code:
from flask import Flask
from jinja2 import Template
import numpy as np
from bokeh.plotting import figure, save, output_file
from bokeh.embed import components
from bokeh.resources import INLINE
from bokeh.models import AjaxDataSource, LinearColorMapper, ColorBar
from bokeh.core.json_encoder import serialize_json
from matplotlib import cm
# hot color map from matplotlib
hot = [f'#{r[0]:02X}{r[1]:02X}{r[2]:02X}' for r in cm.hot(range(256), bytes=True)]
pixH, pixW = 3, 5
tMin, tMax = 100, 200
page = Template("""
<!DOCTYPE html>
<html lang="en">
<head>
<title>Ajax data source</title>
{{ resources|indent(4)|safe }}
{{ script|indent(4)|safe }}
</head>
<body>
{{ div|indent(4)|safe }}
</body>
</html>
""")
# Initialize the Flask application
app = Flask(__name__)
@app.route('/')
def index():
color_mapper = LinearColorMapper(palette=hot, low=tMin, high=tMax)
im = np.uint16(np.random.rand(pixH, pixW)*(tMax-tMin)+tMin)
source = AjaxDataSource(data_url="profile_data", polling_interval=3000)
source.data=dict(im=[im])
fig = figure(plot_width=400, plot_height=200)#, x_range=(0, pixW), y_range=(0, pixH))#, output_backend="webgl")#
fig.image(image='im', x=0, y=0, dw=pixW, dh=pixH, source=source, color_mapper=color_mapper)
color_bar = ColorBar(color_mapper=color_mapper, label_standoff=12, border_line_color=None, location=(0,0))
fig.add_layout(color_bar, 'right')
output_file('image.html', title='output file')
save(fig)
script, div = components(fig, INLINE)
return(page.render(resources=INLINE.render(), div=div, script=script))
@app.route('/profile_data', methods=['GET','POST'])
def Profile():
im = np.uint16(np.random.rand(pixH, pixW)*(tMax-tMin)+tMin)
return(serialize_json(dict(im=[im])))
if __name__ == '__main__':
app.run(port=80)
Thank you for your support.