How to use `bokeh.models.expressions`?

How to use expression to calculate a column value from another of a ColumnDataSource?

In the docs, given syntax is -{'expr': some_expression}, ...)

What is some_expression here ? Is it a mathematical expression ?

Can someone give an practical example/ implementation for bokeh.models.expressions ?

Expression subclasses represent computations that happen in a browser, with JavaScript. There are currently only two pre-defined expressions:

  • Stack which can stack (add) values across CDS columns (e.g. for stacked bar charts)
  • CumSum which computes a cumulative sum of a CDS column

In order to use anything other than these, you would currently have to write a custom extension, including a JavaScript implementation, which you could then use like any of the other existing expressions. As for what those implementations look like, you can check out the implementation of Stack in stack.ts

However, none of this is really especially useful or interesting to users, for the most part. If you want to apply a (JavaScript) transformation to a CDS column, then you can use CustomJSTransform[1] which allows you to specify a snippet of JS to do the transformation, rather than having to create an entire custom extensions.

  1. The difference between a “transform” and an “expression” is pretty mundane: Expressions are a limited kind of transform, that only have “vectorized” operations. However, only CustomJSTransform exists for user-facing use, and it’s probably always what you’d want. ↩︎