Action when Background task ends

How do I send a notification, or update the page automatically, when a background task ends? Thank you.

I you want to send an email, the background task itself can send an email right before finishing.

To show a notification on the form you can add a Timer that every second calls a server function and checks if the background task has finished.

Here is some (untested) example code:

The background task could set its status with:

anvil.server.task_state['progress'] = 'Doing this and that'

The function called by the timer:

@anvil.server.callable
def get_status(id):
  task = anvil.server.get_background_task(id)
  if task.is_completed():
    return None
  else:
    return task.get_state()['progress']

The timer:

  def background_check_tick(self, **event_args):
    task_status = anvil.server.call_s('get_status', id=self.task_id)
    if task_status:
      self.task_status_label.text = task_status
    else:
      self.task_status_label.text = 'Done!'
      self.background_check.interval = 0
1 Like

Thank you so much, it is really what I was looking for!