I’m trying to update widgets based on a change that occurs on a ColumnDataSource
object. The idea is to be able to update only one object (the source object) and instantly see the changes in the widgets without having to explicitly update their properties. Is this possible in any form?
Here’s what I’ve tried:
from bokeh.models import Select
from bokeh.models.layouts import Column
from bokeh.models.sources import ColumnDataSource
from bokeh.io import curdoc
import pandas as pd
df = pd.DataFrame({
'samples': ['1', '2', '3', '1'],
'labels': ['label1', 'label2', 'label1', 'label2']}
)
init_sample = '1'
source = ColumnDataSource(data=df.query('samples == @init_sample').to_dict('list'))
def update_data(attr, old, new):
source.update(data=df.query('samples == @new').to_dict('list'))
unique_samples = df.samples.unique().tolist()
samples = Select(title="select", value=unique_samples[0], options=unique_samples)
labels = Select(
title="labels",
value=list(set(source.data['labels']))[0],
options=list(set(source.data['labels']))
)
samples.on_change('value', update_data)
curdoc().add_root(Column(samples, labels))