javascript - 如何在 Bokeh 中创建加载指示器?

标签 javascript python bokeh

我正在使用带有 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/

相关文章:

javascript - 变量在函数中灰显

javascript - 无法在 jQuery-UI 选项卡中显示 Google DFP 广告

python - Numpy 裁剪二维数组到非 NaN 值

python - 在 NumPy 中根据相邻值计算值的惯用方法是什么?

python - ipywidgets : how to update plot with multiple series based on checkbox selection

javascript - jquery replace 不替换所有空格 -

javascript - 如何使用复选框显示/隐藏 HTML 元素

python - 在 python 3.x 中删除 raw_input() 的原因是什么?

python - 条形图的 Bokeh 对数刻度

python - 如何在工具栏中不显示多个图标的情况下添加多个悬停工具?