text.on_change对散景TextInput不响应 [英] text.on_change Not Responsive for Bokeh TextInput

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

问题描述

我是使用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服务器的用途,成为运行真实python回调的东西.)请参阅:

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

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

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

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)

解决方案

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

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对散景TextInput不响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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