mwcraig / ipyevents

A custom widget for returning mouse and keyboard events to Python. Documentation:
https://ipyevents.readthedocs.io/en/latest/index.html
BSD 3-Clause "New" or "Revised" License
112 stars 27 forks source link

add support for event target information #51 #62

Closed lordvlad closed 3 years ago

lordvlad commented 3 years ago

Quick and easy fix for #51

For now, the event object has another dictionary added at the key target, containing (for now) only the tag name, id and class name of the target element.

jifcon commented 3 years ago

Hi lordvlad,

Do you have an example on how to use the target feature? I'm looking for a way to know which element is calling the event (the idea is to have some icons and the same method i.e. on_click and perform some actions depending on the element that is calling the on_click method).

Thanks++!

lordvlad commented 3 years ago

this feature is not merged yet, but once merged, you could do

    from ipywidgets import Label, HTML, HBox, Image, VBox, Box, HBox
    from ipyevents import Event 
    from IPython.display import display

    l = HTML('''
        <button id=eeny>eeny</button>
        <button id=meeny>meeny</button>
        <button id=miny>miny</button>
        <button id=moe>moe</button>
    ''')

    h = HTML('Event info')
    d = Event(source=l, watched_events=['click'])

    def handle_event(event):
        h.value = "You clicked %s" % event["target"]["id"]

    d.on_dom_event(handle_event)

    display(l, h)
lordvlad commented 3 years ago

bump

mwcraig commented 3 years ago

@lordvlad -- sorry it took roughly forever to respond to this. I added some explanation to the demo notebook -- does that look good to you?

lordvlad commented 3 years ago

looks great! thanks

mwcraig commented 3 years ago

Thanks, @lordvlad, merging -- I should have this released by Friday.

mwcraig commented 3 years ago

@lordvlad -- this has been released in 0.9.0. I'm planning to release the first stable version, 2.0.0, tomorrow.

0.9.0 supports jlab 2 and 3. The stable release will support only jlab3 and higher.