MCPcopy Index your code
hub / github.com/pyscript/pyscript / create_proxy

Function create_proxy

core/src/stdlib/pyscript/ffi.py:48–71  ·  view source on GitHub ↗

Create a persistent JavaScript proxy of a Python function. This proxy allows JavaScript code to call the Python function seamlessly, maintaining the correct context and argument handling. This is especially useful when passing Python functions as callbacks to JavaScript APIs (

(func)

Source from the content-addressed store, hash-verified

46
47
48def create_proxy(func):
49 """
50 Create a persistent JavaScript proxy of a Python function.
51
52 This proxy allows JavaScript code to call the Python function
53 seamlessly, maintaining the correct context and argument handling.
54
55 This is especially useful when passing Python functions as callbacks
56 to JavaScript APIs (without `create_proxy`, the function would be
57 garbage collected after the declaration of the callback).
58
59 ```python
60 from pyscript import ffi
61 from pyscript import document
62
63 my_button = document.getElementById("my-button")
64
65 def py_callback(x):
66 print(f"Callback called with {x}")
67
68 my_button.addEventListener("click", ffi.create_proxy(py_callback))
69 ```
70 """
71 return _cp(func)
72
73
74def to_js(value, **kw):

Callers 3

decoratorFunction · 0.90
get_eventMethod · 0.90
_attach_event_handlerFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected