22

I'm working with Bokeh 0.12.2 in a Jupyter notebook and it frequently throws exceptions about "Models must be owned by only a single document":

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-23-f50ac7abda5e> in <module>()
      2     ea.legend.label_text_font_size = '10pt'
      3 
----> 4 show(column([co2, co, nox, o3]))

C:\Users\pokeeffe\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\io.py in show(obj, browser, new, notebook_handle)
    308     '''
    309     if obj not in _state.document.roots:
--> 310         _state.document.add_root(obj)
    311     return _show_with_state(obj, _state, browser, new, notebook_handle=notebook_handle)
    312 

C:\Users\pokeeffe\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\document.py in add_root(self, model)
    443             self._roots.append(model)
    444         finally:
--> 445             self._pop_all_models_freeze()
    446         self._trigger_on_change(RootAddedEvent(self, model))
    447 

C:\Users\pokeeffe\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\document.py in _pop_all_models_freeze(self)
    343         self._all_models_freeze_count -= 1
    344         if self._all_models_freeze_count == 0:
--> 345             self._recompute_all_models()
    346 
    347     def _invalidate_all_models(self):

C:\Users\pokeeffe\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\document.py in _recompute_all_models(self)
    367             d._detach_document()
    368         for a in to_attach:
--> 369             a._attach_document(self)
    370         self._all_models = recomputed
    371         self._all_models_by_name = recomputed_by_name

C:\Users\pokeeffe\AppData\Local\Continuum\Anaconda3\lib\site-packages\bokeh\model.py in _attach_document(self, doc)
     89         '''This should only be called by the Document implementation to set the document field'''
     90         if self._document is not None and self._document is not doc:
---> 91             raise RuntimeError("Models must be owned by only a single document, %r is already in a doc" % (self))
     92         doc.theme.apply_to_model(self)
     93         self._document = doc

RuntimeError: Models must be owned by only a single document, <bokeh.models.tickers.DaysTicker object at 0x00000000042540B8> is already in a doc

The trigger is always calling show(...) (although never the first time after kernel start-up, only subsequent calls).

Based on the docs, I thought reset_output() would return my notebook to an operable state but the exception persists. Through trial-and-error, I've determined it's necessary to also re-define everything being passing to show(). That makes interactive work cumbersome and error-prone.

[Ref]:

reset_output(state=None)

  Clear the default state of all output modes.

  Returns: None


  • Am I right about reset_output() -- is it supposed to resolve the situation causing this exception?

  • Else, how do I avoid this kind of exception?

patricktokeeffe
  • 1,058
  • 1
  • 11
  • 21
  • My first suggestion is to upgrade. Bokeh and the Notebook are both very large, cross-runtime things, and integrating them is a constant challenge for improvement. I know there has been work since `0.12.2` though I don't have time to research the details .Otherwise, please post a complete code sample to inspect/try out. For instance, I know that showing the same plot multiple times works fine (I just tried it to be sure) so it's not really possible to speculate what your specific problem is without a test notebook to reproduce the issue. – bigreddot May 29 '17 at 13:46

7 Answers7

6

It may be because of conflicting objects that has the same name. you need to create completely new objects every time.

Reza
  • 728
  • 1
  • 10
  • 28
5

I've been working in a jupyterlab notebook iterating on visualizations of a large amount of data with bokeh, holoviews, and panel, and have been running into this issue periodically.

Here are a couple of additional things that may help. Note that p is used as the bokeh conventional name for the figure. I am posting on this old thread because it was the top result in my Google search for the error message.

Try clearing the document (found in docs):

from bokeh.io import curdoc

curdoc().clear()

I observed that panel was able to display a bokeh object even when bokeh show would not.

import panel as pn

pn.extension()

pn.pane.Bokeh(p)

Digging into how panel is able to display an object even when bokeh is not, I noticed this function, which fixed the problem for me:

import panel as pn

pn.io.model.remove_root(p)

If you don't have panel installed, here is the source code from above:

from bokeh.models import Model

for model in p.select({'type': Model}):
    prev_doc = model.document
    model._document = None
    if prev_doc:
        prev_doc.remove_root(model)

Hopefully this helps someone, or future me.

Brian Larsen
  • 612
  • 8
  • 9
4

Seems it can by fixed by differentiating the source name Like this:

source1 = df
p1.circle('A', 'B', source=source1)

source2 = df
p2 = figure()
p2.circle('C', 'D', source=source2)

sourceN = df
p2 = figure()
p2.circle('X', 'Y', source=sourceN)
Scorpioooooon21
  • 491
  • 5
  • 17
4

This solution works without upgrading or downgrading packages.

try:
    reset_output()
    output_notebook()
    show(p)
except:
    output_notebook()
    show(p)

Solution provided here :

https://github.com/bokeh/bokeh/issues/8579

Snehal Nair
  • 181
  • 1
  • 6
2

I ran into this error message when using file_html from from bokeh.embed, after i upgraded to bokeh version 1.01. Downgrading again to bokeh version 0.12.16 solved it. (pip install bokeh==0.12.16) Note sure why.

Joris
  • 1,158
  • 1
  • 16
  • 25
0

Try using after creating each plot. add_root will add that model as a root of the current Document, making sure that each Model is added to a single Document:

    curdoc().add_root(column([plot]))
    curdoc().title = doc_title //Add a title to the doc
    show(figure)

Note: column(the list of plots) can be replaced with any object which inherits the Model class.

Refer to the link for more details on add_root and bokeh Documents: https://docs.bokeh.org/en/latest/docs/reference/document.html?highlight=add_root#bokeh.document.document.Document.add_root

bono1567
  • 1
  • 1
  • 1
    Welcome to StackOverFlow, can you please add more details on how your answer achieves what the question requires and how it compares to the other proposed questions? – Mike Elahi Oct 26 '20 at 15:10
0
column_data_source = ColumnDataSource(dataframe)

After each row in the jupyter notebook that used the column data source we have to make it again simply. The column data source seems like we cant use it many times in the same code.

This seems a little hack but it worked for me when i faced the same error.

Jainil Patel
  • 1,284
  • 7
  • 16