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

为何Bokeh的Label组件render_mode='css'在一台设备正常另一台报错?

Bokeh Label组件报错及边框不显示问题解决

问题场景

使用以下代码向图表添加文本框:

citation = bokeh.models.Label(x=xPosition, y=yPosition, x_units='screen', y_units='screen',
                 text=textstr, 
                 render_mode='css',
                 border_line_color='wheat', border_line_alpha=0,
                 background_fill_color='wheat', background_fill_alpha=0)

p.add_layout(citation)

该代码在一台Windows 10笔记本上运行正常,但在另一台同系统笔记本上抛出AttributeError,报错信息如下:

AttributeError                            Traceback (most recent call last)
Cell In[58], line 80
     74 p.line('distance', 'grade', name='Grade', color="blue", line_width=0.30, 
     75        y_range_name='Grade', line_dash="4 2", source=source)
     78 #labels = LabelSet(x='weight', y='height', text='names',
     79 #              x_offset=5, y_offset=5, source=source, render_mode='canvas')
---> 80 citation = bokeh.models.Label(x=xPosition, y=yPosition, x_units='screen', y_units='screen',
     81                  text=textstr, 
     82                  render_mode='css',
     83                  border_line_color='wheat', border_line_alpha=0,
     84                  background_fill_color='wheat', background_fill_alpha=0)
     86 p.add_layout(citation)
     89 # show the results

File C:\ProgramData\Anaconda3\lib\site-packages\bokeh\models\annotations\labels.py:122, in Label.__init__(self, *args, **kwargs)
    121 def __init__(self, *args, **kwargs) -> None:
--> 122     super().__init__(*args, **kwargs)

File C:\ProgramData\Anaconda3\lib\site-packages\bokeh\models\annotations\labels.py:78, in TextAnnotation.__init__(self, *args, **kwargs)
     77 def __init__(self, *args, **kwargs) -> None:
---> 78     super().__init__(*args, **kwargs)

File C:\ProgramData\Anaconda3\lib\site-packages\bokeh\models\annotations\annotation.py:49, in Annotation.__init__(self, *args, **kwargs)
     48 def __init__(self, *args, **kwargs) -> None:
---> 49     super().__init__(*args, **kwargs)

File C:\ProgramData\Anaconda3\lib\site-packages\bokeh\models\renderers\renderer.py:76, in Renderer.__init__(self, *args, **kwargs)
     75 def __init__(self, *args, **kwargs) -> None:
---> 76     super().__init__(*args, **kwargs)

File C:\ProgramData\Anaconda3\lib\site-packages\bokeh\model\model.py:110, in Model.__init__(self, *args, **kwargs)
    107 if "id" in kwargs:
    108     raise ValueError("initializing 'id' is not allowed")
--> 110 super().__init__(**kwargs)
    111 default_theme.apply_to_model(self)

File C:\ProgramData\Anaconda3\lib\site-packages\bokeh\core\has_props.py:298, in HasProps.__init__(self, **properties)
    296     if value is Undefined or value is Intrinsic:
    297         continue
--> 298     setattr(self, name, value)
    300 for name in self.properties() - set(properties.keys()):
    301     desc = self.lookup(name)

File C:\ProgramData\Anaconda3\lib\site-packages\bokeh\core\has_props.py:333, in HasProps.__setattr__(self, name, value)
    330 if isinstance(descriptor, property): # Python property
    331     return super().__setattr__(name, value)
--> 333 self._raise_attribute_error_with_matches(name, properties)

File C:\ProgramData\Anaconda3\lib\site-packages\bokeh\core\has_props.py:368, in HasProps._raise_attribute_error_with_matches(self, name, properties)
    365 if not matches:
    366     matches, text = sorted(properties), "possible"
--> 368 raise AttributeError(f"unexpected attribute 'render_mode' to Label, possible attributes are angle, angle_units, background_fill_alpha, background_fill_color, border_line_alpha, border_line_cap, border_line_color, border_line_dash, border_line_dash_offset, border_line_join, border_line_width, coordinates, group, js_event_callbacks, js_property_callbacks, level, name, propagate_hover, subscribed_events, syncable, tags, text, text_align, text_alpha, text_baseline, text_color, text_font, text_font_size, text_font_style, text_line_height, text_outline_color, visible, x, x_offset, x_range_name, x_units, y, y_offset, y_range_name or y_units

备注:两台设备均已升级至最新可用Bokeh版本。注释掉render_mode='css'后,第二台设备代码可正常运行,但文本框边框无法显示。

解决方案

  • 移除废弃属性:Bokeh新版本中,Label组件已移除render_mode属性,该功能由coordinates属性替代。由于代码中已设置x_units='screen'和y_units='screen',直接删除render_mode='css'参数即可解决报错。
  • 修复边框显示:边框不显示是因为border_line_alpha=0(完全透明),将其修改为大于0的数值(如1)即可显示边框:
    citation = bokeh.models.Label(x=xPosition, y=yPosition, x_units='screen', y_units='screen',
                     text=textstr, 
                     border_line_color='wheat', border_line_alpha=1,
                     background_fill_color='wheat', background_fill_alpha=0)
    
    p.add_layout(citation)
    
  • 确认版本一致性:虽然声称都是最新版本,仍需验证两台设备的Bokeh版本是否完全一致。在终端运行以下命令查看版本:
    import bokeh
    print(bokeh.__version__)
    
    若版本不一致,重新安装对应版本:
    pip install bokeh==<确认的版本号>
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 09:14:59