Force show spinner (opposite of anvil.server.no_loading_indicator)

what a great idea.

Here’s a demo of how to do it.

https://anvil.works/build#clone:QELJIOSD62ZWIULK=BK6RBPA5BK7M76YFSGNUO2MG

the end user code:

  def button_1_click(self, **event_args):
    """This method is called when the button is clicked"""
    with loading_indicator:
      from time import sleep
      sleep(3)

WmXgZH9dpi

the code to make it work

import anvil as _anvil

class _loading_indicator:
  def __enter__(self):
    _anvil.js.call_js('setLoading', True)
    return self
  def __exit__(self, exc_type, exc_value, tb):
    _anvil.js.call_js('setLoading', False)

loading_indicator = _loading_indicator()

# setLoading is the javascript function anvil uses to turn the loading indicator on and off
# I guess it's not part of the docs so can't be relied upon...

You could also extend it to be a decorator for a function if you wanted… but since you request a context manager…

11 Likes