Add roots to curdoc() dynamically not updating

I would like to do something similar to this:


def upload_h5_to_server(attr, old, new):
    filename = write_temp_file(file_input.value)
    curdoc().add_root(ExtendedView(sync.Sync(filename)).layout)


file_input = FileInput(align='end', accept='.h5')
file_input.on_change('value', upload_h5_to_server)

curdoc().add_root(file_input)
curdoc().title = "SyncViewer"

Unfortunately, when upload_h5_to_server is called via callback, this update (at least visually) doesn’t occur. If I call it manually, it will update as expected but I assume this is b/c no session has been setup yet.

Is there a proper technique for this?

After playing about in the debugger, I have made it work like this:

curdoc().roots[0].children = [my_new_layout]

But I feel like this isn’t really the intent.

@Cosmidark c.f. similar recent post

replace “completely clearing curdoc” with “adding roots dynamically” above.

1 Like

I think that’s literally what my code above is doing. I did try to clean it up a bit with the name property and select function.

I’m not sure why it would be considered unusual to at least add elements dynamically though. In the particular use case I have (that screen show from my prior post), I don’t know how many lines of data are coming in so I can’t pre-generate a set of empty plots.

In my case, I do have a top level layout composed of my plots, some data tables and a few controls. The plots are added once I know how many there should be.

It is, I was adding context for “I feel like this isn’t really the intent.”

I’m not sure why it would be considered unusual to at least add elements dynamically though.

I’m not sure what to add except to note that in practice, almost no one ever seems to do it, which is also exactly why it is more likely to suffer from bugs. As I mentioned in the other topic, a GH issue with an MRE would be appropriate.

1 Like