Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Models must be owned by only a single document' issue #167

Open
axil opened this issue Aug 23, 2022 · 2 comments
Open

'Models must be owned by only a single document' issue #167

axil opened this issue Aug 23, 2022 · 2 comments

Comments

@axil
Copy link
Contributor

axil commented Aug 23, 2022

The following code that intermixes two BokehModel instances with an ipywidget instance

import bokeh.plotting as bp
import bokeh.models as bm
import ipywidgets as ipw
from jupyter_bokeh import BokehModel
p = bp.figure()
src = bm.ColumnDataSource(data={'x': [1,2], 'y': [3,4]}) 
p.line(source=src)
radio = bm.RadioGroup(labels=['a', 'b'], active=0)
label = ipw.Label('c')
callback = bm.CustomJS(args=dict(data_src=src))
radio.js_on_change('active', callback)
ipw.VBox([
    BokehModel(p),
    BokehModel(radio), 
    label,
])

generates the following error:

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

Supposedly the problem is with jupyter_bokeh because

import bokeh.layouts as bl
bp.show(bl.column(p, radio))

works fine.

It is similar to #154, and I understand that

ipw.VBox([
    BokehModel(bl.column(p, radio)),
    label,
])

is a workaround, but this trick is not always possible, for example, it is not possible for the following layout:

ipw.VBox([
    BokehModel(p),
    ipw.HBox([
          BokehModel(radio), 
          label,
    ]),
])

and the main difference from issue 154 is that I don't understand which model is being 'shared' here.

@mattpap
Copy link
Contributor

mattpap commented Aug 23, 2022

There is a potential workaround for the "single document" error:

doc = Document()
doc.add_root(p)
doc.add_root(radio)
ipw.VBox([
    BokehModel(p),
    ipw.HBox([
          BokehModel(radio), 
          label,
    ]),
])

This way p and radio will share a document and BokehModel will not attempt to attach a new one under such setup. However, that only solves part of the problem. The other issue is that each BokehModel(...) is an independent embedding point, which is currently undefined behavior when using the same document. Consider each BokehModel(...) as if calling show() multiple times with the same document. However, I have some ideas how this could be alleviated.

@axil
Copy link
Contributor Author

axil commented Aug 24, 2022

@mattpap Yes, it helps with the ownership issue. Yet this code displays both widgets twice and generates the following error

image

for every model (a total of 8 times). Do you know how to deal with it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants