Skip to content

Commit d743680

Browse files
committed
docs: modernize guides
1 parent b5371a3 commit d743680

2 files changed

Lines changed: 59 additions & 51 deletions

File tree

examples/core_features/dynamic_layout.py

100644100755
Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
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
210
from trame.ui.html import DivLayout
311
from trame.widgets import html
412

5-
# -----------------------------------------------------------------------------
6-
# Trame app
7-
# -----------------------------------------------------------------------------
813

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()
1019

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
1424

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
1630

31+
def _build_ui(self):
32+
with DivLayout(self.server) as self.ui:
33+
self.server.ui.first_line(self.ui) # Insert place holder
1734

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)
2437

2538

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()
2942

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-
# -----------------------------------------------------------------------------
4243

4344
if __name__ == "__main__":
44-
server.start()
45+
main()

examples/core_features/multi_layout.py

100644100755
Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
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
210
from trame.ui.html import DivLayout
311
from trame.widgets import html
412

5-
# -----------------------------------------------------------------------------
6-
# Trame app
7-
# -----------------------------------------------------------------------------
813

9-
server = get_server()
14+
class MultiLayout(TrameApp):
15+
def __init__(self, server=None):
16+
super().__init__(server)
17+
self._build_ui()
1018

11-
# -----------------------------------------------------------------------------
12-
# UI setup
13-
# -----------------------------------------------------------------------------
19+
def _build_ui(self):
20+
with DivLayout(self.server) as self.ui:
21+
html.A("Open UI(a)", href="/?ui=a", target="_blank")
22+
html.Br()
23+
html.A("Open UI(b)", href="/?ui=b", target="_blank")
1424

15-
with DivLayout(server):
16-
html.A("Open UI(a)", href="/?ui=a", target="_blank")
17-
html.Br()
18-
html.A("Open UI(b)", href="/?ui=b", target="_blank")
25+
with DivLayout(self.server, "a"):
26+
html.Div("UI for A")
1927

20-
with DivLayout(server, "a"):
21-
html.Div("UI for A")
28+
with DivLayout(self.server, "b"):
29+
html.Div("UI for B")
2230

23-
with DivLayout(server, "b"):
24-
html.Div("UI for B")
2531

26-
# -----------------------------------------------------------------------------
27-
# start server
28-
# -----------------------------------------------------------------------------
32+
def main():
33+
app = MultiLayout()
34+
app.server.start()
35+
2936

3037
if __name__ == "__main__":
31-
server.start()
38+
main()

0 commit comments

Comments
 (0)