-
-
Notifications
You must be signed in to change notification settings - Fork 485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Register plotly event handlers only once, at mount #2505
Register plotly event handlers only once, at mount #2505
Conversation
register event listeners only once in plotly, rather than on every update
I think this also fixes #2435, thanks for the PR! |
This may not be quite right... it looks like when calling |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this pull request, @bmaranville!
I looks like it fixes a really st*pid mistake. 😄 Of course, we shouldn't re-assign event handlers on every update!
But why do you think there's something wrong? Do you have an example? It tested with the following code and it seems to work nicely:
fig = go.Figure()
plot = ui.plotly(fig).classes('w-full h-96').on('plotly_click', ui.notify)
def add_trace():
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[random(), random(), random()]))
plot.update()
ui.button('Add trace', on_click=add_trace)
What I forgot is that changing the config causes newPlot to be triggered on EDIT: I also found that the event listeners were unregistered by toggling the visibility of the ui.plotly element (using bind_visibility_from) - I don't know enough about how the visibility attributes work to know why that is happening. |
Well, I still can't reproduce But I can confirm that toggling the visibility stops events from firing. 😕 |
I don't think your code above triggers another newPlot after the first one - I think the only way to do that is to use the from nicegui import ui
from numpy.random import random
import plotly.graph_objects as go
fig = { "data": [], "config": {} }
plot = ui.plotly(fig).classes('w-full h-96').on('plotly_click', ui.notify)
def add_trace():
fig['data'].append(go.Scatter(x=[1, 2, 3], y=[random(), random(), random()]).to_plotly_json())
plot.update()
def add_drawrect():
fig['config']['modeBarButtonsToAdd'] = ['drawrect']
plot.update()
ui.button('Add trace', on_click=add_trace)
ui.button('Enable drawrect', on_click=add_drawrect)
ui.run() |
Oh right, thanks! That makes sense. |
I created issue #2519 to investigate the problem with toggled visibility. |
All the event handlers in plotly were being registered on every update, which was causing a lot of issues. Event handlers should only be registered once. This PR moves the registration of the event handlers to a one-time operation in the
mounted()
function.Fixes #2504, I think