Quantcast
Channel: 🎛️ Dash - Plotly Community Forum
Viewing all articles
Browse latest Browse all 6271

Conditional callback using a javascript workaround

$
0
0

In my particular use-case, I have encountered some unforeseen problems. Using client-side callbacks I have managed to achieve the desired behaviour by simulating a “conditional callback”. I have seen other people requesting such a feature so this might save someone some time in the future. In addition, there’s probably a cleaner method to become the same results, so suggestions are very welcome.

Desired behaviour: the user can choose from a dataset which data to display. Meanwhile, the server is polled and if there is new data, the display should be automatically updated.

My initial problem: A user can select data to be shown, triggering callback A. At a regular interval, callback B polls for new data. By passing this data to A it is displayed. Since both the user and the polling call A this caused some issues. Initially, I tried to fix it by having B return a no_update if there is no new data. However, when the poll request B overlaps with a user request A, it seems that the no_update prevents the user request from properly updating.

My hack-ish fix: To prevent B from always calling callback A, even if there is no new data, I decoupled A from B. Instead of connecting the output of B to the input of callback A, a hidden button serves as an intermediary. Callback B triggers a client-side javascript function that checks the condition (is there new data?). If true, the hidden button is programmatically clicked, triggering callback A.

If this is useful to anyone, I would be glad to provide a code example.

3 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 6271

Trending Articles