Comments (10)
Hi @TsaiTung-Chen,
A number of 48000 * 5 datapoints results in a message of 60MB, which causes the browser to struggle quite a bit. Have you tried creating a plain Plotly example (without NiceGUI) with that many points? Does it render more smoothly?
from nicegui.
I just tested with the following HTML code. It is killing the browser. 😄
<html>
<head>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<div id="plot"></div>
<script>
const n = 48000 * 50;
let x = new Array(n);
let y = new Array(n);
for (let i = 0; i < n; i++) {
x[i] = Math.random();
y[i] = Math.random();
}
Plotly.newPlot("plot", [{ x: x, y: y, mode: "markers", type: "scatter" }]);
</script>
</body>
</html>
from nicegui.
Hi @falkoschindler,
Thank you for your immediate reply. I know a dataset of 48000 * 50 samples is really large. But updating the chart with a pair of numpy arrays instead of list
s with the same number of samples works as expected (does not cause all UI elements to hang). In addition, even if I use a pair of list
s, after clicking the force reload button to refresh all UI elements, the loading button and stuck chart relive. So can I say that this framework should have the ability to handle this large dataset but refreshing just does not work properly?
Regarding the plain Plotly
I've tried with the following code, and it works well.
import random
import plotly.graph_objects as go
x = [ float(i) for i in range(48000*50) ]
y = [ random.uniform(-i, i) for i in range(48000*50) ]
fig = go.Figure(go.Scatter(x=x, y=x))
fig.show()
from nicegui.
pandas is better than list.
import random
import asyncio
import pandas as pd
from nicegui import ui, native
import plotly.graph_objects as go
data = pd.DataFrame({
'x':[ float(n) for n in range(48000*50)],
'y':[ random.uniform(-i, i) for i in range(48000*50) ]
})
x = data['x'] # using numpy arrays might avoid ui hanging
y = data['y'] # using numpy arrays might avoid ui hanging
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
ui.run(port=native.find_open_port(), native=True)
from nicegui.
Ok, I think I finally understand:
- Filling the plot with 48000*50 datapoints on button click freezes the UI.
- Reloading the browser tab does render the datapoints. So it doesn't look like a frontend/browser problem.
- Switching to NumPy arrays by removing
.tolist()
causes the plot to render without reloading the browser.
I tested with the following code:
import asyncio
import numpy as np
import plotly.graph_objects as go
from nicegui import ui
fig = go.Figure(data={'type': 'scatter'})
plot = ui.plotly(fig)
async def update_data():
fig.update_traces(x=[], y=[])
plot.update()
await asyncio.sleep(0.1)
x = np.arange(48000*int(n.value))
y = np.random.uniform(-100, 100, 48000*int(n.value))
fig.update_traces(x=x.tolist(), y=y.tolist())
# fig.update_traces(x=x, y=y)
plot.update()
n = ui.number(value=50)
ui.button('Update data', on_click=update_data)
ui.run()
Does anyone have an idea what is going on?
from nicegui.
@falkoschindler I have an observation that may be related to this.
In the code below, if you put n_samples equal to 1000, the click event works fine. If you put it equal to 100000, the ui crashes and loses connection. My guess is that ui.plotly needs work in handling data transfer.
n_samples = 1000
data = numpy.random.rand(n_samples)
histogram = go.Figure(data=[go.Histogram(x=data)])
histogram_ui = ui.plotly(histogram)
histogram_ui.on('plotly_click', ui.notify)
from nicegui.
@mohsenminaei Using your code, I can't reproduce a problem with 100_000
samples. Even 1_000_000
samples are displayed after loading for a few seconds.
from nicegui.
@falkoschindler Sorry I was not clear in my response. As you said the data samples are displayed with no problem. The crash happens when I start clicking on the chart. With 100k data, usually after clicking 5-6 times the app crashes. I recorded my page in the video below with this code (when I move mouse cursor in a zig zag way, I'm clicking on the chart):
import numpy as np
from nicegui import ui
from plotly import graph_objects as go
n_samples = 100000
data = np.random.rand(n_samples)
histogram = go.Figure(data=[go.Histogram(x=data)])
histogram_ui = ui.plotly(histogram)
histogram_ui.on('plotly_click', ui.notify)
ui.run()
NiceGUI.mp4
from nicegui.
@falkoschindler - An issue I've been experiencing with plotly events on nicegui just brought me here and may be related. The returned event object contains a complete copy of all of the data in the plot for each point returned, at least for clicking and hover events. If this complete copy is serialized to text from the JavaScript side maybe that is the hang up for large data sets? Selection events that contain multiple points appear even worse since each point selected contains its own complete copy of the data in the plot giving n x m scaling for the serialized object size. The only exploration I've done to date is logging to the console to inspect the returned object (for example, replacing ui.notify
with lambda x: print(x.args)
in @mohsenminaei 's code above).
from nicegui.
@mohsenminaei @krashdifferent Yes, point click events can cause a huge amount of data to be sent back and forth. You can limit it by specifying the event arguments like so:
histogram_ui.on('plotly_click', ui.notify, args=['event'])
I think the original issue is unrelated to user events. It's still unclear to me why updating traces with lists rather than NumPy arrays causes the UI to hang.
from nicegui.
Related Issues (20)
- ui.number displayed number format not maintained HOT 2
- Tooltip not working with ui.upload component HOT 2
- ui.codemirror not updating when the binded dictionary changes HOT 3
- 3D objects get stuck in scene when reloading. HOT 1
- ui.select() dropdown width unexpected behaviour
- Reloading is very slow HOT 10
- `True (error) or False (no error)` in `Input validation` should be `True (no error) or False (error)` HOT 1
- How to cut startup time in half while native=True. HOT 2
- [WinError 10013] at start (Problem binding to port 80?) HOT 5
- Update `fastapi` to 0.111.1 HOT 2
- unexpected package issue HOT 2
- ui.state in an instance method HOT 5
- Aggrid upgrade. v30 to v32 HOT 6
- Quasar components in tables with Python loops HOT 1
- Packed exe file gave "internal server error" when ran HOT 2
- ui.download doesnt' work in native mode HOT 11
- nicegui does not support the ability to IPv6 HOT 1
- Can't the ui.link change the color of the font HOT 1
- Unpredictable Behavior of `ui.textarea` with Over a Million Characters HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nicegui.