"Models must be owned by only a single document" error appears when splitting files
See original GitHub issueWhen writing a bokeh app with multiple Panels, it seems to matter whether the same code is put into one python file or split across multiple files. I’m not sure whether this is intended behavior, but at the very least it is very unexpected for a bokeh newcomer like me.
As a minimum working example, let’s consider a simplification of the tabs example with only a single tab (you can also clone the code on github).
# panels.py
from bokeh.models.widgets import Panel, Tabs
from bokeh.io import output_file, show, curdoc
from bokeh.layouts import layout
from bokeh.plotting import figure
output_file("slider.html")
p1 = figure(plot_width=300, plot_height=300)
p1.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="navy", alpha=0.5)
tab1 = Panel(child=p1, title="line")
tabs = Tabs(tabs=[ tab1 ])
l = layout(tabs)
curdoc().add_root(l)
bokeh serve panels.py --show
works fine; reloading the page also works fine.
Now, let’s use essentially the same code, but split it into two separate files:
# panels.py
from bokeh.models.widgets import Tabs
from bokeh.io import output_file, show, curdoc
from bokeh.layouts import layout
from bokeh.plotting import figure
output_file("slider.html")
from panel1 import tab1
tabs = Tabs(tabs=[ tab1 ])
l = layout(tabs)
curdoc().add_root(l)
and
# panel1.py
from bokeh.plotting import figure
from bokeh.models.widgets import Panel
p1 = figure(plot_width=300, plot_height=300)
p1.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="navy", alpha=0.5)
tab1 = Panel(child=p1, title="line")
bokeh serve panels.py --show
still works fine, but reloading the page gives the following error:
Error running application handler <bokeh.application.handlers.script.ScriptHandler object at 0x10eac2c50>: Models must be owned by only a single document, Grid(id='db7c1991-fde0-4287-abf3-8488b03517a6', ...) is already in a doc
It seems, now that tab1
lives in panel1.py
, some “bokeh magic” stopped working.
Edit: panel1.py updated (was missing tab1 line)
P.S. Running bokeh 0.13.0 on MacOSX 10.13.6, and using Chrome 68.0
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:7 (4 by maintainers)
Top GitHub Comments
@hklarner there is a new function for creating standard tile providers in the next release (and using the module attributes week be deprecated)
App code is executed new to generate a document for every session that connects. Every session must get its own unique set of Boken objects, so the app code always must generate new instances (this is to ensure that different users are completely isolated). However, Python caches module imports, and we cannot do anything about that. The implication is that Bokeh objects cannot be module global variables.
If you want to split up the app code into multiple modules, that’s fine, but you will need to encapsulate all Bokeh object creation inside functions that always return new instance, rather than creating any as module globals.
As an aside, in the future, please refer usage/support questions such as this to the mailing list first.