nicegui: Updating Ploty chart with a large dataset causes the UI to hang
Description
I’m trying to plot some audio data with a sample rate of 48000 Hz. Updating the Plotly chart with the large dataset causes the whole NiceGUI to hang. I’m not quite sure how to reproduce this issue because same code sometimes works but sometimes does not.
Minimal code
Here is my minimal code, hope someone knows what’s going on.
import random
import asyncio
from nicegui import ui, native
import plotly.graph_objects as go
fig = go.Figure(
data={"type": 'scatter'},
layout={"margin": dict(l=20, r=20, t=20, b=20)}
)
plot = ui.plotly(fig)
async def update_data():
update_bt.props('loading') # update button
try:
# Clear data
fig.update_traces(x=[], y=[])
plot.update()
await asyncio.sleep(1) # refresh UI
# Update plot data
fig.update_traces(x=x, y=y)
plot.update()
finally:
update_bt.props(remove='loading') # update button
print('The Plotly chart should have been refreshed') # the ploty chart does not be refreshed after this line
update_bt = ui.button('Update data', on_click=update_data)
ui.button('Force reload', on_click=ui.navigate.reload) # force reload always works
# 50-sec audio data with 48000 Hz sample rate
x = [ float(n) for n in range(48000*50) ] # using numpy arrays might avoid ui hanging
y = [ random.uniform(-i, i) for i in x ] # using numpy arrays might avoid ui hanging
ui.run(port=native.find_open_port(), native=True)
I’ve added a force reload button at the bottom. If the plotly chart does not be refreshed after the update_data returns, clicking the force reload button always refreshes all UI elements.
I’ve tried using dict instead of plotly.graph_objects and the results are same.
My hardware
M2 Pro Mac Mini macOS 14.5
Python packages
python 3.10 nicegui==1.4.29 pywebview==5.1 plotly==5.22.0
Thank you for any help you can provide.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 16 (8 by maintainers)
@falkoschindler hopefully related, but is https://github.com/predict-idlab/plotly-resampler supported by ui.plotly? It handles large datasets by resampling the waveform and only showing a small subset of samples depending on the zoom. I tried it, and it works with Dash for 1M points for example, but not with ui.plotly when I zoom. The callback part when zooming seems to not be supported yet.
IMHO, letting NiceGUI support the callback part of zooming or directly embedding plotly-resampler into NiceGUI may be good choices.