如何实现 JavaScript 回调来更改 Bokeh 图标题

Blu*_*lue 3 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})


Run Code Online (Sandbox Code Playgroud)

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

有任何想法吗 ?

big*_*dot 7

.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))
Run Code Online (Sandbox Code Playgroud)

然而,当 Bokeh >= 1.1 时,您可以直接使用js_link并避免创建一个CustomJS

text_input = TextInput(title="Add graph title", value='')
text_input.js_link('value', fig.title, 'text')
Run Code Online (Sandbox Code Playgroud)