You cannot directly invoke a Python function from JS : JS is executed in your browser; Python is executed in your server. In order to call a Python function (in your server) from JS (in your browser), you need an interaction from the browser to the server.
Read more