From b8a353b9af08688ea2fb986aba682d9d5c43bd3b Mon Sep 17 00:00:00 2001 From: Alex Davies Date: Thu, 7 Nov 2024 09:45:08 -0400 Subject: [PATCH] Moved robot container listing to own tab and method for better composability --- guiTools/spiri_sdk_guitools/sim_drone.py | 59 ++++++++++++++---------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/guiTools/spiri_sdk_guitools/sim_drone.py b/guiTools/spiri_sdk_guitools/sim_drone.py index 1717775..6283820 100644 --- a/guiTools/spiri_sdk_guitools/sim_drone.py +++ b/guiTools/spiri_sdk_guitools/sim_drone.py @@ -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)