Python callback set after add_root doesn't work

I am not sure if this is the expected behaviour or a bug, but if I do

curdoc().add_root(layout)
myfig.on_event(events.SelectionGeometry, my_python_func)

The callback is not registered and never called. However, if I switch the order of the lines and call on_event before doing add_root(layout), then the callback is registered and called correctly.

Following the MVC pattern, I was trying to create a GUI completely (including the add_root operation), with no callbacks associated until a Controller object is completely created, so that the controller methods are later registered as callbacks. I want the GUI class to be independent of everything else, that’s why I was creating all GUI elements with dummy datasources and no callbacks, and then update everything after the Controller has been created and initialized.

Thanks!

@Pablo

add_root is the explicit step that collects all the reachable Bokeh objects for serialization to the browser. Anything you want to be mirrored to the browser has to be added before the call to add_root. That is, this is expected behavior, and you will need to call add_root later (usually it is simply the very last step in a a Bokeh app script).

Additionally, there is no GUI actually created until things hit the browser, and nothing hits the browser until after all the app code is executed to build up the serialized object graph. I don’t think there is any bearing on MVC practice here. Calling curdoc().add_root(...) is just the more-or-less boilerplate step you do at the end to tell Bokeh what to send to the browser.