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

Hello!

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:

<py-script>
    def butonclicked():
        print("hello")
</py-script>

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

Thanks,
WhMonkey

from js import document,
from pyodide import create_proxy


def buttonwrapper(_):
    DoStuff(args)
    pass


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:

https://pastebin.com/KhQ2cg7C

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

2 Likes

Yep, it does! Thank you so much!!

1 Like