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

**URL:** <https://forum.anaconda.com/t/is-it-possible-to-run-a-definition-from-python-with-a-button/21012>\
**Category:** PyScript\
**Created:** [May 8, 2022, 8:29am UTC](https://forum.anaconda.com/t/is-it-possible-to-run-a-definition-from-python-with-a-button/21012 "2022-05-08T08:29:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![WhMonkey](https://avatars.discourse-cdn.com/v4/letter/w/00a5b5/32.png) [@WhMonkey](https://forum.anaconda.com/u/WhMonkey)\
**Post date:** [May 8, 2022, 8:29am UTC](https://forum.anaconda.com/t/is-it-possible-to-run-a-definition-from-python-with-a-button/21012/1 "2022-05-08T08:29:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jts](https://dub1.discourse-cdn.com/anaconda/user_avatar/forum.anaconda.com/jts/32/1272_2.png) [@jts](https://forum.anaconda.com/u/jts)\
**Post date:** [May 9, 2022, 5:52pm UTC](https://forum.anaconda.com/t/is-it-possible-to-run-a-definition-from-python-with-a-button/21012/2 "2022-05-09T17:52:46Z")

</div>

```auto
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) 

```

---

<div class="post-metadata">

**Author:** ![WhMonkey](https://avatars.discourse-cdn.com/v4/letter/w/00a5b5/32.png) [@WhMonkey](https://forum.anaconda.com/u/WhMonkey)\
**Post date:** [May 10, 2022, 2:04pm UTC](https://forum.anaconda.com/t/is-it-possible-to-run-a-definition-from-python-with-a-button/21012/3 "2022-05-10T14:04:38Z")

</div>

Hello @jts !

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

 ![image](https://europe1.discourse-cdn.com/anaconda/original/2X/a/a43b8beb441d20cdd4d6e3cf78c60035136be427.png)

If you need it, my index code:

[https://pastebin.com/KhQ2cg7C](https://pastebin.com/KhQ2cg7C)

---

<div class="post-metadata">

**Author:** ![criarlogins](https://avatars.discourse-cdn.com/v4/letter/c/f5a800/32.png) [@criarlogins](https://forum.anaconda.com/u/criarlogins)\
**Post date:** [May 10, 2022, 5:09pm UTC](https://forum.anaconda.com/t/is-it-possible-to-run-a-definition-from-python-with-a-button/21012/4 "2022-05-10T17:09:57Z")

</div>

Here I’m using this:

On the py-script tag I use:

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

```

On the html:

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

```

Hope it helps

---

<div class="post-metadata">

**Author:** ![WhMonkey](https://avatars.discourse-cdn.com/v4/letter/w/00a5b5/32.png) [@WhMonkey](https://forum.anaconda.com/u/WhMonkey)\
**Post date:** [May 11, 2022, 3:44pm UTC](https://forum.anaconda.com/t/is-it-possible-to-run-a-definition-from-python-with-a-button/21012/5 "2022-05-11T15:44:31Z")

</div>

Yep, it does! Thank you so much!!
