Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

python - text.on_change Not Responsive for Bokeh 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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...