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

Use Plotly.react instead of Plotly.newPlot #2516

Merged
merged 2 commits into from
Feb 5, 2024

Conversation

platinops
Copy link

This is just a refactor, triggered by the discussion in #2505.

It's better to use Plotly.react than Plotly.newPlot as stated in the Plotly docs:

Plotly.react has the same signature as Plotly.newPlot above, and can be used in its place to create a plot, but when called again on the same <div> will update it far more efficiently than Plotly.newPlot), which would destroy and recreate the plot. Plotly.react is as fast as Plotly.restyle/Plotly.relayout documented below.

Test code to verify events are still OK, also after changing the plot config:

from random import random

from nicegui import ui

def get_trace_data() -> dict:
    return {
        "x":[1, 2, 3],
        "y": [random(), random(), random()],
    }

data = [get_trace_data() for _ in range(1)]
config = {"displayModeBar": True}
fig = {
    "data":data,
    "config":config,
}
plot = ui.plotly(fig)
plot = plot.classes('w-full h-96').on('plotly_click', ui.notify)

def add_trace():
    fig["data"] = [*fig["data"], get_trace_data()]
    plot.update()

def toggle_mode_bar():
    fig["config"]["displayModeBar"] = not fig["config"]["displayModeBar"]
    plot.update()

ui.button('Add trace', on_click=add_trace)
ui.button('Toggle mode bar', on_click=toggle_mode_bar)

ui.run(host="0.0.0.0", port=5000)

@bmaranville
Copy link
Contributor

I wondered about this. It would simplify the code a bit.

@falkoschindler falkoschindler added the enhancement New feature or request label Feb 5, 2024
@falkoschindler falkoschindler added this to the 1.4.14 milestone Feb 5, 2024
Copy link
Contributor

@falkoschindler falkoschindler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Thanks, @platinops!
This simplifies things quite a bit. 🙂

@falkoschindler falkoschindler merged commit e4fe519 into zauberzeug:main Feb 5, 2024
1 check passed
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.

None yet

3 participants