Instead of Interval component running function every few seconds, have live function update Dash itself arbitrarily?

Hello, all. This is a bigger question than usual, and I would extremely appreciate any advice on this. I have a Python script that continuously runs (until you manually cancel it, or the kernel dies), which is is analyzing a twitter account for new tweets, and automatically updates a csv file whenever a new tweet occurs.

Essentially, I want a Data Table in Dash to be updated live with new tweets as they come in, but the dcc.Interval component does not seem to work in the way I would need it to. As I understand it, it reruns a callback on a predetermined interval, but I want a continuously running function to update a Dash component by itself whenever it wants to (i.e., whenever a tweet comes in).

What would be the proper way of going about this?

1 Like

One way I was thinking of doing this was by placing an Interval component within the app.layout, instead of a callback, and having the global twitter script (global so it can run continuously?) update a global variable and be semi-controlled by two callbacks: one for starting it, and one for closing it.

This way, 1) the app.layout, with a global variable defining some Dash object, would automatically update every once once in a while and reflect any changes that occur to the global variable, 2) a global script would be in charge of updating the global variable, and 3) two callbacks would control the starting and stopping of the script, but nothing else.

How does this sound?

One option would be simply to read the last line(s) of the data file using the interval component (e.g. once per second) and update the table accordingly.

But in that case, how would I fire off the script to run continuously in the background in the first place, and also close it, without affecting the rest of the Dash app?

Ah, i thought that you wanted to run the script (which updates the .csv file) in one process and the Dash application in another. If this is not the case, you could just fetch the data directly in the callback (which is triggered by the interval component) and add it to the table.