首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Bokeh:小部件上的textInput on_change函数不像预期的那样工作

Bokeh:小部件上的textInput on_change函数不像预期的那样工作
EN

Stack Overflow用户
提问于 2015-12-08 09:19:44
回答 3查看 7K关注 0票数 2

我的简短剧本如下所示:

代码语言:javascript
复制
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服务器(Bokeh)运行时,我得到了绘图,但是on_change回调不像预期的那样工作。

假设textInput的值应该是输入框中的内容/字符串,但我多次更改它,但回调函数update_title从未被调用( count全局变量始终为0)。因此,显然底层的textInput.value没有改变,我如何才能更改on_change函数的值呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-12-08 13:30:42

下面是一个简单的TextInput示例,它使用回调而不是.on_change()。对于像我这样的初学者来说,这可能比OP更有帮助。我非常轻微地修改了滑块示例

guide/interaction/callbacks.html#customjs-for-model-property-events

代码语言:javascript
复制
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)
票数 2
EN

Stack Overflow用户

发布于 2015-12-08 10:50:46

我和你有同样的问题。搜索之后,on_change函数不再使用bokeh0.10,而是使用即将发布的0.11版本。

来自:https://groups.google.com/a/continuum.io/forum/#!topic/bokeh/MyztWSef4tI

如果在最新的dev构建中使用(新的) Bokeh服务器,可以遵循以下示例:https://github.com/bokeh/bokeh/blob/master/examples/app/sliders.py

来自:https://groups.google.com/a/continuum.io/forum/#!topic/bokeh/PryxrZPX2QQ

服务器最近被彻底重写,速度更快,更小/更简单,更易于使用、部署和解释。主要公关刚刚合并为master,并将在12月发布的0.11版中出现

下载dev版本:https://anaconda.org/bokeh/bokeh/files

票数 2
EN

Stack Overflow用户

发布于 2020-04-16 09:23:29

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

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34152066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档