Search code examples
pythonbokehbokehjs

Can I attach a plain DataSource to a document on Bokeh?


I have a Bokeh server running and I am embeding it as a Bokeh document in my application, this allows me to change a glyph data source from a component in my front end (while having the sockets connection) by accessing it like:

let mySource = Bokeh.documents[0].get_model_by_name('mySource');
...
(alter the data)
...
mySource.change.emit();

However my problem is that this source needs to be the source of a glyph, for it to be accessible in Bokeh.documents[0] through my front end components. But I want this datasource just to be an empty list, and add "ids" through the front end, and then in python get the data for those IDs which is going to be the bokeh glyph source.

The problem, I can not find a way to attach a plain datasource into the bokeh root. I only can get a datasource in there as part of a glyph (But I do not need a glyph for a list of ids).

Does anyone has some advice regarding this?


Solution

  • You can add a ColumnDataSource to a glyph or to a data table and hide it using visible attribute. See example with DataTable and empty ColumnDataSource below (works for Bokeh v2.1.1):

    from bokeh.io import curdoc
    from bokeh.models import ColumnDataSource, DataTable, TableColumn
    
    columns=[TableColumn(field=str(i),title=str(i),width=200,name=str(i)) for i in range(2)]
    source = ColumnDataSource(data={}, name='mySource')
    table=DataTable(source=source,columns=columns,name='myTable',visible=False)
    curdoc().add_root(table)