question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Models must be owned by only a single document" error appears when splitting files

See original GitHub issue

When 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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
bryevdvcommented, Feb 17, 2019

@hklarner there is a new function for creating standard tile providers in the next release (and using the module attributes week be deprecated)

2reactions
bryevdvcommented, Sep 20, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bokeh - Models must be owned by only a single document ...
I have an interactive standalone .html with three tabs. This is a pseudo-code example as I have to simplify it to minimal in...
Read more >
Models must be owned by only a single document
The problem here is that your multiple calls to components produces multiple documents, and multiple documents cannot share a ColumnDataSource.
Read more >
Models must be owned by only a single document [error]
Trying to build a dashboard with multiple pages using Flask/Bokeh combination. The code that I use for main file ( test.py ) collects(imports)...
Read more >
Cloning tabs gives RuntimeError: Models must be owned by ...
Hello,. In my application I have tabs which are dynamically populated. The tabs must be dynamic, because of performance issues.
Read more >
Custom Editor API - Visual Studio Code
Custom editors have a single document model per resource but there may be multiple editor instances (views) of this document. For example, imagine...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found