Retrieve all renderers being used by ColumnDataSource (on python side)

Simplest MRE:


from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.io import curdoc # maybe i need this?

src = ColumnDataSource(data={'x':[1,2,3],'y':[1,2,3]})

f=figure()
r1 = f.scatter(x='x',y='y',source=src)
r2 = f.line(x='x',y='y',source=src)

Here I have 2 renderers being driven by the same CDS. I’m wondering if there’s a way to retrieve these renderers from the CDS on the python side. In my far more involved use case, the CDS (actually multiple CDS’s) are being held in a class instance, but I’m trying to keep the renderers “free” from my class and instead be up to the user. However if I want to add a filter… I need to instantiate a CDSView and add that view to all renderers that are being driven by each CDS.

So my hope is something like:

# where self already has a "source_dict" that contains a bunch of CDS models
def addFilter(self,filter_specs):
    fi = filter_specs #...make the filter based on args provided
    view = CDSView(filters=[fi])
    for src in source_dict.values():
        rends = ???? #... magically find all the renderers currently instantiated by user that are being driven by src
        for r in rends:
            rends.view=view

Thanks…

There’s not any back-references from data sources to renderers that use the sources, so there are not any magic options besides the standard two:

  • search all the models looking for renderers with the data source in question
  • manually keep track of renderers that add a given data source up front

You could also combine the two approaches, e.g. set a specific name property value when you add the renderer with the data source, then take advantage of Bokeh APIs like plot.select to search for every model with that name.

Thanks for reply.

  • search all the models looking for renderers with the data source in question

How do I do this? I know about Bokeh.index[Object.keys(Bokeh.index)[0]] on the JS side but not python side.

In recent versions, Document.models is a DocumentModelManager object that you can iterate over (e.g. for m in doc.models)