Update.on_change legend_label in CDSView

I have a bokeh server running well where a country is Selected and this updates a CDSView by BooleanFilter to a region (list of countries).

I’ve tried with GroupFilter too, but cannot pick legend_labels from the CDSView filters.

Extract:

source1 = ColumnDataSource(dfall)
bools = [True if c == ‘CH’ else False for c in source1.data[‘geoId’]]
view3 = CDSView(source=source1, filters=[ #GroupFilter(column_name=‘geoId’, group=‘CH’),
BooleanFilter(bools)
])
color = [‘red’, ‘green’, ‘blue’, ‘black’, ‘brown’, ‘teal’, ‘olive’, ‘orange’, ‘pink’, ‘plum’, ‘yellow’, ‘indigo’]
plot1 = figure(plot_height=300, x_axis_type=‘linear’, x_range = (-1, 50),
y_axis_label=‘cases / deaths’, y_range = (0, 1.1))
plot1.step(x=‘id’, y=‘dfm_cmax’, source=source1, view=view3, legend_label = ‘geoId’)

Is this legend_label option possible or must I revert to a manual label technique?

HI @awsbarker it’s not really clear to me from that description what exactly it is that you are trying to accomplish. Please edit your post to do the following:

  • explain a bit more thoroughly in words what you are trying to do
  • add a small, complete script that highlights the question so that we can actually try running ourselves
  • format the code using the </> button in the edit toolbar or enclosing code blocks in ``` fences

Thanks Bryan, sorry I tried to rush out a quick overview. Here is a more detailed description with example:

When using bokeh server I take a CDSView using BooleanFilters to collect a list of countries that are in the same geographic region as a Selected country using Select widget. The purpose is to compare Covid19 by region.

This all works in below code but I need to be able to show some sort of legend identifying the different countries and of course the change in countries when a new country is selected.

dataframe normalise cases again max in each country group

dfall['dfm_cmax'] = dfall.cmax / gbcm.transform('max')
source1 = ColumnDataSource(dfall)
bools = [True if c == 'CH' else False for c in source1.data['geoId']]
view3 = CDSView(source=source1, filters=[#GroupFilter(column_name='geoId', group='CH'),
                                         BooleanFilter(bools)])
plot1 = figure(plot_height=300, x_axis_type='linear', x_range = (-1, 50), y_axis_label='cases / deaths', y_range = (0, 1.1))
r = plot1.line(x='id', y='dfm_cmax', source=source1, view=view3)   #, legend_group='geoId')#,  color = Category20)
#legend = Legend(items=[LegendItem(label="orange", renderers=[r], index=0)])

pays = Select(title="Country of residence", value="Switzerland", options = sorted(df_pays['Name'].to_list()))
reg = df_pays[df_pays['Name'] == pays.value].Region.iloc[0]
xx = df_pays[df_pays['Name'] == pays.value].Code.iloc[0]

def update_data(attr, old, new):
    print(f'attr {attr} {old} {new}')
    # Get the current slider values
    reg = df_pays[df_pays['Name'] == pays.value].SRegion.iloc[0]
    pinreg = df_pays.Code[df_pays['SRegion'] == reg].to_list() # country list in region
    print(f"view1 update {pays.value} subregion is {pinreg}")
    xx = df_pays[df_pays['Name'] == pays.value].Code.iloc[0]

    b = [True if c in pinreg else False for c in source1.data['geoId']]
    view3.filters[0] = BooleanFilter(b)

pays.on_change('value', update_data)

layout1 = column(pays, plot1) #, name='g1')
pan1 = Panel(child=layout1, title = 'Top Mortality Rates vs C19')
tabs = Tabs(tabs = [pan1], name = 'g1')

curdoc().add_root(tabs)
curdoc().title = "C19 Risks"

This code works as expected and produces a single figure showing line glyphs (with warning CDSVIEW filters with connected E-1024) for each country in the selected country/region.

I’d like to add a legend or label to represent each line (or step, or circle to avoid E1024 error) and eventually colour them.

I’ve left comments in the code to show where I’ve tried legend_group when filtering with GroupFilter, and also tried to build a LegendItem loop but these I cannot debug to resolve.

So, the question is, is there a way within CDSView using booleanfilters to create labels or legends each time a Selected.on_change update is made to respresent a column that somehow represents the booleanfilter?

Variable “pinreg” in the code produce a list of countries [‘CH’, ‘AT’, ‘BE’…] by ISO code this would be my of label or legend and I suppose if I cannot make this happen automatically I could annotate some text placing list items.

Hope this is clearer.

PS : its live now at https://awsb.ddns.net/c19/c19

Bryan, to answer my own question and for your reference I found on github bokeh issue #9878 to be same as mine.

Conclusion : legend_group doesn’t respect CDSView filter.

Looks like I’ll have to try some workarounds.