Moved robot container listing to own tab and method for better composability

This commit is contained in:
Alex Davies 2024-11-07 09:45:08 -04:00
parent e42b5f1740
commit b8a353b9af
1 changed files with 34 additions and 25 deletions

View File

@ -66,11 +66,38 @@ class Robot:
self.processes = []
robots.add(self)
async def ui_containers(self, element):
docker_elements = {}
container_status = {}
with element:
while True:
#Poll for data that changes
for container in self.containers():
try:
health = container.attrs['State']['Health']['Status']
except KeyError:
health = "Unknown"
container_status[container] = f"{container.name} {container.status} {health}"
if container not in docker_elements:
docker_elements[container] = ui.element().classes("w-full")
with docker_elements[container]:
ui.label().bind_text(container_status, container)
logelement = ui.expansion("Logs").style('margin: 10px;').classes('w-full')
asyncio.create_task(container_logs(container, logelement))
#Check for containers that have been removed
removed = set(docker_elements.keys()) - set(self.containers())
for container in removed:
self.robot_ui.remove(docker_elements[container])
docker_elements.pop(container)
await asyncio.sleep(1)
async def ui(self, element):
adocker = aiodocker.Docker()
with element:
robot_ui = ui.element().classes("w-full outline p-4")
with robot_ui:
self.robot_ui = ui.element().classes("w-full outline p-4")
with self.robot_ui:
ui.label(f"{self.robot_type}")
ui.label(f"""Sysid: {self.sysid}""")
ui.button("Start", on_click=self.start).classes("m-2")
@ -80,29 +107,11 @@ class Robot:
robots.remove(self)
element.remove(robot_ui)
ui.button("Delete", on_click=delete_robot).classes("m-2")
docker_elements = {}
container_status = {}
while True:
#Poll for data that changes
for container in self.containers():
try:
health = container.attrs['State']['Health']['Status']
except KeyError:
health = "Unknown"
container_status[container] = f"{container.name} {container.status} {health}"
if container not in docker_elements:
docker_elements[container] = ui.element()
with docker_elements[container]:
ui.label().bind_text(container_status, container)
logelement = ui.expansion("Logs").style('margin: 10px;').classes('w-full')
asyncio.create_task(container_logs(container, logelement))
#Check for containers that have been removed
removed = set(docker_elements.keys()) - set(self.containers())
for container in removed:
robot_ui.remove(docker_elements[container])
docker_elements.pop(container)
await asyncio.sleep(1)
with ui.tabs() as tabs:
tab_containers = ui.tab('Containers')
with ui.tab_panels(tabs, value=tab_containers):
tab = ui.tab_panel(tab_containers).classes("w-full")
asyncio.create_task(self.ui_containers(tab))
async def async_stop(self):
return await run.io_bound(self.stop)