繁体   English   中英

如何实现JavaScript回调以更改散景图标题

[英]How to implement a javascripts callback to change a Bokeh graph title

我只想允许用户更改散景图的标题。 这是我尝试过的代码的最小示例。 问题是如何进行回调。


from bokeh.io import show, output_file
from bokeh.plotting import figure
from bokeh.models import CustomJS, Button

fig = figure(title='title')
fig.line(x=[1,2,3], y=[1,2,3])


callback = CustomJS(args={'title':fig.title}, code="""title.text = text_input.get('value');
""")

text_input = TextInput(title="Add graph title", value='', callback=callback)


widgets_layout = column(text_input)


figures_layout = row(fig)


page_layout = row(widgets_layout, fig)


script, div = components(page_layout)
return render_to_response('fig.html', {'script': script, 'div': div})


我没有收到任何错误,但是在TextInput字段中输入新标题时没有任何反应。

有任何想法吗 ?

.get(...)语法很久以前就已删除。 在Bokeh的任何较新版本中,只需直接访问.value即可。 另外,要在回调中定义text_input ,您需要在args传递它。 这是您代码的更新版本:

from bokeh.io import show
from bokeh.layouts import column, row
from bokeh.models import CustomJS, TextInput
from bokeh.plotting import figure


fig = figure(title='title')
fig.line(x=[1,2,3], y=[1,2,3])

text_input = TextInput(title="Add graph title", value='')
text_input.js_on_change('value', CustomJS(
    args={'title': fig.title, 'text_input': text_input},
    code="title.text = text_input.value"
))

widgets_layout = column(text_input)

figures_layout = row(fig)

show(row(widgets_layout, fig))

但是,如果Bokeh> = 1.1,则可以只使用js_link而不必完全创建CustomJS

text_input = TextInput(title="Add graph title", value='')
text_input.js_link('value', fig.title, 'text')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM