问题
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