Data generator inside a callback

I am trying to employ a data generator inside a callback of a dash app. The idea is to plot some values which are being updated within a data generator function. The generator is created using yield and my question is that how can I employ the generator in an correct way in dash applications. Here are some information that might be helpful for more clarification:

Thanks in advance for your helps,

I am a newbie to Dash Plotly - but I recently created an example with an animated graph which updates once a second.
I had similar errors when the format of the data I was returning was not quite what Dash was expecting…
Feel free to check my code in case it helps…

thanks a lot for your advice and the code. Indeed, I didn’t manage to run the code, however, here is my understanding from your code:

  • data generator should be out of callback
  • the output of data generator should be a global variable from the point of “dash app”.
  • the generator will be assessed for according to the n_intervals periods,

I will follow such a structure to adapt it to my own application, but a question that confuses me:

if data generator takes more time than ‘n_intervals’, the app will miss the data? or it still continues performing the generator in parralel while updating the app?

I mean that is it possible to force the callback to update immediately after finishing of each data delivery from generator?

is it possible to have a callback with a dependency on a data generator that might take variable period of time (for processing)?

Again thanks

Hi,
Thanks for the update - but I am not sure those are the takeaways I was intending to share. My issue was the actual content of the ‘data’ variable was not correct and I got the same error as you. If, however, you have been able to successfully plot your chart outside the callback with the same data - then this is not the same issue for you.

In terms of your other points - as I am also a DASH Newbie not sure how accurate the following points may be…
My get_data takes less than the 1s interval - so I don’t have the same issue you are potentially faced with.
I am also fairly new to Python but my guess is that if the generator is running on the same thread as the callback, it would not be possible for the callback to trigger again as your generator call is still tieing up the callback thread?

I guess a dirty/hack way could be to use a global variable which is set and unset by your generator to indicate when it is still generating and check for that at the start of the callback and return if set, and thereby avoiding another call to the generator?

hello, again thanks, in fact, with the same view, I tried to employ:

def get_new_data():

        global net_output
        net_output = next(generate)

APP definition …

def get_new_data_every(period=UPDADE_INTERVAL):

   while True:
          get_new_data()
          time.sleep(period)

force generator to run in another thread

UPDADE_INTERVAL =2
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
executor = ThreadPoolExecutor(max_workers=1)
executor.submit(get_new_data_every(period=UPDADE_INTERVAL))

run app

but not successful yet, another issue is that the generator will do calculations on GPU and not CPU, and I don’t know whether this is something to be taken into account or no?