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

时间:2019-05-08 15:36:20

标签: javascript bokeh

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


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字段中输入新标题时,什么也没有发生。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

.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))

但是,在散景> = 1.1的情况下,您只需使用js_link,而不必完全创建CustomJS

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