容器作为工具提示不显示内容

时间:2019-06-18 19:22:45

标签: python ipywidgets bqplot

我正在创建散点图,并希望在工具提示中显示一些信息。

以下内容可以正常工作:

import bqplot as bqp
import ipywidgets as ipw

xSc = bqp.LinearScale()
ySc = bqp.LinearScale()

tt = ipw.Label("A")
def hover_handler(self, content):
    tt.value = str(content)

s = bqp.Scatter(x=[0, 1, 2], y=[1, 2, 3], scales=dict(x=xSc, y=ySc),
                tooltip=tt)
s.on_hover(hover_handler)
bqp.Figure(marks=[s])

(没有轴,可以使代码简短)

将鼠标悬停在每个点上可以显示其content很好。

enter image description here

但是,我不想简单地显示原始内容。相反,我想以表格形式显示它(但是默认的bqp.Tooltip不足以满足我的需求)。

但是,如果我将标签包裹在ipw.VBox中,则工具提示会变成一个很小的垂直条。添加min_widthmin_height会增加工具提示的大小,但是没有内容(即使tt是使用默认值创建的)。如果我单独调用以单独显示VBox,则该版本会正常显示(即使未定义布局),甚至在将鼠标移到这些点上时也会更新。

import bqplot as bqp
import ipywidgets as ipw
from IPython.display import display

xSc = bqp.LinearScale()
ySc = bqp.LinearScale()

tt = ipw.Label("A")
vb = ipw.VBox(children=[tt], layout=ipw.Layout(min_width='100px', min_height='100px'))
display(vb)
def hover_handler(self, content):
    tt.value = str(content)

s = bqp.Scatter(x=[0, 1, 2], y=[1, 2, 3], scales=dict(x=xSc, y=ySc),
                tooltip=vb)
s.on_hover(hover_handler)
bqp.Figure(marks=[s])

enter image description here

我需要怎么做才能正确显示工具提示?

1 个答案:

答案 0 :(得分:0)

我认为,针对您要实现的目标,最好使用Output小部件作为工具提示,然后创建所需的小部件并使用输出小部件作为上下文管理器来显示它们。

如果您想查看可以显示哪些其他信息,请尝试在content函数中打印hover_handler

    import bqplot as bqp
    import ipywidgets as ipw
    from IPython.display import display, clear_output

    xSc = bqp.LinearScale()
    ySc = bqp.LinearScale()


    out = ipw.Output()

    def hover_handler(self, content):
        out.clear_output()
        with out:
            label = ipw.Label(content['data']['name'])
            display(label)

    s = bqp.Scatter(x=[0, 1, 2], y=[1, 2, 3], scales=dict(x=xSc, y=ySc),
                    tooltip=out)
    s.on_hover(hover_handler)
    bqp.Figure(marks=[s])

要显示信息表,您可能需要一个ipy.HTML小部件,可用于传递HTML表。有时候,我会通过df.to_html()方法使用熊猫。

相关问题