Is it possible to run a definition from python with a button?


I would like to know if it’s possible to use a definition from my PyScript code and use it as a function for a button. For example, I have my PyScript:

    def butonclicked():

I would basically like to know if I can use the definition ‘buttonclicked()’ in an HTML button?


from js import document,
from pyodide import create_proxy

def buttonwrapper(_):

ButtonClickEvent = create_proxy(buttonwrapper)

button = document.getElementById("button")
button.addEventListener("click", ButtonClickEvent) 
1 Like

Hello @jts !

Thanks for you response, although, it gives me an error in console. (See attached image)

If you need it, my index code:

Here I’m using this:

On the py-script tag I use:

<def btn_clicked(*args, **kwargs):
  pyscript.write("id_btn", "string to inject")>

On the html:

<button id="id_btn" class="" type="submit" pys-onClick="btn_clicked"> Test </button>

Hope it helps

1 Like

Yep, it does! Thank you so much!!

1 Like