python - Dynamically add tabs with figures in bokeh - Stack Overflow

admin2025-04-09  1

I have a bokeh server application (3.7.0). When the application is started one tab is shown and when the execute button is pressed another tab is added. It works as long as the second tab does not contain figures. How can I solve this?

    from bokeh.io import curdoc
    from bokeh.layouts import layout
    from bokeh.models.widgets import Div, Button
    from bokeh.models import Tabs, TabPanel
    from bokeh.plotting import figure
 
def tab1():

    w_1 = Div(text="<b>T1</b>",
          styles={'font_family': 'arial', 'text_align': 'center', 'font-size': '20px', 'color': 'blue'}, width=200, height=100)
    p1 = figure(width=300, height=300)
    p1.scatter([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="navy", alpha=0.5)
    w_execute = Button(label='Execute', name='Execute')

    l1 = layout([[w_1],
                [p1],
                [w_execute]])
    t1 = TabPanel(child=l1,title="T1")
    tabs = Tabs(tabs=[t1])
    
    def func_run(new):
        add_t2(t1)

    w_execute.on_click(func_run)
    curdoc().add_root(tabs)

def add_t2(t1):
    curdoc().clear()
    t2 = tab2()
    tabs = Tabs(tabs = [t1, t2])
    curdoc().add_root(tabs)

def tab2():
    w_2 = Div(text="<b>T2</b>",
          styles={'font_family': 'arial', 'text_align': 'center', 'font-size': '20px', 'color': 'blue'}, width=200, height=100)
    p2 = figure(width=300, height=300)
    p2.scatter([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="navy", alpha=0.5)

    # l2 = layout([[w_2],
    #             [p2]]) # Does not work
    l2 = layout([[w_2]]) # Works
    t2 = TabPanel(child=l2,title="T2")
    return t2

tab1()
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744203302a235902.html

最新回复(0)