A custom widget for returning mouse and keyboard events to Python

2.0.4 · active · verified Wed Apr 15

ipyevents provides a custom widget for capturing and returning mouse and keyboard events from Jupyter widgets to Python. It enables adding interactive features like keyboard shortcuts, reacting to clicks on images, and handling arbitrary mouse/keyboard events within a Jupyter environment. The current version is 2.0.4, targeting JupyterLab 3 and above. Releases are driven by compatibility with JupyterLab versions and feature additions.

Warnings

Install

Imports

Quickstart

This quickstart creates a Label widget and an Event listener attached to it. It watches for 'click', 'keydown', and 'mouseenter' events, displaying event information in an HTML widget. This demonstrates how to capture browser events and process them in Python.

from ipywidgets import Label, HTML, display
from ipyevents import Event

l = Label('Click or type on me!')
l.layout.border = '2px solid red'
h = HTML('Event info')

d = Event(source=l, watched_events=['click', 'keydown', 'mouseenter'])

def handle_event(event):
    lines = [f'{k}: {v}' for k, v in event.items()]
    content = '<br>'.join(lines)
    h.value = content

d.on_dom_event(handle_event)

display(l, h)

view raw JSON →