text.on_change Not Responsive for Bokeh TextInput

我的未来我决定 提交于 2020-01-28 02:33:30

问题


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)

回答1:


By you using show 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



来源:https://stackoverflow.com/questions/51690690/text-on-change-not-responsive-for-bokeh-textinput

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!