Search code examples
pythonbuttonserverbokeh

Adding objects dynamically in bokeh server application


I would like to add objects dynamically on the bokeh server. The example I am trying to run is the following bokeh server app:

from bokeh.layouts import column
from bokeh.plotting import curdoc
from bokeh.models import Button

def add_button():
    print("adding button")
    curdoc().add_root(column(button, button2))


 button = Button(label="Start", button_type="success")
 button.on_click(add_button)
 button2 = Button(label="Next", button_type="success")

 curdoc().add_root(column(button))

Many thanks for any help.


Solution

  • Did you want to keep adding a new button each time? if so try this :

    from bokeh.layouts import column, layout
    from bokeh.plotting import curdoc
    from bokeh.models import Button
    from bokeh.models.widgets import Div
    
    
    def add_button():
        print("adding button")
        layout.children.append(Button(label="Hi I am another button", button_type="success"))
    
    
    button = Button(label="Click to add a button", button_type="success")
    button.on_click(add_button)
    layout = layout([[button]])
    curdoc().add_root(layout)
    

    If you only wanted to add a new button once, then just append Button2.