Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Pyscript python function in javascript?

I have a Javascript code and wanted to implement python functions using Pyscript.

Simple Example:

let's say I have a function:

<py-script>
def addOne(a):
    value = int(a) + 1
    return(value)
</py-script>

and say I want to use this function in javascript:

<script>
let compute = addOne(12)
</script>

The issue here is that it will say "addOne()" function is not defined. I was wondering if there was a way to use pyscript python functions inside javascript?

like image 759
Hanbin Go Avatar asked Aug 14 '26 07:08

Hanbin Go


1 Answers

PyScript use Pyodide for translating objects between JavaScript and Python. Such cross-call needs a proxy of the Python function in the JS scope. So, if you create a proxy of the Python function, you can set this proxy as a callback in an event listener in JS. See https://pyodide.org/en/stable/usage/type-conversions.html#call-js-from-py

Well, you cannot set arbitrary arguments to be passed in a JS event. However, the main point of calling Python is to handle some event, isn't it? E.g. you want to do something in Python when a button is clicked. If so, the event handler (your Python function through the proxy) gets the PointerEvent. Its currentTarget property tells you the clicked DOM element. By the attributes of the element, you can practically pass over any data you want.

<py-script>
import pyodide
import js

class myClass():
    def __init__(self):
        self.add_one_proxy = pyodide.create_proxy(self.add_one)
        js.document.getElementById('mybutton').addEventListener("click", self.add_one_proxy)
    
    def add_one(self, pointerEventObj):
        """Callback from JS when a button is clicked"""
        btn_elem = pointerEventObj.currentTarget
        data = btn_elem.getAttribute('data')
        js.document.getElementById('resultdiv').innerHTML = str(int(data)+1)
    
    def shutdown(self):
        self.add_one_proxy.destroy()

klass = myClass()
# Make sure in the program to keep holding a reference of klass ...
# do the whole program here
# When all done, call klass.shutdown()
</py-script>

<body>
    <button id="mybutton" data="12" />
    <div id="resultdiv">calculated data will come here on button click</div>
</body>
like image 132
Netcreator Avatar answered Aug 16 '26 00:08

Netcreator



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!