Hi,
I’m looking for a way to detect coordinates where user clicked on image.
The idea is that user sees screenshot of desktop machine refreshed every second and has ability to click somewhere on it and expect reaction as if he actually clicked with mouse attached to streamed desktop machine.
I’m aware how to program backend on server side, but I’m stuck with frontend in dash.
Right now I managed to print screenshot every second with html.Img.
I create component with current screenshot with:
How can I get coordinates where user clicked on streamed image? (It may be some other component - it doesn’t have to be html.Img). What I need is information that user clicked point that’s for example in 60% of desktop screen height and 30% of desktop screen width.
It might be possible to do this in a dcc.Graph component. You could put the image in the background and then remove all the lines and ticks as in this example and then add a scatter trace containing a dense grid of transparent points on top of it that you’ll use to determine the x and y coordinates of the click.
Doing it in a Graph component works well to leverage the re_layout events and the graph drawing capabilities.
However, in my use case I need to update the background image every 1 second.
It is not practical to re-create the Graph component every 1 second.
Do you have an idea to just update the image without re-creating the Graph component?
Or another idea of how to get the mouse click coordinates for an image displayed with the html.Img component?
Thanks for the reply! Dash Leaflet and the possibility to have a caching layer sounds great.
That makes me think there might be a way to do caching with a dcc.Store. but I’m not dash-expert enough to imagine how that would work between the client and server side.
I also found that when using the Graph component, there is the fig.update_layout_images(source=...) method that can update the background image in the Figure to avoid re-creating it every second.
However, after more experimenting, it seems that the overhead of (re)creating a go.Figure is small, the main part seems to be serializing the image data itself.
For reducing that, base64 encoding seems to help. Something like this yields visible improvement over just passing the file path string.
In any case lowering the resolution of the image on the server side improves refresh speed the most.
Hope these pointers help somebody!
For me the best solution I found so far is the mouse and keyboard event of ipywidgets: ipyevents
I wish there was the same generic container component for Dash!