Trigger a Dash event discretionary from python code

Hi.

I have a loading dialog that prompts to the user while the data is being loaded. I can easily show it when the user clicks a button, but I also need an easy way to hide it when the data is loaded.

I was thinking that I nice way to address it it would be to have a callback that listens to an event (a signal) and hides the dialog when this event is received. The last piece I need to finish my puzzle it would be to have a way to trigger a Dash event that a callback can listen to directly from my Python code.

I have been looking in the documentation and in the forums, but I havenā€™t found any way to trigger this kind of events.

As a workaround, I could use a hidden div and use it as a trigger for hiding the dialog. But the other way looks like a more convenient way to me; moreover, I think the potential of Dash could be enlarger provided with this capability.

The most generic solution to this is going to be per-component loading states, as discussed in šŸ“£ Dash Loading States. This is a big project, and weā€™re looking for companies or organizations to help sponsor the work (Consulting, Training & Open-Source Development).

Another alternative is to build your own component (Build Your Own Components | Dash for Python Documentation | Plotly) that provides properties / attributes that represent the different stages of this component (ā€œloadingā€, ā€œdone_loadingā€, etc).

The most generic solution to this is going to be per-component loading states, as discussed in :mega: Dash Loading States.

Awesome. This really suits our needs, although we are still looking for a way to discretionally trigger callbacks from Python code.

This is a big project, and weā€™re looking for companies or organizations to help sponsor the work (Consulting, Training & Open-Source Development).

We will consider it. Thank you.

Another alternative is to build your own component (Dash Documentation & User Guide | Plotly) that provides properties / attributes that represent the different stages of this component (ā€œloadingā€, ā€œdone_loadingā€, etc).

Iā€™ve already done that, here is our loading react component: grasia-dash-components/src/components/LoadingDialog.react.js at master Ā· Grasia/grasia-dash-components Ā· GitHub But weā€™re looking for a way to integrate with the official dash-renderer repo (see opened issue).

Going back to the topic thread, would you be willing to merge a pull request that allows to trigger dash Events from python code? I think that it isnā€™t a big work, am I right?

Right now, Dash uses regular HTTP requests, not websockets, so the event has to ā€œoriginateā€ from the browser. So, even if you were to able to call the Python function in Python, we donā€™t have a way to propagate that update to the dash front-end. In the future, if we ever add websocket support, this would be possible.

1 Like

possible, server side events might be of interest here. this bypasses the need for websockets, allowing for the existing mechanisms in place to provide pushing to the browser.

This is both very doable with generators, a interesting implementation based on redis also exists.

Any progress on this? Especially based on the last comment.

No progress on this, and this isnā€™t currently scheduled for the next few months. Weā€™re focusing on a few other things for the short term: Projects Ā· plotly Ā· GitHub

I see, well, fingers crossed to see it at some point. Still, the changes over the past few months have been great already!

And, follow-up question: I could try to workaround using events, because I donā€™t want to handle too many inputs. What are the events associated with dcc.Dropdown and dcc.Input? I only want to register ā€˜user makes a changeā€™ or ā€˜user interactsā€™. Is there a doc somewhere?

There isnā€™t really. I"m actually trying to move away from Event, as it isnā€™t quite the right abstraction (itā€™s not possible to tell _which_event was fired, which makes it unuseful and confusing). Instead, I recommend using Input to trigger your change. If you donā€™t want to handle as many inputs in the callback, just place them at the end and use *args:

@app.callback(Output(...), [Input(...), Input(...), Input(...), ...])
def update(value1, value2, *args)

If I understood the comment by @_jf correctly, it should somehow be possible to use Server-Sent-Events to ā€œLive-Updateā€ the Dash-Dashboard, right? This would be pretty helpful, as I currently have a Prediction-Service in the back-end which I want to use to publish new predictions.
I had a look at the Medium-Post, but I am not sure if and how this could be integrated within Dash? Do you guys know any further resources on how to implement SSE such that the Dash-Interface is getting updates from the prediction-service?

Thank you in advance and happy holidays everyone!