Hi, there I’m trying to log a user out and update their online status in my table to false whenever they close the tab. I have the javascript event listener for when someone closes the tab but how would I call a python function from that event listener
Event Listener:
window.addEventListener('beforeunload', function (e) {
var exited = True;
});
Code Sample:
#logout code to run on close
user=anvil.users.get_user(allow_remembered=True)
anvil.users.logout()
anvil.server.call('sign_out',user['email'])
open_form('Main')
#Server code
@anvil.server.callable
def sign_out(target):
app_tables.users.get(email=target).update(online=False)
There’s a section in the docs that’s hard to find (it doesn’t come up when you search for Javascript) that talks about calling Python functions from Javascript: Anvil Docs | HTML Forms
I’m using a python function as a JavaScript callback for a JavaScript event listener.
When a JavaScript event fires and calls the callback it passes the event to the callback. So e is a JavaScript event. I don’t use it but I do need the argument otherwise python throws a TypeError for wrong number of arguments.
If I’m using python functions as callbacks in other libraries I might use *args if I don’t know how many args the JavaScript library will give me.