Anvil Community Forum

Generic Offline Mode Handler Logic

Nothing spectacular, but I thought I’d share it anyway.

Basically a higher order function that you can wrap arbitrary functions in so offline mode cases are handled on the form side.

Plug your functions to handle in here.

  def try_action(self, function_to_try):
    except anvil.server.AppOfflineError:
      alert("No network connection. Check your connection and try again later!")

For example here’s an event handler for a click event:

  def button_save_to_database_click(self, **event_args):

And the base function it is pointing to:

  def save_to_database(self, **event_args):
    new_node = self.node_container.get_components()[0]
    sample_tree = self.load_data(new_node)'upsert_hierarchy', sample_tree)

Very nice!

I like the simplicity of the structure! You could even setup a timer in the except block to listen for a network reconnect to try the action again too.

And if you added *args as one of the arguments of try_action, you could then have functions with arguments passed to them. :grinning_face_with_smiling_eyes: