Bokeh小部件为单个事件调用CustomJS和Python回调? [英] Bokeh widgets call CustomJS and Python callback for single event?

查看:182
本文介绍了Bokeh小部件为单个事件调用CustomJS和Python回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Bokeh应用程序,它利用各种小部件事件的Python回调。对于某些事件,我想在回调Python函数之前执行一些JavaScript代码。这可能吗?

I have a Bokeh application that makes use of the Python callbacks for various widget events. With certain events, I'd like to execute some JavaScript code before making the callback to the Python function. Is this possible?

在这种情况下,Python回调可能是长时间运行的,我想在长时间之前和之后启动和停止Javascript微调器对象 - 运行Python代码。

In this case, the Python callback is potentially long-running, and I'd like to start and stop a Javascript spinner object before and after the long-running Python code executes.

推荐答案

截至Bokeh 1.0.4,忙碌/完成事件(启用事物)比如触发微调器或其他UI事件)仍然是一个开放的功能请求。

As of Bokeh 1.0.4, "busy" / "done" events (to enable things like triggering spinners or other UI events) are still an open feature request.

与此同时,你最好的选择是使用一些虚拟模型来触发 CustomJS 回调。例如,您可以添加一个不可见的字形,并在其上触发 CustomJS 任何属性作为忙事件的代理。这很笨重,但很实用。

In the mean time, your best bet is to use some "dummy" model to trigger a CustomJS callback. For instance, you could add an invisible glyph, and trigger a CustomJS any property on it as a proxy for a "busy" event. This is clunky, but serviceable.

这是一个非常粗略的大纲示例。第一个警报会立即弹出。关闭它,5秒后会弹出下一个警报。

Here is a very rough outline example. The first alert will pop up immediately. Close it, the next alert will pop up 5 seconds later.

import time

from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.models import Button, CustomJS
from bokeh.plotting import figure

p = figure()
p.circle([1,2,3,4,5], [2,6,3,1,6])

dummy = p.circle([1], [2], alpha=0)
dummy.glyph.js_on_change('size', CustomJS(code="""
alert(cb_obj.size.value)
"""))

b = Button()
def cb():
    dummy.glyph.size = 10
    time.sleep(5)
    dummy.glyph.size = 20

b.on_click(cb)

curdoc().add_root(column(b, p))

这篇关于Bokeh小部件为单个事件调用CustomJS和Python回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆