我的简短剧本如下所示:
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函数的值呢?
发布于 2015-12-08 13:30:42
下面是一个简单的TextInput示例,它使用回调而不是.on_change()。对于像我这样的初学者来说,这可能比OP更有帮助。我非常轻微地修改了滑块示例
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)发布于 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版中出现
发布于 2020-04-16 09:23:29
我修改了这个例子,它可能会有帮助:
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)https://stackoverflow.com/questions/34152066
复制相似问题