Add Action class to allow more flexible actions, update example

This commit is contained in:
ChaoticByte 2025-02-27 20:15:32 +01:00
parent f3cdff4d38
commit 60886b4d0f
No known key found for this signature in database
3 changed files with 34 additions and 14 deletions

View file

@ -9,17 +9,34 @@ import subprocess
import time import time
from enum import Enum from enum import Enum
from typing import Tuple from typing import Tuple, List
import requests import requests, urllib3
# don't need the warning, ssl verification needs to be disabled explicitly
urllib3.disable_warnings(category=urllib3.connectionpool.InsecureRequestWarning)
# base classes and types and stuff # base classes and types and stuff
class Action:
def __init__(self, name: str, c: callable, *args, **kwargs):
self.name = name
self.c = c
self.args = args
self.kwargs = kwargs
def __call__(self):
self.c(*self.args, **self.kwargs)
class SystemState(Enum): class SystemState(Enum):
OK = 0 OK = 0
FAILED = 1 FAILED = 1
UNKNOWN = 2 UNKNOWN = 2
class System: class System:
def __init__(self, name: str, description: str): def __init__(self, name: str, description: str):
@ -29,10 +46,9 @@ class System:
self.state_verbose = "" self.state_verbose = ""
self.last_update = 0 self.last_update = 0
def get_actions(self) -> dict: def get_actions(self) -> List[Action]:
# to be overridden # to be overridden
# return {'ActionName': callable, ...} return []
return {}
def _update_state(self): def _update_state(self):
self.update_state() self.update_state()

View file

@ -4,7 +4,7 @@ import asyncio
import datetime import datetime
from typing import List from typing import List
from .system import System, SystemState from .system import Action, System, SystemState
from nicegui import ui, html, run from nicegui import ui, html, run
@ -42,8 +42,9 @@ def init_ui(
if t.description != "" or t.state_verbose != "": if t.description != "" or t.state_verbose != "":
ui.separator().style("margin-top: auto;") ui.separator().style("margin-top: auto;")
with ui.card_actions(): with ui.card_actions():
for n, c in actions.items(): for a in actions:
ui.button(text=n, on_click=c) assert isinstance(a, Action)
ui.button(text=a.name, on_click=a)
elif isinstance(t, str): elif isinstance(t, str):
ui.label(t).classes("text-2xl textmedium w-full text-center").style("margin-top: 1.5rem; margin-bottom: .5rem") ui.label(t).classes("text-2xl textmedium w-full text-center").style("margin-top: 1.5rem; margin-bottom: .5rem")

View file

@ -1,12 +1,13 @@
# Copyright (c) 2025, Julian Müller (ChaoticByte) # Copyright (c) 2025, Julian Müller (ChaoticByte)
from typing import List
from nicegui import ui from nicegui import ui
from dashboard.system import HTTPServer, System, SystemState from dashboard.system import Action, HTTPServer, System, SystemState
from dashboard.ui import init_ui from dashboard.ui import init_ui
# define systems # define systems
@ -26,11 +27,13 @@ class ExampleSystem(System):
self.state = SystemState.FAILED self.state = SystemState.FAILED
self.state_verbose = f"{self.name} is currently stopped." self.state_verbose = f"{self.name} is currently stopped."
def get_actions(self) -> dict: def get_actions(self) -> List[Action]:
actions = []
if self.started: if self.started:
return {"Stop": self.stop} actions.append(Action("Stop", self.stop))
else: else:
return {"Start": self.start} actions.append(Action("Start", self.start))
return actions
def start(self): def start(self):
self.state = SystemState.UNKNOWN self.state = SystemState.UNKNOWN
@ -56,4 +59,4 @@ systems = [
# #
init_ui(systems) init_ui(systems)
ui.run(show=False, title="Dashboard") ui.run(show=False, title="Dashboard", port=8000)