Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: button event key error when using %matplotlib widget in jupyter lab #29578

@GanshengT

Description

@GanshengT

Bug summary

Python 3.11
import ipympl
import ipywidgets
import matplotlib
print("ipympl:", ipympl.version)
print("ipywidgets:", ipywidgets.version)
print("matplotlib:", matplotlib.version)

ipympl: 0.9.3
ipywidgets: 8.1.5
matplotlib: 3.10.0

%matplotlib widget

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])

def on_click(event):
print("Click detected at", event.xdata, event.ydata)

fig.canvas.mpl_connect('button_press_event', on_click)
plt.show()

Code for reproduction

%matplotlib widget

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])

def on_click(event):
    print("Click detected at", event.xdata, event.ydata)

fig.canvas.mpl_connect('button_press_event', on_click)
plt.show()

Actual outcome

No printing when a click event occurred

In the info log: ---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
File ~/anaconda3/envs/catan_test/lib/python3.11/site-packages/ipympl/backend_nbagg.py:279, in Canvas._handle_message(self, object, content, buffers)
276 self.manager.handle_json(content)
278 else:
--> 279 self.manager.handle_json(content)

File ~/anaconda3/envs/catan_test/lib/python3.11/site-packages/matplotlib/backends/backend_webagg_core.py:474, in FigureManagerWebAgg.handle_json(self, content)
473 def handle_json(self, content):
--> 474 self.canvas.handle_event(content)

File ~/anaconda3/envs/catan_test/lib/python3.11/site-packages/matplotlib/backends/backend_webagg_core.py:264, in FigureCanvasWebAggCore.handle_event(self, event)
261 e_type = event['type']
262 handler = getattr(self, f'handle_{e_type}',
263 self.handle_unknown_event)
--> 264 return handler(event)

File ~/anaconda3/envs/catan_test/lib/python3.11/site-packages/matplotlib/backends/backend_webagg_core.py:288, in FigureCanvasWebAggCore._handle_mouse(self, event)
286 e_type = event['type']
287 button = event['button'] + 1 # JS numbers off by 1 compared to mpl.
--> 288 buttons = { # JS ordering different compared to mpl.
289 button for button, mask in [
290 (MouseButton.LEFT, 1),
291 (MouseButton.RIGHT, 2),
292 (MouseButton.MIDDLE, 4),
293 (MouseButton.BACK, 8),
294 (MouseButton.FORWARD, 16),
295 ] if event['buttons'] & mask # State after press/release.
296 }
297 modifiers = event['modifiers']
298 guiEvent = event.get('guiEvent')

File ~/anaconda3/envs/catan_test/lib/python3.11/site-packages/matplotlib/backends/backend_webagg_core.py:295, in (.0)
286 e_type = event['type']
287 button = event['button'] + 1 # JS numbers off by 1 compared to mpl.
288 buttons = { # JS ordering different compared to mpl.
289 button for button, mask in [
290 (MouseButton.LEFT, 1),
291 (MouseButton.RIGHT, 2),
292 (MouseButton.MIDDLE, 4),
293 (MouseButton.BACK, 8),
294 (MouseButton.FORWARD, 16),
--> 295 ] if event['buttons'] & mask # State after press/release.
296 }
297 modifiers = event['modifiers']
298 guiEvent = event.get('guiEvent')

KeyError: 'buttons'
12:05:24 PM

KeyError Traceback (most recent call last)
File ~/anaconda3/envs/catan_test/lib/python3.11/site-packages/ipympl/backend_nbagg.py:279, in Canvas._handle_message(self, object, content, buffers)
276 self.manager.handle_json(content)
278 else:
--> 279 self.manager.handle_json(content)

File ~/anaconda3/envs/catan_test/lib/python3.11/site-packages/matplotlib/backends/backend_webagg_core.py:474, in FigureManagerWebAgg.handle_json(self, content)
473 def handle_json(self, content):
--> 474 self.canvas.handle_event(content)

File ~/anaconda3/envs/catan_test/lib/python3.11/site-packages/matplotlib/backends/backend_webagg_core.py:264, in FigureCanvasWebAggCore.handle_event(self, event)
261 e_type = event['type']
262 handler = getattr(self, f'handle_{e_type}',
263 self.handle_unknown_event)
--> 264 return handler(event)

File ~/anaconda3/envs/catan_test/lib/python3.11/site-packages/matplotlib/backends/backend_webagg_core.py:288, in FigureCanvasWebAggCore._handle_mouse(self, event)
286 e_type = event['type']
287 button = event['button'] + 1 # JS numbers off by 1 compared to mpl.
--> 288 buttons = { # JS ordering different compared to mpl.
289 button for button, mask in [
290 (MouseButton.LEFT, 1),
291 (MouseButton.RIGHT, 2),
292 (MouseButton.MIDDLE, 4),
293 (MouseButton.BACK, 8),
294 (MouseButton.FORWARD, 16),
295 ] if event['buttons'] & mask # State after press/release.
296 }
297 modifiers = event['modifiers']
298 guiEvent = event.get('guiEvent')

File ~/anaconda3/envs/catan_test/lib/python3.11/site-packages/matplotlib/backends/backend_webagg_core.py:295, in (.0)
286 e_type = event['type']
287 button = event['button'] + 1 # JS numbers off by 1 compared to mpl.
288 buttons = { # JS ordering different compared to mpl.
289 button for button, mask in [
290 (MouseButton.LEFT, 1),
291 (MouseButton.RIGHT, 2),
292 (MouseButton.MIDDLE, 4),
293 (MouseButton.BACK, 8),
294 (MouseButton.FORWARD, 16),
--> 295 ] if event['buttons'] & mask # State after press/release.
296 }
297 modifiers = event['modifiers']
298 guiEvent = event.get('guiEvent')

KeyError: 'buttons'
12:05:24 PM

Expected outcome

print("Click detected at", event.xdata, event.ydata). This should print out the click coordinates

Additional information

No response

Operating system

Mac 15.1.1 (24B91), M2

Matplotlib Version

matplotlib: 3.10.0

Matplotlib Backend

module://ipympl.backend_nbagg

Python version

Python 3.11.11

Jupyter version

4.3.4

Installation

conda

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions