Dynamically add widget to column

I am trying to add a widget dynamically to a column. I found this old post on StackOverflow but it doesn’t work.

The issue is that column(children=[…]) returns type Column and the children method in Column is of type List[UIElement] and it does not support append or insert methods like a Python list. Perhaps the Bokeh list type was introduced after the SO post.

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

load_data_button = Button(label="Load Data")
clear_data_button = Button(label="Clear Data")

doc_layout = column(children=[load_data_button, clear_data_button])
current_doc = curdoc().add_root(doc_layout)

new_button = Button(label="New")

doc_layout.children.append(new_button)

Am I doing something incorrectly here? Is there another/better way to do dynamic insertion of widgets? I am coming from Shiny Python and looking for the equivalent of ui_insert.

@kampai-shp It it definitely does support methods like append and insert:

In [1]: from bokeh.plotting import figure, column

In [2]: layout = column(children=[figure(), figure()])

In [3]: layout.children.append(figure())

In [4]: layout.children
Out[4]: [figure(id='p1001', ...), figure(id='p1035', ...), figure(id='p1070', ...)]

You should post the code that you tried that did not work, along with a full stack trace or other error message.

@kampai-shp In fact, the code you have above works just fine. You will need to provide more details about the specifics of your situation, including any actual error message, how exactly you are running things, and most especially, all relevant version information.

Thanks for the quick response, Bryan. I see now that it does work, despite flagging in Pylance in VSCode:

Cannot access attribute "append" for class "List[UIElement]"
  Attribute "append" is unknown Pylance (reportAttributeAccessIssue)
(function) append: Unknown

This typing error usually means the attribute doesn’t exist and the code won’t run. I’ll #type ignore it.

What is Bokeh’s process for getting type checkers to recognize custom Bokeh types? Another one is having the “active” attribute of RadioButtonGroup return a Nullable[int]. It also flags in Pylance.

Bokeh’s custom property descriptors predate tools like MyPy, but provide extensive and important features (automatic validation, serialization, docs integration, as well as more types that are not otherwise expressible) that make it unfeasible to simply move away from them. So it’s not an ideal situation in the context of contemporaty tooling. There are some ongoing efforts to try and improve things, feel free to chime in any specific issues for motivation.

Allow to statically type models and properties in bokeh · Issue #13292 · bokeh/bokeh · GitHub
Add support for manually generated stub files (*.pyi) by mattpap · Pull Request #13900 · bokeh/bokeh · GitHub