Adding constant text to legend read from ColumnDataSource

When creating a figure from a ColumnDataSource object, I have a ‘legend’ attribute to which I am trying to manually add some text:

p.multi_line(xs=xs, ys=ys, source=src, 
             legend = 'legend' + 'some text')

however Bokeh read this (the legend field) as (string + string) and not (columndatasource field + string to be added) which is what I need it to do.

Is there a way to explicitly access a CDS field? something like:

p.multi_line(xs=xs, ys=ys, source=src, 
             legend = src['legend'] + str['some text'])

hope question is clear / thanks

I don’t think there is a way to do this, if I am understanding your question correctly. At the low level, Bokeh legends can accept two kinds of things: value or field:

from import value, field

# Creates a single legend row, labeled "foo"
p.multi_line(..., legend=value("foo"))

# Does a "groupby" on a CDS column "foo" and creates multiple legend rows
p.multi_line(..., legend=field("foo"))

As a last observation, Bokeh does a little auto-detecting if you are not explicit. If you just pass legend="foo" then Bokeh will interpret that you wanted field("foo") if a column “foo” exists in the glyph’s data source. Otherwise it interprets as value("foo") if no such column is present.

If I understand your meaning, you want to somehow mix these two modes? That is not possible at present. I think you will need to construct a value(...) for every row you want in the legend up front, with exactly the text you want to see in each row (i.e. you will have to do any “groupby” yourself on the python side)

Solved by adding a ‘legend’ column with unique values on the python side .