text.on_change 对 Bokeh TextInput 没有响应 [英] text.on_change Not Responsive for Bokeh TextInput

查看:15
本文介绍了text.on_change 对 Bokeh TextInput 没有响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用 Python 散景绘图工具和小部件的初学者.在我下面的代码中,我试图将图表的标题更改为 TextInput 框的值.然而,虽然在输入文本时出现该框并且未聚焦,但没有任何变化.可能导致此问题的原因是什么,我可以做些什么来解决它?

I am a beginner to using Python's bokeh plotting tool and widgets. In my following code I am trying to have the title of the graph change to the value of the TextInput box. However, while the box appears upon entering in text and unfocusing, nothing changes. What could be causing this issue and what can I do to fix it?

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 进程.那是散景服务器.使用真正的 python 回调(即使用 on_change)只能在散景服务器应用程序中使用(这是散景服务器的目的,成为运行真正的 python 回调的东西.) 见:

By you using show(grid) you are creating a standalone HTML document as output. This has no possible way of running real python callbacks, because browsers have no ability to run python code. Running real callbacks requires having a connection to a persistent Python process. That is the Bokeh server. Using real python callbacks (i.e. with on_change) is only possible in bokeh server applications (that is the purpose of the bokeh server, to be the thing that runs real python callbacks.) See:

https://docs.bokeh.org/en/latest/docs/user_guide/server.html

还可以在 Juyter 笔记本中嵌入 Bokeh 服务器应用程序,例如,请参见此处:

It's also possible to embed Bokeh server apps in Juyter notebooks, for an example of that, see here:

https://github.com/bokeh/bokeh/blob/master/examples/howto/server_embed/notebook_embed.ipynb

这篇关于text.on_change 对 Bokeh TextInput 没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆