【问题标题】:ValueError: with BokehValueError:散景
【发布时间】:2023-03-08 11:15:02
【问题描述】:

这里是重现错误的完整代码示例。我可以在以前版本的 Bokeh 中执行此操作,但升级此代码后不再允许我向 GridBox 添加 div。

from bokeh.models import Panel, Tabs
from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.io import curdoc
from bokeh.models.widgets import Div
from bokeh.layouts import gridplot


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)

p2 = figure(plot_width=300, plot_height=300)
p2.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="navy", alpha=0.5)

p3 = figure(plot_width=300, plot_height=300)
p3.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="navy", alpha=0.5)

p4 = figure(plot_width=300, plot_height=300)
p4.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="navy", alpha=0.5)

g1 = gridplot([p1, p2, p3, p4], ncols=2, plot_width=800, plot_height=600)

tab1 = Panel(child=g1, title="circle")

tabs = Tabs(tabs=[ tab1 ])

doc = curdoc()
doc.add_root(tabs)

my_div = Div(text="Please wait...")

doc._roots[0].tabs[0].child.children[1].children.append(my_div)

这是我收到的错误消息,

ValueError: 期望一个元素 列表(要么(元组(实例(LayoutDOM),Int,Int), Tuple(Instance(LayoutDOM), Int, Int, Int, Int))),得到无效的 seq 项目 [Div(id='2515', ...)]

【问题讨论】:

    标签: python bokeh


    【解决方案1】:

    先说几点:

    • 请、请、请始终提供所有相关信息。在这种情况下,您对网格的评论让我误以为只考虑gridplot API。如果您在异常中包含了行号/更多上下文,那么您的问题将在昨天得到回答。

    • 你绝对不应该到处乱搞doc._roots。它被标记为“私人”是有原因的,并且可能随时更改名称或被删除,恕不另行通知。 (这就是私有的意思,没有保证)。您引用了您创建的 Tabs 对象,您应该直接更新它。

    在 1.1 之前对整个布局系统进行了非常大的改造。修复了很多很多错误和问题。我们进行了数千次测试来维护版本兼容性,但这(显然)是发生的意外 API 损坏。用户直接与GridPlot 交流是不寻常的,所以这就是它被错过的原因。 GridPlotchildren 现在必须提供明确的网格坐标,如果您查看 GridPlot.children 的内容,您会看到:

    [(Figure(id='1852', ...), 0, 0),
     (Figure(id='1889', ...), 0, 1),
     (Figure(id='1926', ...), 1, 0),
     (Figure(id='1963', ...), 1, 1)]
    

    这是一个包含明确网格位置的元组列表。

    因此,具体而言,您可能想要:

    tabs.tabs[0].child.children[1].children.append((my_div, 2, 0))
    

    但是,我还要注意,您还可以将 div 附加到包含 GridPlot 的列中,这似乎也更容易做到。

    【讨论】:

    • 我的版本是1.1.0。我正在 Jupyter 笔记本中运行代码。
    • 我仍然收到此错误。 ValueError: 期望 List(Either(Tuple(Instance(LayoutDOM), Int, Int), Tuple(Instance(LayoutDOM), Int, Int, Int, Int))) 的元素,得到带有无效项的 seq [Div(id= '2140', ...)]
    • 我明天试试这个。
    猜你喜欢
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 2014-12-27
    • 1970-01-01
    相关资源
    最近更新 更多