add resampler

This commit is contained in:
Daniel Weschke
2026-04-28 11:29:30 +02:00
parent d4a9b747b3
commit 196cb7d507

23
app.py
View File

@@ -6,7 +6,7 @@ import numpy as np
import time
import datetime
try:
from nicegui import app, ui, events, Client
from nicegui import app, ui, events, Client, run
except ModuleNotFoundError as e:
print('Module `nicegui` not found.')
exit(1)
@@ -16,6 +16,7 @@ try:
import plotly as ply
import plotly.subplots
import local_file_picker
import plotly_resampler as prs
except ModuleNotFoundError as e:
print('Module `plotly` not found.')
exit(1)
@@ -39,7 +40,7 @@ ELM = {
'plotly_light': 'plotly_light',
'plotly_dark': 'plotly_dark',
'update_last': time.time(),
'update_timeout': 1,
'update_timeout': 1.0,
'label_update_last_fmt': 'Last refresh: {last}',
}
@@ -103,7 +104,7 @@ async def pick_file() -> None:
def get_data(filename=ELM['filename']):
data = None
if filename is not None and os.path.isfile(filename):
data = np.genfromtxt(fname=filename, skip_header=1)
data = np.genfromtxt(fname=filename, skip_header=1, max_rows=2)
if len(data) > 0 and len(data[0]) > 6:
names = ['time', 'ts', 'ta', 'tb', 'tc', 'td', 'hp', 'hl']
@@ -117,12 +118,15 @@ def get_data(filename=ELM['filename']):
)
return data
def update_data():
async def update_data():
out = ''
if not app.storage.user['timer'] \
or time.time() - ELM['update_last'] > ELM['update_timeout']:
ELM['update_busy'] = True
filename=ELM['filename']
data_all = get_data(filename)
t1 = time.time()
data_all = await run.cpu_bound(get_data, filename)
t2 = time.time()
data = data_all
if data is not None:
@@ -132,7 +136,7 @@ def update_data():
# delta = datetime.datetime.now() - data['time']
delta = data['time'][-1] - data['time']
data = data[np.where(delta <= datetime.timedelta(hours=time_range))]
print(datetime.datetime.now(), filename, data_all.shape, data.shape)
out += f"{datetime.datetime.now()}, {filename}, {data_all.shape}, {data.shape}"
ELM['label_data_len'].set_text(ELM['label_data_len_fmt'].format(
num=len(data), numa=len(data_all)))
@@ -178,6 +182,8 @@ def update_data():
ELM['label_update_last_fmt'].format(
last=datetime.datetime.fromtimestamp(
ELM['update_last']).strftime("%Y-%m-%dT%H:%M:%S.%f")))
out = f"{t2 - t1:.3f}, {time.time() - t1:.3f}, " + out
print(out)
def update_fig():
ELM['fig'].update_yaxes(
@@ -191,10 +197,9 @@ def update_fig():
def index():
# initialize objects
pio.templates.default = 'plotly_dark'
# ELM['fig'] = pgo.Figure()
ELM['fig'] = ply.subplots.make_subplots(
ELM['fig'] = prs.FigureResampler(pgo.Figure()).set_subplots(
rows=2, cols=1, specs = [[{'t': 0.0}], [{}]], shared_xaxes=True,
vertical_spacing=0.02)
vertical_spacing=0.025)
# render header
ui.context.client.content.classes('h-[100vh]')