Formatting Text Glyph

I have a ColumnDataSource (source) with three columns and create some text glyphs:

x y score
1 1 12.123456
2 2 99.199999

p.text(x='x', y='y', text='score')

Is there an elegant way to format the glyph (ie, 1 decimal for the score)?
Checking the documentation doesn’t seem to point anything obvious: figure — Bokeh 3.2.0 Documentation

One workaround is to make an extra column in source to store the formatted score and referencing that:

x y score     formatted_score
1 1 12.123456 12.1
2 2 99.199999 99.2

p.text(x='x', y='y', text='formatted_score')

But I was wondering if anyone had any suggestions for doing this more elegantly?

Text glyphs don’t currently have any direct integration with [tick] formatters, but it seems like a reasonable thing to possibly consider. Presently you could accomplish this with a CustomJSTransform.

Isn’t this working?

p.text(x="x", y="y", text=f"({p.x}, {p.y}): {p.score:.1f}")

@CatChenal Something like that can work for a single value, as the OP states, they have lots of values, stored in a column of a ColumnDataSource, and wanted to know if they could all be formatted dynamically on the client.

Unfortunately this doesn’t work with a ColumnDataSource.
As of now the only solutions I can think of are:

  1. Creating a new formatted column as stated in my first post.
    Inelegant and memory scaling is not ideal. What if I have N different types of text glyphs, each with a different format? That means I need to make N new columns…

  2. CustomJSTransform as Bryan stated
    A bit clunky, but by far the “best” workaround.

  3. Creating each text glyph individually
    This would be using your solution, but it would completely ignore the ColumnDataSource dynamism and I would lose a lot of functionality.
    An example of this in pseudo-code would be:

for every row in ColumnDataSource:
     p.text(x=row.x, y=row.y, text=f"{row.score:.1f}")

Just fyi, I am using option 1 for now as I don’t need the unformatted score.
But if I did have a requirement to keep the unformatted score, or for more general solutions I would be using Bryan’s suggestion.

Just an FYI that option 3 is not advisable in general. Bokeh is specifically designed to efficiently accommodate “vectorization” via data sources. Glyphs objects, themselves, are quite expensive.

More concretely: If you add one glyph with a 500 element CDS, Bokeh will not break a sweat. If you add 500 individual glyph objects, your plot will be significantly larger and probably unusably slow.