散景:小部件上的 textInput on_change 功能未按预期工作

Bokeh: textInput on_change function on widget doesn't work as expected

我的简短脚本如下所示:

output_server('ts_sample.html')

count = 0
def update_title(attrname, old, new):
    global count
    count = count + 1

textInput = TextInput(title="query_parameters", name='fcp_chp_id', value='fcp_chp_id')
textInput.on_change('value', update_title)

curdoc().add_root(textInput)
p = figure( width=800, height=650,title="ts_sample",x_axis_label='datetime' )
p.line(np.array(data['date_trunc'].values, dtype=np.datetime64), data['latitude'], legend="test")
p.xaxis[0].formatter=bkmodels.formatters.DatetimeTickFormatter(formats=dict(hours=["%F %T"]))
show(curdoc())

当散景服务器 (bokeh serve) 为 运行 并且我得到了绘图时它有效,但 on_change 回调没有按预期工作。

假设textInput的值应该是输入框里的content/string,但是我改了好多次都没有调用回调函数update_title(count全局变量一直为0) .所以显然底层 textInput.value 没有改变,我怎样才能改变值 attr 并触发 on_change 函数?

我和你有同样的问题。 经过搜索,on_change 函数不适用于 bokeh 0.10 版本,但适用于即将发布的 0.11 版本。

发件人:https://groups.google.com/a/continuum.io/forum/#!topic/bokeh/MyztWSef4tI

If you are using the (new) Bokeh server in the latest dev builds, you can follow this example, for instance: https://github.com/bokeh/bokeh/blob/master/examples/app/sliders.py

发件人:https://groups.google.com/a/continuum.io/forum/#!topic/bokeh/PryxrZPX2QQ

The server has recently been completely re-written from the ground up and is faster, smaller/simpler, and much easier to use and deploy and explain. The major PR was just merged into master, and will show up in the upcoming 0.11 release in December

下载开发版:https://anaconda.org/bokeh/bokeh/files

这是一个使用回调而不是 .on_change() 的简单 TextInput 示例。对于像我这样的初学者,这可能比 OP 更有帮助。我稍微修改了

中的滑块示例

http://docs.bokeh.org/en/latest/docs/user_guide/interaction/callbacks.html#customjs-for-model-property-events

from bokeh.layouts import column
from bokeh.models import CustomJS, ColumnDataSource, Slider
from bokeh.models import TextInput
from bokeh.plotting import figure, show

x = [x*0.005 for x in range(0, 200)]
y = x

source = ColumnDataSource(data=dict(x=x, y=y))

plot = figure(plot_width=400, plot_height=400)
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)

callback = CustomJS(args=dict(source=source), code="""
        var data = source.get('data');
        var f = cb_obj.get('value')
        x = data['x']
        y = data['y']
        for (i = 0; i < x.length; i++) {
            y[i] = Math.pow(x[i], f)
        }
        source.trigger('change');
    """)

#slider = Slider(start=0.1, end=4, value=1, step=.1, title="power", callback=callback)
#layout = vform(slider, plot)

text_input = TextInput(value="1", title="power", callback=callback)
layout = column(text_input, plot)

show(layout)

我改编了这个例子,可能会有帮助:

from bokeh.layouts import column
from bokeh.models import TextInput
from bokeh.models import CustomJS, ColumnDataSource, Slider
from bokeh.plotting import figure, show

x = [x*0.005 for x in range(0, 200)]
y = x

source = ColumnDataSource(data=dict(x=x, y=y))

plot = figure(plot_width=400, plot_height=400)
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)

callback = CustomJS(args=dict(source=source), code="""
        var data = source.data;
        var f = cb_obj.value;
        x = data['x']
        y = data['y']
        for (i = 0; i < x.length; i++) {
            y[i] = Math.pow(x[i], f)
        }
        source.change.emit();
    """)

#slider = Slider(start=0.1, end=4, value=1, step=.1, title="power", callback=callback)
#layout = vform(slider, plot)

text_input = TextInput(value="1", title="power", callback=callback)
layout = column(text_input, plot)

show(layout)