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
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
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):
OK = 0
FAILED = 1
UNKNOWN = 2
class System:
def __init__(self, name: str, description: str):
@ -29,10 +46,9 @@ class System:
self.state_verbose = ""
self.last_update = 0
def get_actions(self) -> dict:
def get_actions(self) -> List[Action]:
# to be overridden
# return {'ActionName': callable, ...}
return {}
return []
def _update_state(self):
self.update_state()

View file

@ -4,7 +4,7 @@ import asyncio
import datetime
from typing import List
from .system import System, SystemState
from .system import Action, System, SystemState
from nicegui import ui, html, run
@ -42,8 +42,9 @@ def init_ui(
if t.description != "" or t.state_verbose != "":
ui.separator().style("margin-top: auto;")
with ui.card_actions():
for n, c in actions.items():
ui.button(text=n, on_click=c)
for a in actions:
assert isinstance(a, Action)
ui.button(text=a.name, on_click=a)
elif isinstance(t, str):
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)
from typing import List
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
# define systems
@ -26,11 +27,13 @@ class ExampleSystem(System):
self.state = SystemState.FAILED
self.state_verbose = f"{self.name} is currently stopped."
def get_actions(self) -> dict:
def get_actions(self) -> List[Action]:
actions = []
if self.started:
return {"Stop": self.stop}
actions.append(Action("Stop", self.stop))
else:
return {"Start": self.start}
actions.append(Action("Start", self.start))
return actions
def start(self):
self.state = SystemState.UNKNOWN
@ -56,4 +59,4 @@ systems = [
#
init_ui(systems)
ui.run(show=False, title="Dashboard")
ui.run(show=False, title="Dashboard", port=8000)