|
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 |
2 | 10 | from trame.ui.html import DivLayout |
3 | 11 | from trame.widgets import html |
4 | 12 |
|
5 | | -# ----------------------------------------------------------------------------- |
6 | | -# Trame app |
7 | | -# ----------------------------------------------------------------------------- |
8 | 13 |
|
9 | | -server = get_server() |
| 14 | +class DynamicLayout(TrameApp): |
| 15 | + def __init__(self, server=None): |
| 16 | + super().__init__(server) |
| 17 | + self.line_count = 1 |
| 18 | + self._build_ui() |
10 | 19 |
|
11 | | -# ----------------------------------------------------------------------------- |
12 | | -# UI setup |
13 | | -# ----------------------------------------------------------------------------- |
| 20 | + def add_line(self): |
| 21 | + with self.ui: |
| 22 | + html.Div(f"Line: {self.line_count}") |
| 23 | + self.line_count += 1 |
14 | 24 |
|
15 | | -line_count = 1 |
| 25 | + def update_first_line(self): |
| 26 | + with self.server.ui.first_line: |
| 27 | + self.server.ui.first_line.clear() |
| 28 | + html.Div(f"First line: {self.line_count}") |
| 29 | + self.line_count += 1 |
16 | 30 |
|
| 31 | + def _build_ui(self): |
| 32 | + with DivLayout(self.server) as self.ui: |
| 33 | + self.server.ui.first_line(self.ui) # Insert place holder |
17 | 34 |
|
18 | | -def update_first_line(): |
19 | | - global line_count |
20 | | - with server.ui.first_line: |
21 | | - server.ui.first_line.clear() |
22 | | - html.Div(f"First line: {line_count}") |
23 | | - line_count += 1 |
| 35 | + html.Button("Add line", click=self.add_line) |
| 36 | + html.Button("Update first line", click=self.update_first_line) |
24 | 37 |
|
25 | 38 |
|
26 | | -# Start with some UI to control a |
27 | | -with DivLayout(server) as layout: |
28 | | - server.ui.first_line(layout) # Insert place holder |
| 39 | +def main(): |
| 40 | + app = DynamicLayout() |
| 41 | + app.server.start() |
29 | 42 |
|
30 | | - def add_line(): |
31 | | - global line_count |
32 | | - with layout: |
33 | | - html.Div(f"Line: {line_count}") |
34 | | - line_count += 1 |
35 | | - |
36 | | - html.Button("Add line", click=add_line) |
37 | | - html.Button("Update first line", click=update_first_line) |
38 | | - |
39 | | -# ----------------------------------------------------------------------------- |
40 | | -# start server |
41 | | -# ----------------------------------------------------------------------------- |
42 | 43 |
|
43 | 44 | if __name__ == "__main__": |
44 | | - server.start() |
| 45 | + main() |
0 commit comments