Mouse click events

Just starting to use plotly with Python and trying to figure out how to receive mouse events. So far all the docs I’ve seen seem to imply that the code needs to run on a Jupyter notebook.

Is there no way to receive mouse events with Python alone (I am using VSCode if this matters)? I get it that the plotting is done in a browser. Is there perhaps an alternative non-browser way to plot it that will allow mouse events to be sent back to the python code?

See function “extract_click_coordinates” from this post:

Are you referring to dash? I’m asking because you tagged this topic with Plotly Python

No, I was referring to Plotly with Python. However, if it’s impossible to achieve with Plotly alone and Dash does have this functionality, then I might need to look into Dash as well.

Actually I’m not sure if this is possible in plotly. What do you want to do with the click coordinates?

Here an example in Dash:

I am working on optimizing parameters for trading systems. I want to have the following workflow:

  1. Optimize with various parameters (I wrote the code in python).
  2. Plot the optimization results as a 3D surface (I write the code for this using Plotly Python)
  3. A mouse click on any point on the surface should be received back to the python code, in which I will have a function to do some further processing on the specific parameters chosen.

Essentially I need a way to receive mouse click on figure back into the python code. I think this is a common use case.

This sounds like a Dash app to me.

Another option could be using ìpwidgets but I have never used that nor do we have a lot of questions on that on the forums lately. :thinking:

Thanks, I will look into Dash then. I tried to ran examples with ipwidgets but none worked for me on local python, I guess this requires Jupyter or other notebooks which I really don’t want to use.

Hello @Walrus,

You should be able to do this through regular python and plotly. It will just take more effort.

Id highly recommend using dash as it will be much easier to accomplish.

1 Like

Any tips how to?

To follow up on this, I implemented that in Dash but now encounter another problem.

My app includes a dcc.Graph with a figure containing a 3D surface plot. A click on the plot is received and processed in a callback with Input(‘opt-graph’,‘clickData’).

My issue is that the callback can’t differentiate between clicks meant to merely rotate to surface around and a clicks on a specific point on the plot that meant to fire the callback. The intention is to allow the user to first rotate the plot as they please (and perhaps zoom in and out in the process), and then have them actually select a specific point on the plot and process this choice in the callback.

Is there a way to generate another kind of click to solve that? for example, a double-click, or a right-click? Alternatively, is it possible not to have a “click and drag plot” kind of event not get registered as clickdata event? Or any other way to distinguish between the two?

Hello @Walrus,

You may be able to listen to the layout and make sure that it isnt also being adjusted. If so, then you can ignore the clickData

1 Like

I’ll try that, thanks @jinnyzor