jupyter_bokeh icon indicating copy to clipboard operation
jupyter_bokeh copied to clipboard

Allow to share a `Document` between multiple `BokehModel`s

Open axil opened this issue 3 years ago • 4 comments

p1 = figure()
p2 = figure(x_range=p1.x_range)
ipw.VBox([BokehModel(p1), BokehModel(p2)])

generates

RuntimeError: Models must be owned by only a single document, DataRange1d(id='14198', ...) is already in a doc

Is there a fix or a workaround maybe?

axil avatar May 30 '22 10:05 axil

I've found a workaround:

BokehModel(column(p1,p2))

Is it the only possible way to solve it?

axil avatar May 30 '22 11:05 axil

Putting the plots together in a Bokeh layout was going to be my only suggestion. "Being in a document together" is the foundational definition that is essentially identical with "being able to share pieces together". But each BokehModel creates a new document for all the models it ends up containing. So, if you want to share pieces between different plots, those plots need to be configured on the same BokehModel, so that they are both in the same Document.

bryevdv avatar May 31 '22 16:05 bryevdv

I see. Maybe there's an alternative way of linking the plot ranges on the js level without sharing the Range object between the plots? Or it will hit the same limitation?

axil avatar Jun 02 '22 10:06 axil

Maybe there's an alternative way of linking the plot ranges on the js level without sharing the Range object between the plots?

You can always link manually, by setting up event listeners and updating related plots, but that defies the point of using bokeh (well, partially at least). This is a current limitation that each embedded model constructs its own document, and, by design, there is no sharing between models belonging to different documents. However, I don't see any technical reason not to lift this limitation. Before that happens, your options are either to link manually or to use bokeh's layouts and wrap a whole layout as a BokehModel (as you already found out).

mattpap avatar Jun 02 '22 10:06 mattpap