我是使用 Python 的 Bokeh 绘图工具和小部件的初学者。在我的以下代码中,我试图将图表的标题更改为 TextInput 框的值。但是,虽然在输入文本和取消聚焦时出现该框,但没有任何变化。是什么导致了这个问题,我该如何解决?
p=figure(
height=400,
x_axis_type='datetime',
title=(company+' ('+tickerstring+') ')
)
thedates = np.array(stockdates, dtype=np.datetime64)
source = ColumnDataSource(data=dict(
x=thedates,
y=stockcloseprices
))
p.line('x', 'y', source=source)
p.grid.grid_line_color="white"
p.xaxis.axis_label = 'Date'
p.yaxis.axis_label = 'Price'
p.add_tools(HoverTool(
tooltips=[
("Date", "@x{%F}"),
('Close',"@y")
],
formatters={
'x':'datetime', # use 'datetime' formatter for 'date' field
},
mode='vline'
))
def update_title(attrname, old, new):
p.title = text.value
div = Div(text='<br><b> Key Points </b><br><br>'+percentagechange+'<br><br>'+performance,
width=200, height=100)
text = TextInput(value='Name', title="Enter Ticker Here:")
text.on_change('value', update_title)
grid = gridplot([p, div, text], ncols=2, plot_width=570, plot_height=400)
show(grid)
最佳答案
通过使用 show(grid)
,您将创建一个独立的 HTML 文档作为输出。这不可能运行真正的 python 回调,因为浏览器无法运行 python 代码。运行真正的回调需要连接到持久的 Python 进程。那是 Bokeh 服务器。使用真正的 python 回调(即使用 on_change
)只能在 bokeh 服务器应用程序中使用(这是 bokeh 服务器的目的,成为运行真正 python 回调的东西。 )见:
https://docs.bokeh.org/en/latest/docs/user_guide/server.html
也可以在 Juyter 笔记本中嵌入 Bokeh 服务器应用程序,有关示例,请参见此处:
https://github.com/bokeh/bokeh/blob/master/examples/howto/server_embed/notebook_embed.ipynb
关于python - text.on_change 对 Bokeh TextInput 没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34085683/