gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:01:16 26 4
gpt4 key购买 nike

我只是想让用户能够更改 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 字段中输入新标题时没有任何反应。

有什么想法吗?

最佳答案

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

关于javascript - 如何实现 JavaScript 回调来更改 Bokeh 图标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56044315/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com