Update UI from the server

I am writing a simulation application that reads in data from a database, configures the simulation, runs it in the cloud, and then retrieves and plots the result.
It also gets notified of changes to the database so it can update the UI.
Simulation duration ranges from nearly instantaneous to multiple days.

So I’m building this dashboard app thingy for configuring the simulation and plotting the results.
Due to the wide range of simulations, I want to be able to run the UI by itself for simple simulations, in a notebook for custom scripting, or as a script that just saves the output.

The part I’m having trouble with is updating the UI when an external change happens.
All the examples I can find use client-initiated updates, like you drag a slider and that updates the plot.
In my case, the update is external, from the database or the simulation server.

Does Panel have a portable way to update the UI based on an external server-initiated callback?

1 Like

Hi @pepijndevos

Yes Panel has. For example you can use pn.state.add_periodic_callback. You can use that to periodically check for updates to a file, a database or similar and then react to that change by for example updating widgets, panes, layouts and Parameterized classes.

You can find a lot of examples using this search Search: pn.state.add_periodic_callback.

Please note that this updates a single user session only. I believe a global add_peridic_callback that will update across all user sessions is coming. But I have not seen it released yet.

If the function you need to execute is long running you might want to run it in a seperate thread (see Async and Concurrency — Panel 0.12.4 documentation (holoviz.org)) or in a separate process outside the application.

If you have specific questions please try to make a small, specific code example to start from.

I think this is an area Panel is very good at. So hope you find a good solution :+1:


Thanks! The separate thread seems really promising, I will need to try that.

What I found with Dash and Bokeh that you can’t just update properties from a thread. They indeed require a periodic callback to poll for updates. I just learned that Bokeh does have add_next_tick_callback to schedule an update from a worker thread on the main thread, which also seems promising.

I’m still kind of trying to figure out how Panel is different from Bokeh, because apparently in Panel you can just update some state from a thread and it just works? I’ll have to try this.

It’s also a bit mysterious to me which state is per client and what is global to the server.

1 Like