I’ve built a heatmap and it’s all working nicely except the colors aren’t updating from a select widget’s js_on_change
as I expect it to. Everything renders, looks great, but nothing changes with the widget. Maybe there’s something I should be specifying in LinearColorMapper? Could someone please help me understand what I might be doing wrong?
Here’s a simplified example that replicates the issue:
from pandas import *
from bokeh.io import show
from bokeh.models import LinearColorMapper, ColumnDataSource, Select, CustomJS, CDSView
from bokeh.layouts import column, layout
from bokeh.plotting import figure
from bokeh.palettes import Viridis256
df = DataFrame({'attribute': ['Y', 'Y', 'Y', 'Y', 'Z', 'Z', 'Z', 'Z']
, 'period': [1, 2, 3, 4, 1, 2, 3, 4]
, 'dimension_1': [1, 37, 44, 13, 41, 51, 18, 14]
, 'dimension_2': [10, 3, 44, 53, 20, 9, 18, 14]
, 'dimension_3': [80, 37, 22, 13, 13, 44, 18, 14]})
df['period'] = df['period'].astype(str)
periods = df.period.unique().tolist()
attributes = df.attribute.unique().tolist()
df['active_dimension'] = df['dimension_1']
source = ColumnDataSource(data=df)
view =CDSView(source=source)
values_select = Select(title="Values:", options=["dimension_1", "dimension_2", 'dimension_3'])
values_select.js_on_change('value', CustomJS(args=dict(source=source, values_select=values_select), code="""
source.df['active_dimension'] = source.df[values_select.value]
source.change.emit();
p.reset.emit()
"""))
color_mapper = LinearColorMapper(palette=Viridis256, low=df.active_dimension.min(),
high=df.active_dimension.max())
p = figure(x_range=periods, y_range=attributes)
p.rect(x="period", y="attribute", width=1, height=1, line_color=None, source=source,
view=view,
fill_color={'field': 'active_dimension', 'transform': color_mapper})
l = layout(column([values_select, p]))
show(l)