Skip to content

Double-click events are not recognised in Jupyter notebook #18404

Closed

Description

@nvaytet

Bug report

Bug summary

Using the first example from the events documentation detects double-click events properly in the Qt backend, but when used in a Jupyter notebook, the two clicks are recognised as two separate single click events.

Code for reproduction

I've adapted the code to show the contents of the event as the figure title instead of a print statement, because the output of print statements inside functions triggered by events disappear in Jupyter notebooks.

import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
ax.plot(np.random.rand(10))

def onclick(event):
    ax.set_title('%s click: button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
          ('double' if event.dblclick else 'single', event.button,
           event.x, event.y, event.xdata, event.ydata))

cid = fig.canvas.mpl_connect('button_press_event', onclick)

Actual outcome

The title always says single-click, no matter how fast I do my double-clicking.

Screenshot at 2020-09-04 08-02-20

The double click events are properly captured when using the standard Qt window renderer.
I have tried both the default %matplotlib notebook backend and the ipympl widget backend, and they show identical behaviour.

Matplotlib version

  • Operating system: Ubuntu 20.04
  • Matplotlib version: 3.2.2
  • Matplotlib backend (print(matplotlib.get_backend())): nbAgg, ipympl
  • Python version: 3.7.6
  • Jupyter version (if applicable): jupyter_client=6.1.6, jupyter_core=4.6.3, notebook=6.0.3
  • Other libraries:

Installed in a conda env from conda-forge.

Activity

  1. added this to the v3.4.0 milestone on Sep 4, 2020
  2. tacaswell commented on Sep 5, 2020

    @tacaswell
    Member

    In the desktop backends we rely on the framework to detect the double click, hopefully this fix is as simple as getting the js to do the same for us.

  3. added
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
    on Sep 5, 2020
  4. tacaswell commented on Sep 5, 2020

    @tacaswell
    Member

    Labeled as good first issue as the changes should be relatively localized (as it only related to how we pass mouse events from the js frontend back to the Python side), but medium difficulty as it will require understanding some of the internals of our event system (to fabricate the correct Event on the python side), the protocol we use between the front end and the python process, and working in both Python and JS.

    I think each of these task is well contained and tractable for someone who is comfortable working in both js and python.

  5. ianhi commented on Sep 6, 2020

    @ianhi
    Contributor

    Ipympl has a different javascript setup than nbagg so this will also need to be fixed there. I opened matplotlib/ipympl#261 with a quote of the original post in this issue.

    I've adapted the code to show the contents of the event as the figure title instead of a print statement, because the output of print statements inside functions triggered by events disappear in Jupyter notebooks.

    In jupyterlab you can see them by opening the Log Console and setting the level to info:
    image
    and then you can see the events in the log console:
    image

    You can also use an ipywidgets.Output to capture the output and display it.

    import matplotlib.pyplot as plt
    import numpy as np
    import ipywidgets as widgets
    fig, ax = plt.subplots()
    ax.plot(np.random.rand(10))
    out = widgets.Output()
    
    @out.capture()
    def onclick(event):
        print(event)
        ax.set_title('%s click: button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
              ('double' if event.dblclick else 'single', event.button,
               event.x, event.y, event.xdata, event.ydata))
    display(out)
    cid = fig.canvas.mpl_connect('button_press_event', onclick)
  6. nvaytet commented on Sep 7, 2020

    @nvaytet
    Author

    @ianhi nice! I didn't know about the Log Console trick.

    Another workaround is to use os.write, as I suggested in this other issue: jupyter/notebook#244 (comment)

  7. tacaswell commented on Sep 7, 2020

    @tacaswell
    Member

    @hershd23 It is "up for grabs" until someone starts working on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesGUI: nbagg🌱 Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions