How to get the subset of rows of a ColumnDataSource involved in a CDSView ?

Hi,

In my use case below, updating the group of the GroupFilter needs to update the FactorRange too.

Is there a way to get the subset of rows of the ColumnDataSource involved in the CDSView so that the right factors can easily be found ?

I created for the CustomJS the same subset in JavaScript as the filter but it could be a bother if the CDSView contains several filters.

from bokeh.io import output_file, show

from bokeh.models import (FactorRange,
                          CustomJS,
                          ColumnDataSource,
                          CDSView,
                          GroupFilter,
                          Select)
from bokeh.layouts import widgetbox, column
from bokeh.plotting import figure

output_file("CDSView_Select_FactorRange.html")

source = ColumnDataSource(dict(
    cat=['foo', 'bar', 'baz', 'qux', 'corge', 'grault', 'qux', 'corge', 'garply'],
    val=[5, 10, 1, 7, 8, 5, 3, 2, 4],
    grp=['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b']
    ))

p = figure(title="Test CDSView Select FactorRange", y_range=[], width=400, height=400)
group_filter = GroupFilter(column_name='grp', group='a')
view = CDSView(source=source, filters=[group_filter])

# How to get factors for the view ?
p.y_range.factors = ['foo', 'bar', 'baz', 'qux', 'corge']

p.hbar(y='cat', height=0.5, right='val',
       source=source, view=view)

code = """
    var grp = cb_obj.value;
    group_filter.group = grp;
   
    var factors = [];
    for (var i = 0; i < source.data['grp'].length; i++){
        if (source.data['grp'][i] == grp) {
            factors.push(source.data['cat'][i]);
        }
    }
    plot.y_range.factors = factors;
    source.change.emit();
"""
cb = CustomJS(args={'group_filter': group_filter,
                    'source': source,
                    'plot': p},
               code=code)
select = Select(title="Categ:", value="a", options=[("a", "Cat a"), ("b", "Cat b")], callback=cb)

layout = column(select, p)
show(layout)

Thanks