You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bokeh HTML应用Widget自定义悬浮提示的问题求助

解决方案

针对你遇到的三个问题,以下是具体修复方案:


1. 修复边框不显示问题

Bokeh默认样式会覆盖自定义边框,通过提高CSS选择器特异性来解决,同时明确边框样式以确保生效。

2. 修复提示箭头位置问题

为Slider容器添加相对定位,让伪元素相对于Slider定位,再调整箭头的位置使其正确附着在提示框左侧。

3. 在Python中定义提示文本

利用Bokeh Widget的attributes参数添加自定义data-tooltip属性,将提示文本直接写在Python代码中,无需在CSS中单独定义。


修改后的代码

main.py(关键部分修改)

# Set up widgets
offset = Slider(title="offset", value=0.0, start=-5.0, end=5.0, step=0.1,
                css_classes=['tooltip'],
                attributes={'data-tooltip': 'Change the offset of the sine wave'})
amplitude = Slider(title="amplitude", value=1.0, start=-5.0, end=5.0, step=0.1,
                   css_classes=['tooltip'],
                   attributes={'data-tooltip': 'Change the amplitude of the sine wave'})
phase = Slider(title="phase", value=0.0, start=0.0, end=2*np.pi,
               css_classes=['tooltip'],
               attributes={'data-tooltip': 'Change the phase of the sine wave'})
freq = Slider(title="frequency", value=1.0, start=0.1, end=5.1, step=0.1,
              css_classes=['tooltip'],
              attributes={'data-tooltip': 'Change the frequency of the sine wave'})
  • 移除了每个Slider的专属CSS类(如offset)
  • 添加attributes={'data-tooltip': '提示文本'}参数,直接在Python中定义提示内容

styles.css(完整修改)

/* 为Slider容器添加相对定位,确保伪元素正确定位 */
.tooltip {
  position: relative;
}

/* 鼠标悬停时显示提示框和箭头 */
.tooltip.bk-slider:hover:before, .tooltip.bk-slider:hover:after {
  display: block;
}

/* 提示框样式:提高选择器特异性,复刻Bokeh图表提示风格 */
.tooltip.bk-slider:before {
  content: attr(data-tooltip); /* 读取Python中定义的data-tooltip属性值 */
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  left: 101%;
  margin-left: 15pt;
  width: 300px;
  padding: 10px;
  border: 1px solid #ccc; /* 明确边框样式,覆盖Bokeh默认 */
  border-radius: 3px;
  background-color: white;
  color: #333;
  text-align: center;
  display: none;
  z-index: 1000;
  box-shadow: 0 2px 5px rgba(0,0,0,0.15); /* 添加阴影匹配Bokeh风格 */
}

/* 提示箭头样式:正确附着在提示框左侧 */
.tooltip.bk-slider:after {
  content: "";
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  left: calc(101% + 15pt);
  margin-left: -5px; /* 调整位置使箭头与提示框对齐 */
  border-width: 5px;
  border-style: solid;
  border-color: transparent white transparent transparent;
  display: none;
  z-index: 1001;
}
  • 使用attr(data-tooltip)动态读取Python中定义的提示文本
  • 提高选择器特异性(.tooltip.bk-slider:before)确保边框样式生效
  • 调整箭头定位逻辑,使其正确附着在提示框左侧

效果说明

  1. 提示框边框正常显示,样式与Bokeh图表提示一致
  2. 箭头正确出现在提示框左侧,指向对应的Slider组件
  3. 所有提示文本均在Python代码中定义,无需修改CSS即可更新内容

内容的提问来源于stack exchange,提问作者Azrael_DD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 12:05:21