首页 > 解决方案 > Python Bokeh 2.0.1 - UndoTool 的 JS 回调?

问题描述

我最近开始在 Anaconda 上使用 Bokeh 2.0.1。我的首要目标是使用 Bokeh 菜单工具将一些数据集可视化为独立的 html 文件。

除了现有工具之外,我还添加了一个功能,DoubleTap 在图上的抽头坐标处放置一个标签。它按计划工作,但是,我希望通过标准 Bokeh UndoTool 撤消此操作。我尝试向相关图形的 UndoTool 实例添加一个 CustomJS 回调。但是,我无法让它工作 - 当我单击图中的撤消按钮时,添加的标签不会消失。显然,“撤消”回调不会被触发。

我知道“撤消”回调不是问题,因为我还将它绑定到一个按钮并且它按计划工作。

概念代码为:

from bokeh.plotting import figure
from bokeh.events import MenuItemClick, ButtonClick
from bokeh.models import CustomJS, Button
from bokeh.events import DoubleTap

add_label = CustomJS(--something--)
remove_label = CustomJS(--something else--)

f_h = figure(tools='undo')
f_h.js_on_event(DoubleTap, add_label) # Works as planned - adds a label on a double tap

loc_button = Button()
loc_button.js_on_event(ButtonClick, remove_label) # Also works as planned - removes the last added label

f_h.tools[0].js_on_event(MenuItemClick, remove_label) # Doesn't work - aside from the standard scaling undo behavior nothing happens

提前致谢,

光伏

标签: pythonbokeh

解决方案


有一个reset事件:

from bokeh.io import show
from bokeh.models import CustomJS
from bokeh.plotting import figure

p = figure(tools='reset')
p.circle(x=0, y=0)

p.js_on_event('reset', CustomJS(code="console.log('Resetting!')"))

show(p)


推荐阅读