Files
ArchGPUFrontend/src/monitor_example.py
2025-09-18 03:41:48 +02:00

83 lines
3.1 KiB
Python

#!/usr/bin/env python3
"""
Example of using the refactored monitoring classes with NiceGUI's reactive system.
This demonstrates how the bindable dataclasses automatically update the UI.
"""
from nicegui import ui, app
from utils import GPUMonitor, SystemMonitor
# Create monitor instances (bindable dataclasses)
system_monitor = SystemMonitor()
gpu_monitor = GPUMonitor()
app.timer(2.0, system_monitor.update)
app.timer(2.0, gpu_monitor.update)
@ui.page('/')
async def index_page():
"""Example usage of monitoring classes with NiceGUI"""
# Create UI that automatically updates when dataclass fields change
with ui.card().classes('w-full'):
ui.label('System Monitor').classes('text-h4')
# CPU section - binds directly to dataclass fields
with ui.row():
ui.label('CPU:')
ui.label().bind_text_from(system_monitor, 'cpu_percent',
lambda x: f'{x:.1f}%')
ui.label().bind_text_from(system_monitor, 'cpu_model')
# Memory section
with ui.row():
ui.label('Memory:')
ui.label().bind_text_from(system_monitor, 'memory_percent',
lambda x: f'{x:.1f}%')
ui.label().bind_text_from(system_monitor, 'memory_used',
lambda x: f'{x / (1024**3):.1f} GB used')
# Disk section
with ui.row():
ui.label('Disk:')
ui.label().bind_text_from(system_monitor, 'disk_percent',
lambda x: f'{x:.1f}%')
# Process count
with ui.row():
ui.label('Processes:')
ui.label().bind_text_from(system_monitor, 'process_count')
# GPU Monitor section (if available)
if gpu_monitor.available:
with ui.card().classes('w-full mt-4'):
ui.label('GPU Monitor').classes('text-h4')
with ui.row():
ui.label('GPU:')
ui.label().bind_text_from(gpu_monitor, 'gpu_name')
ui.label().bind_text_from(gpu_monitor, 'vendor',
lambda x: f'({x.value})')
with ui.row():
ui.label('Usage:')
ui.label().bind_text_from(gpu_monitor, 'usage',
lambda x: f'{x:.1f}%')
ui.label('Temp:')
ui.label().bind_text_from(gpu_monitor, 'temperature',
lambda x: f'{x:.1f}°C')
with ui.row():
ui.label('Memory:')
ui.label().bind_text_from(gpu_monitor, 'memory_percent',
lambda x: f'{x:.1f}%')
ui.label().bind_text_from(gpu_monitor, 'memory_used',
lambda x: f'({(x / 1024.0):.2f} GB / {(gpu_monitor.memory_total / 1024.0):.2f} GB)')
else:
with ui.card().classes('w-full mt-4'):
ui.label('No GPU detected').classes('text-h4')
if __name__ in {"__main__", "__mp_main__"}:
ui.run(port=8081, title='System Monitor Example')