bokeh / jupyter_bokeh

An extension for rendering Bokeh content in JupyterLab notebooks
BSD 3-Clause "New" or "Revised" License
253 stars 48 forks source link

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

Open axil opened 2 years ago

axil commented 2 years ago
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 commented 2 years ago

I've found a workaround:

BokehModel(column(p1,p2))

Is it the only possible way to solve it?

bryevdv commented 2 years ago

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.

axil commented 2 years ago

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?

mattpap commented 2 years ago

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).