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)
| 46 | |
| 47 | |
| 48 | def 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 | |
| 74 | def to_js(value, **kw): |
no outgoing calls
no test coverage detected