From 026eed0e823e295c155a152fadcb97f40735ff39 Mon Sep 17 00:00:00 2001 From: Sebastien Jourdain Date: Wed, 21 Feb 2024 11:46:02 -0700 Subject: [PATCH] docs(discussion): provide example for #440 --- examples/validation/discussion/440.py | 51 +++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 examples/validation/discussion/440.py diff --git a/examples/validation/discussion/440.py b/examples/validation/discussion/440.py new file mode 100644 index 00000000..ee8b90dc --- /dev/null +++ b/examples/validation/discussion/440.py @@ -0,0 +1,51 @@ +import asyncio +from trame.app import get_server +from trame.ui.html import DivLayout +from trame.widgets import trame +from trame.decorators import TrameApp, controller + + +def toggle_node(node, node_id): + if node.get("id") == node_id: + new_visible = (node.get("visible") + 1) % 2 + return {**node, "visible": new_visible} + return node + + +@TrameApp() +class App: + def __init__(self, server=None): + self.server = get_server(server, client_type="vue3") + self.ui = self._build_ui() + + @property + def state(self): + return self.server.state + + def toggle_visibility(self, node_id): + with self.state: + self.state.pipeline = [toggle_node(n, node_id) for n in self.state.pipeline] + + @controller.add_task("on_server_ready") + async def auto_toggle(self, **kwargs): + while True: + for node_id in ["1", "2"]: + await asyncio.sleep(0.1) + self.toggle_visibility(node_id) + + def _build_ui(self): + with DivLayout(self.server) as layout: + trame.GitTree( + sources=( + "pipeline", + [ + {"id": "1", "parent": "0", "visible": 1, "name": "Mesh"}, + {"id": "2", "parent": "1", "visible": 1, "name": "Contour"}, + ], + ), + ) + + +if __name__ == "__main__": + app = App() + app.server.start()