Decorators for PyScript #1133
marimeireles
started this conversation in
Proposals
Replies: 4 comments
-
I rather like the @handler decorator, of all the event syntax it is the most productive and is in pure python. It provides a single line that proxies, handles event data, and subscribes via addEventListener. |
Beta Was this translation helpful? Give feedback.
0 replies
-
@handler(event="click", button )
def button_click(*e):
js.console.log("button_click called with *event")
js.console.log( e[0] )
@handler(event="mousemove", element=Element("empty_button").element )
def button_mousemove(*e):
js.console.log("button_mousemove called with *event")
js.console.log( e[0] ) |
Beta Was this translation helpful? Give feedback.
0 replies
-
Also allows us to get deeper into the addEventListener api... addEventListener(type, listener)
addEventListener(type, listener, {once, capture, passive, signal}) |
Beta Was this translation helpful? Give feedback.
0 replies
-
The one item I am concerned about is turning the event subscription on/off. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi!
This is a collaboration of Ted and I.
Thinking about how we can make the event API better and give broader interactivity with JS objs to pyscript users, we'd like to propose the use of handlers. Creating a JS proxy over a Python function that interacts with a JS obj allows us to return the attributes of this obj. Here's some code for better understanding 😊:
In the python side we define a handler decorator that will take in python functions:
Looking into the JS side:
First we add a setter and a getter for the function that will be proxied:
Now we need to define the proxy function itself that will give us the ability to return the event data or other data the developer decides to pass in:
So that's essentially the PyScript side implementation. The use would be something like this:
Which will return:
Besides that, there's also the possibility to make handlers an even more flexible API allowing users to define their own event interactions:
Allowing the developers to write code like:
I think this is it! Let me know @tedpatrick if I made a fair point to your ideas and please complement or correct me wherever I was mislead. If folks have any inputs on this, happy to hear!
Beta Was this translation helpful? Give feedback.
All reactions