initial commit
This commit is contained in:
193
app.py
Normal file
193
app.py
Normal file
@@ -0,0 +1,193 @@
|
||||
"""
|
||||
|
||||
"""
|
||||
import os
|
||||
import numpy as np
|
||||
import datetime
|
||||
try:
|
||||
from nicegui import app, ui, events, Client
|
||||
except ModuleNotFoundError as e:
|
||||
print('Module `nicegui` not found.')
|
||||
exit(1)
|
||||
try:
|
||||
import plotly.graph_objects as pgo
|
||||
import plotly.io as pio
|
||||
import plotly as ply
|
||||
import plotly.subplots
|
||||
except ModuleNotFoundError as e:
|
||||
print('Module `plotly` not found.')
|
||||
exit(1)
|
||||
|
||||
NAME = 'tempplot'
|
||||
FILENAME = './data/20260420213656.txt'
|
||||
fig = None
|
||||
plot = None
|
||||
label_file = None
|
||||
label_file_fmt = 'tempplot: {filename}'
|
||||
# label_file_fmt = 'tempplot: {os.path.basename(filename)}'
|
||||
|
||||
def main_cli():
|
||||
import argparse
|
||||
parser = argparse.ArgumentParser(
|
||||
description=__doc__,
|
||||
formatter_class=argparse.RawTextHelpFormatter,
|
||||
prefix_chars='-',
|
||||
add_help=False,
|
||||
)
|
||||
parser.add_argument(
|
||||
'--host', type=str, default='', help='default 127.0.0.1')
|
||||
parser.add_argument(
|
||||
'--port', type=str, default='8085', help='default 8080')
|
||||
parser.add_argument(
|
||||
'-v', '--verbose', action='store_true', help='Verbose output')
|
||||
parser.add_argument(
|
||||
'-D', '--debug', action='store_true', help=argparse.SUPPRESS)
|
||||
parser.add_argument(
|
||||
'-h', '--help', action='store_true', help='Show this help message and exit')
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.debug:
|
||||
DEBUG = True
|
||||
print(f'[DEBUG] args: {args}')
|
||||
print(f'[DEBUG] __file__: {__file__}')
|
||||
print(f'[DEBUG] cwd: {os.getcwd()}')
|
||||
print(f'[DEBUG] host: {args.host}')
|
||||
print(f'[DEBUG] port: {args.port}')
|
||||
|
||||
if args.help:
|
||||
parser.print_help()
|
||||
exit(0)
|
||||
|
||||
return args
|
||||
|
||||
|
||||
def np2pgo(x, y, label=None):
|
||||
res = {'x': x, 'y': y}
|
||||
if label is not None:
|
||||
res.update({'name': label})
|
||||
return res
|
||||
|
||||
# def str2date(x):
|
||||
# return datetime.datetime.strptime(x.decode('utf-8'), '%Y%m%d%H%M%S')
|
||||
|
||||
import local_file_picker
|
||||
async def pick_file() -> None:
|
||||
result = await local_file_picker.local_file_picker('./data', multiple=False)
|
||||
# ui.notify(f'You chose {result}')
|
||||
filename = result[-1]
|
||||
update_data(filename)
|
||||
label_file.set_text(label_file_fmt.format(filename=os.path.basename(filename)))
|
||||
|
||||
def get_data(filename=FILENAME):
|
||||
print(f"filename: {filename}")
|
||||
data = np.genfromtxt(fname=filename, skip_header=1)
|
||||
|
||||
if len(data) > 0 and len(data[0]) > 6:
|
||||
names = ['time', 'ts', 'ta', 'tb', 'tc', 'td', 'hp', 'hl']
|
||||
else:
|
||||
names = ['time', 'ts', 'ta', 'tb', 'hp', 'hl']
|
||||
|
||||
data = np.genfromtxt(
|
||||
fname=filename, dtype=None, skip_header=1, converters={
|
||||
0: lambda x: datetime.datetime.strptime(x, '%Y%m%d%H%M%S')},
|
||||
names=names, encoding='utf-8'
|
||||
)
|
||||
print(data.shape)
|
||||
print(data['time'])
|
||||
#data[:, 0] = np.array(data[:, 0], dtype='datetime64')
|
||||
#print(data.time)
|
||||
return data
|
||||
|
||||
def update_data(filename=FILENAME):
|
||||
data = get_data(filename)
|
||||
|
||||
fig.data = []
|
||||
plot.visible = False
|
||||
plot.update()
|
||||
|
||||
fig.add_trace(pgo.Scatter(**np2pgo(data['time'], data['ts'], 'Setp')))
|
||||
fig.add_trace(pgo.Scatter(**np2pgo(data['time'], data['ta'], 'Temp A')))
|
||||
fig.add_trace(pgo.Scatter(**np2pgo(data['time'], data['tb'], 'Temp B')))
|
||||
if len(data) > 0 and len(data[0]) > 6:
|
||||
fig.add_trace(pgo.Scatter(**np2pgo(data['time'], data['tc'], 'Temp C')))
|
||||
fig.add_trace(pgo.Scatter(**np2pgo(data['time'], data['td'], 'Temp D')))
|
||||
|
||||
fig.add_trace(pgo.Scatter(**np2pgo(data['time'], data['hp'], 'Heat')),
|
||||
row=2, col=1)
|
||||
|
||||
plot.visible = True
|
||||
plot.update()
|
||||
|
||||
@ui.page('/')
|
||||
def index():
|
||||
global fig, plot, label_file
|
||||
|
||||
# data = get_data()
|
||||
|
||||
#
|
||||
fig = ply.subplots.make_subplots(rows=2, cols=1)
|
||||
|
||||
#
|
||||
ui.context.client.content.classes('h-[100vh]')
|
||||
ui.add_head_html('''<meta name="viewport", content="width=device-width, initial-scale=1">
|
||||
<meta name="mobile-web-app-capable" content="yes">''')
|
||||
|
||||
dark_mode = ui.dark_mode().bind_value(app.storage.user, 'dark_mode')
|
||||
#ui.button('Dark', on_click=dark.enable)
|
||||
#ui.button('Light', on_click=dark.disable)
|
||||
ui.switch(
|
||||
'Dark mode', on_change=lambda: fig.update_layout(template='plotly_dark' if dark_mode.value else 'plotly_white')
|
||||
).bind_value(app.storage.user, 'dark_mode').props('icon="dark_mode"')
|
||||
print(dark_mode.value)
|
||||
|
||||
ui.colors(primary='#4888c4')
|
||||
|
||||
ui.button('Choose file', on_click=pick_file, icon='folder')
|
||||
|
||||
label_file = ui.label(label_file_fmt.format(filename=os.path.basename(FILENAME)))
|
||||
|
||||
# range_t = ui.range(
|
||||
# min=data['time'][0].timestamp(), max=data['time'][-1].timestamp(),
|
||||
# # on_change=lambda e: fig.update_xaxes(
|
||||
# # range=[str(datetime.datetime.fromtimestamp(e.value["min"])),
|
||||
# # str(datetime.datetime.fromtimestamp(e.value["max"]))]),
|
||||
# )
|
||||
# ui.label().bind_text_from(
|
||||
# range_t, 'value',
|
||||
# backward=lambda v: (
|
||||
# f'min: {datetime.datetime.fromtimestamp(v["min"])}\n\
|
||||
# max: {datetime.datetime.fromtimestamp(v["max"])}'),
|
||||
# ).style('white-space: pre-wrap')
|
||||
|
||||
pio.templates.default = 'plotly_dark'
|
||||
# fig = pgo.Figure()
|
||||
fig.update_layout(
|
||||
height=600,
|
||||
#template='plotly_white'
|
||||
template='plotly_dark' if dark_mode.value else 'plotly_white',
|
||||
)
|
||||
plot = ui.plotly(fig).classes('w-full')
|
||||
|
||||
update_data()
|
||||
|
||||
# print(fig.full_figure_for_development().layout.xaxis.range)
|
||||
# ('2026-04-20 21:36:56', '2026-04-21 10:40:53')
|
||||
# print(fig.full_figure_for_development().layout.xaxis.range[0])
|
||||
# print(type(fig.full_figure_for_development().layout.xaxis.range[0]))
|
||||
|
||||
def main_gui(args):
|
||||
ui.run(
|
||||
host=args.host, port=int(args.port), title=NAME,
|
||||
#dark=True,
|
||||
native=False,
|
||||
show=False, storage_secret='tempview')
|
||||
|
||||
def main(args):
|
||||
return 0
|
||||
|
||||
if __name__ in {'__main__', '__mp_main__'}:
|
||||
args = main_cli()
|
||||
main_gui(args)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user