Skip to content

Commit

Permalink
docs(discussion): provide example for #440
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Feb 21, 2024
1 parent 2e8a8cb commit 026eed0
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions examples/validation/discussion/440.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 026eed0

Please sign in to comment.