Dynamically create tabs with figures in them

Hi all,

I am trying to create a bokeh (3.7.0) data analysis application. With different tabs for different analysis types, e.g. lineplots, histogram etc.

In this little example i am trying to dynamically add tabs with figures in them. It works as long as the second tab does not include a figure. Anyone knows how to solve it?

See also this thread on stackoverflow.

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()

There is an error reported in the JS console:

[Error] TypeError: null is not an object (evaluating 'this.document.event_manager')

This seems possibly like the bug that is fixed by Protect against `document == null` in `_doc_detached()` by mattpap · Pull Request #14430 · bokeh/bokeh · GitHub which will be part of the upcoming 3.7.1 release this week.

Please try again with 3.7.1 once it is released, and if that does not fix the issue for you then open a GitHub Issue with full details.

The PR above was just merged and I can confirm that it fixes the issue. I expect 3.7.1 to be released by tomorrow or the next day.

Edit: 3.7.1 has been released.

Super thanks for fast reply and the fix.