我正在使用带有 Bokeh 功能的单选按钮。我希望能够在单击单选按钮时显示加载指示器,然后在 python 回调完成后显示它已完成。如何使用 Bokeh 做到这一点?
我尝试将 js_onchange 与 onchange 的组合用于单选按钮。我只是想不出一种方法来在 Python 回调完成时通知 JS 端。
callback = CustomJS(args={}, code="""
document.getElementById('message_display').innerHTML = 'loading...';
""")
radio_button.js_on_change('active', callback)
radio_button.on_change('active', some_other_callback)
当我运行它时,innerHTML 设置为加载,并且 on_change Python 回调运行并且图形更新,但我无法触发 JS 方面的更改,将innerHTML 更改为完成。
最佳答案
假设用户 View 中已经有一个绘图,一种选择是在绘图范围的 start
属性上设置回调,以便在绘图更新时触发该回调。
from bokeh.models import CustomJS
p = figure()
def python_callback()
p.y_range = Range1d(None, None)
# get your data here and update the plot
code = "document.getElementById('message_display').innerHTML = 'loading finished';"
callback = CustomJS(args = dict(), code = code)
p.y_range.js_on_change('start', callback)
请参阅下面的工作示例:
import numpy as np
from bokeh.plotting import figure, show
from bokeh.models import CustomJS, ColumnDataSource
points = np.random.rand(50, 2)
cds = ColumnDataSource(data = dict(x = points[:, 0], y = points[:, 1]))
p = figure(x_range = (0, 1), y_range = (0, 1))
p.scatter(x = 'x', y = 'y', source = cds)
cb_to_make_selection = CustomJS(args = {'cds': cds}, code = """
function getRandomInt(max){return Math.floor(Math.random() * Math.floor(max));}
cds.selected.indices = [getRandomInt(cds.get_length()-1)]
""")
p.x_range.js_on_change('start', cb_to_make_selection)
show(p)
关于javascript - 如何在 Bokeh 中创建加载指示器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55798378/