Skip to content
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

Re-use plotly plot if config did not change #1393

Merged
merged 2 commits into from
Aug 12, 2023
Merged

Re-use plotly plot if config did not change #1393

merged 2 commits into from
Aug 12, 2023

Conversation

falkoschindler
Copy link
Contributor

@falkoschindler falkoschindler commented Aug 11, 2023

This PR solves #1386 using Plotly.react instead of Plotly.newPlot when the config did not change. This allows using uirevision to keep the user state (zoom level etc.) while updating the data.

The distinction whether the config changed is necessary because Plotly.react behaves badly if we pass options.config with responsive=true being set. But since it doesn't change when only updating data, we can still use the more efficient Plotly.react in these cases.

While being at this, I improved the update mechanism (implement the updated method instead of requiring to call update from the server) and moved the default setting of responsive=true to the client (simplifies the Python code, combines all config trickery in one place).

This test code provides a button to update the data as well as one to update the config:

from random import random

fig = {
    'data': [{'x': [1, 2, 3], 'y': [0.5, 0.5, 0.5]}],
    'layout': {'uirevision': True},
}
with ui.row().classes('bg-red-100 p-2 w-full'):
    plot = ui.plotly(fig).classes('w-full')

def add_trace():
    fig['data'][0]['y'] = [random(), random(), random()]
    plot.update()

def show_display_mode_bar():
    fig['config'] = {'displayModeBar': True}
    plot.update()

ui.button('Update', on_click=add_trace)
ui.button('Show display mode bar', on_click=show_display_mode_bar)

The plot remains responsive (i.e. fills the width of the container when resizing the window) and keeps the user state (e.g. zoom level) when updating the data. Only when the config changes (e.g. displayModeBar) the user state resets because the plot is re-created.

@falkoschindler falkoschindler self-assigned this Aug 11, 2023
@falkoschindler falkoschindler added the enhancement New feature or request label Aug 11, 2023
@falkoschindler falkoschindler added this to the 1.3.10 milestone Aug 11, 2023
@rodja rodja merged commit d5dde58 into main Aug 12, 2023
4 checks passed
@rodja rodja deleted the plotly branch August 12, 2023 03:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants