|
1 | | -from trame.app import get_server |
| 1 | +#!/usr/bin/env -S uv run --script |
| 2 | +# |
| 3 | +# /// script |
| 4 | +# requires-python = ">=3.10" |
| 5 | +# dependencies = [ |
| 6 | +# "trame", |
| 7 | +# ] |
| 8 | +# /// |
| 9 | +from trame.app import TrameApp |
| 10 | +from trame.decorators import controller |
2 | 11 | from trame.ui.html import DivLayout |
3 | 12 | from trame.widgets import html |
4 | 13 |
|
5 | | -# ----------------------------------------------------------------------------- |
6 | | -# Trame app |
7 | | -# ----------------------------------------------------------------------------- |
8 | 14 |
|
9 | | -server = get_server() |
10 | | -state, ctrl = server.state, server.controller |
| 15 | +class Events(TrameApp): |
| 16 | + def __init__(self, server=None): |
| 17 | + super().__init__(server) |
11 | 18 |
|
12 | | -# ----------------------------------------------------------------------------- |
13 | | -# State setup |
14 | | -# ----------------------------------------------------------------------------- |
| 19 | + # setup state |
| 20 | + self.state.a = 1 |
15 | 21 |
|
16 | | -state.a = 1 |
| 22 | + # build ui |
| 23 | + self._build_ui() |
17 | 24 |
|
18 | | -# ----------------------------------------------------------------------------- |
19 | | -# Methods call |
20 | | -# ----------------------------------------------------------------------------- |
| 25 | + # Can be defined after usage |
| 26 | + self.ctrl.alias_3 = self.method_4 |
21 | 27 |
|
| 28 | + def _build_ui(self): |
| 29 | + with DivLayout(self.server) as self.ui: |
| 30 | + html.Div( |
| 31 | + "State a={{ a }}", |
| 32 | + style="padding: 20px; margin: 20px; border: solid 1px #333;", |
| 33 | + ) |
| 34 | + html.Button("Method 1", click=self.method_1) |
| 35 | + html.Button("Method 2", click=(self.method_2, "['hello', 'world']")) |
| 36 | + html.Button("Method 3", click=(self.method_3, "[1, 2]", "{ x: 3, y: 4 }")) |
| 37 | + html.Button("alias_1", click=(self.ctrl.alias_1, "[2]", "{ z: 4 }")) |
| 38 | + html.Button("alias_2", click=(self.ctrl.alias_2, "[3]", "{ z: 5 }")) |
| 39 | + html.Button("alias_3", click=(self.ctrl.alias_3, "[4]", "{ z: 6 }")) |
| 40 | + html.Button("a+", click="a+=1") |
22 | 41 |
|
23 | | -@ctrl.set("alias_1") |
24 | | -def method_1(*args, **kwargs): |
25 | | - print(f"Server: method_1 {args=} {kwargs=}") |
26 | | - state.a += 1 |
| 42 | + @controller.set("alias_1") |
| 43 | + def method_1(self, *args, **kwargs): |
| 44 | + print(f"Server: method_1 {args=} {kwargs=}") |
| 45 | + self.state.a += 1 |
27 | 46 |
|
| 47 | + @controller.add("alias_2") |
| 48 | + def method_2(self, *args, **kwargs): |
| 49 | + print(f"Server: method_2 {args=} {kwargs=}") |
| 50 | + self.state.a += 2 |
28 | 51 |
|
29 | | -@ctrl.add("alias_2") |
30 | | -def method_2(*args, **kwargs): |
31 | | - print(f"Server: method_2 {args=} {kwargs=}") |
32 | | - state.a += 2 |
| 52 | + @controller.add("alias_2") |
| 53 | + def method_3(self, *args, **kwargs): |
| 54 | + print(f"Server: method_3 {args=} {kwargs=}") |
| 55 | + self.state.a += 3 |
33 | 56 |
|
| 57 | + def method_4(self, *args, **kwargs): |
| 58 | + print(f"Server: method_4 {args=} {kwargs=}") |
| 59 | + self.state.a += 10 |
34 | 60 |
|
35 | | -@ctrl.add("alias_2") |
36 | | -def method_3(*args, **kwargs): |
37 | | - print(f"Server: method_3 {args=} {kwargs=}") |
38 | | - state.a += 3 |
39 | 61 |
|
| 62 | +def main(): |
| 63 | + app = Events() |
| 64 | + app.server.start() |
40 | 65 |
|
41 | | -def method_4(*args, **kwargs): |
42 | | - print(f"Server: method_4 {args=} {kwargs=}") |
43 | | - state.a += 10 |
44 | | - |
45 | | - |
46 | | -# ----------------------------------------------------------------------------- |
47 | | -# UI setup |
48 | | -# ----------------------------------------------------------------------------- |
49 | | - |
50 | | -with DivLayout(server): |
51 | | - html.Div( |
52 | | - "State a={{ a }}", style="padding: 20px; margin: 20px; border: solid 1px #333;" |
53 | | - ) |
54 | | - html.Button("Method 1", click=method_1) |
55 | | - html.Button("Method 2", click=(method_2, "['hello', 'world']")) |
56 | | - html.Button("Method 3", click=(method_3, "[1, 2]", "{ x: 3, y: 4 }")) |
57 | | - html.Button("alias_1", click=(ctrl.alias_1, "[2]", "{ z: 4 }")) |
58 | | - html.Button("alias_2", click=(ctrl.alias_2, "[3]", "{ z: 5 }")) |
59 | | - html.Button("alias_3", click=(ctrl.alias_3, "[4]", "{ z: 6 }")) |
60 | | - html.Button("a+", click="a+=1") |
61 | | - |
62 | | - |
63 | | -# Can be defined after usage |
64 | | -ctrl.alias_3 = method_4 |
65 | | - |
66 | | -# ----------------------------------------------------------------------------- |
67 | | -# start server |
68 | | -# ----------------------------------------------------------------------------- |
69 | 66 |
|
70 | 67 | if __name__ == "__main__": |
71 | | - server.start() |
| 68 | + main() |
0 commit comments