通过 Select Widget 在 Jupyter Notebook 上进行交互式选择引发“模型必须仅由单个文档拥有"错误 [英] Interactive Selection on Jupyter Notebook through Select Widget raise "Models must be owned by only a single document" error

查看:23
本文介绍了通过 Select Widget 在 Jupyter Notebook 上进行交互式选择引发“模型必须仅由单个文档拥有"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过

我使用的是散景 1.0.4

解决方案

我们可能需要改进一些文档,因为您的代码中有几个部分没有意义.

  • push_notebook 是对显示真实 Bokeh 服务器应用程序的补充(即,将 modify_doc 传递给 show).我想不出任何情况下将它们一起使用是有意义的.Bokeh 服务器应用程序功能是 push_notebook 的严格超集,因此,由于您已经在制作 Bokeh 服务器应用程序,因此您应该以标准方式更新所有内容.

  • show 永远不应该在 Bokeh 服务器应用程序代码中被调用inside(在这种情况下,在 modify_doc 内) 这实际上是您得到的异常的近因.你应该

  • 你不应该每次都制作新的情节!push_notebook 和 Bokeh 服务器的全部目的是更新可以有效地每次都制作一个全新的情节.您应该更新现有绘图的数据和属性.

这里有一个笔记本中 Bokeh 服务器应用程序的完整示例:

https://github.com/bokeh/bokeh/blob/master/examples/howto/server_embed/notebook_embed.ipynb

您应该学习并效仿它,因为它代表了最佳实践.

How can I create an interactive bar plot with bokeh through a selection widget on Jupyter Notebook?

Why the Notebook raise the following error:

"RuntimeError: Models must be owned by only a single document, Title(id='1044', ...) is already in a doc ERROR:tornado.access:500 GET /autoload.js?bokeh-autoload-element=1002&bokeh-absolute-url=http://localhost:54277&resources=none (::1) 117.01ms"

I read carefully this example from github and this similar situation from a Google Bokeh Group, in the latter they run a bokeh server not a the jupyter kernel

output_notebook()

dct={'Date' : ["2018-01-07", "2018-01-12", "2018-01-13", "2018-01-14", "2018-01-20", "2018-01-24"],'Activity' : ['A','B','A','B','A','B'],'Count' : [1, 2, 5, 3, 7, 1]}
df=pd.DataFrame(dct)

activity_list=df['Activity'].unique().tolist().copy()
activity_selected='A'
def modify_doc(doc):
     def make_plot(cdf):
         plot = figure()
         plot.vbar(x=cdf.Date, top=cdf.Count, width=0.9)
         push_notebook()
         show(plot, notebook_handle = True)
         return plot
     def update_plot(attr, old, new):
         activity =  select.value
         sdf = df.copy()
         sdf = sdf[sdf['Activity'] == activity]
         layout.children[0] = make_plot(sdf)


     select = Select(title='Select Activity', value=activity_selected, options=activity_list)
     select.on_change('value', update_plot)
     p=make_plot(df)
     layout=column(select, p)
     doc.add_root(layout)
show(modify_doc)

What I expect is something like this in the snapshoot:

I am using Bokeh 1.0.4

解决方案

It's possible we need to make some documentation improvements, because there are several parts of your code that do not make sense.

  • push_notebook is complementary to showing a real Bokeh server app (i.e. passing modify_doc to show). I can't think of any situation where it would make sense to use them together. The Bokeh server app capability is a strict superset of push_notebook, so since you are already making a Bokeh server app, you should just update everything in the standard way.

  • show should never be called inside the Bokeh server app code (in this case, inside modify_doc) This is in fact the proximate cause of the exception you are getting. You should

  • You should not make a new plot every time! The entire purpose of both push_notebook and the Bokeh server is that updates can happen efficiently without making a whole new plot every time. You should be updating the data and attributes of the existing plot.

There is a complete wokring example of a Bokeh server app in a notebook here:

https://github.com/bokeh/bokeh/blob/master/examples/howto/server_embed/notebook_embed.ipynb

You should study and emulate that as it represents best practice.

这篇关于通过 Select Widget 在 Jupyter Notebook 上进行交互式选择引发“模型必须仅由单个文档拥有"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆