0

I am new to Python3 and bokeh. I have a trouble with button in bokeh.

refresh = Button(label='refresh')

def change_click():
if refresh.label == 'refresh':
    refresh.label = 'hello world'
else:
    refresh.label = 'refresh'

refresh.on_click(change_click))

The above is my code, and I am coding in python with bokeh. What I want to make is that let the button's label change every time I click on it, but the label just change to 'hello world' when the program is loaded. And when I click the button, it will not change back to refresh even though the label is 'hello world'. Can some one give me an idea why is this happen and how can I fix it.

1 Answer 1

1

Keep in mind that the indentation is important in python. You also have an extra parenthesis. This is working as you expect

from bokeh.models import Button
from bokeh.io import curdoc

refresh = Button(label='refresh')

def change_click():
    if refresh.label == 'refresh':
        refresh.label = 'hello world'
    else:
        refresh.label = 'refresh'

refresh.on_click(change_click)

curdoc().add_root(refresh)

Launch this with bokeh serve --show example.py.

This is also working:

from bokeh.models import Button
from bokeh.io import curdoc

refresh = Button(label='refresh')

def change_click():
    if refresh.label == 'refresh':
        refresh.update(label='hello world')
    else:
        refresh.update(label='refresh')

refresh.on_click(change_click)

curdoc().add_root(refresh)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.