Skip to content

Commit ef4d0c4

Browse files
committed
docs(examples): use best practice with TrameApp
1 parent 3df33e1 commit ef4d0c4

1 file changed

Lines changed: 41 additions & 31 deletions

File tree

examples/03_markdown/01_markdown.py

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,65 @@
55

66
import os
77

8-
from trame.app import get_server
8+
from trame.app import TrameApp
99
from trame.ui.vuetify3 import SinglePageLayout
10-
from trame.widgets import markdown, vuetify3
10+
from trame.widgets import markdown
11+
from trame.widgets import vuetify3 as v3
12+
from trame.decorators import change
1113

1214
# -----------------------------------------------------------------------------
1315
# Trame setup
1416
# -----------------------------------------------------------------------------
1517

16-
server = get_server(client_type="vue3")
17-
state, ctrl = server.state, server.controller
1818

19-
# -----------------------------------------------------------------------------
20-
# Read markdown file
21-
# -----------------------------------------------------------------------------
19+
class MarkdownApp(TrameApp):
20+
def __init__(self, server=None):
21+
super().__init__(server)
22+
self._build_ui()
2223

24+
# -----------------------------------------------------------------------------
25+
# Read markdown file
26+
# -----------------------------------------------------------------------------
2327

24-
@state.change("file_name")
25-
def update_md(file_name, **kwargs):
26-
md_file_path = os.path.join(os.path.dirname(__file__), file_name)
27-
with open(md_file_path, encoding="utf-8") as md:
28-
ctrl.md_update(md.read())
28+
@change("file_name")
29+
def update_md(self, file_name, **kwargs):
30+
md_file_path = os.path.join(os.path.dirname(__file__), file_name)
31+
with open(md_file_path, encoding="utf-8") as md:
32+
self.ctrl.md_update(md.read())
2933

34+
# -----------------------------------------------------------------------------
35+
# GUI
36+
# -----------------------------------------------------------------------------
3037

31-
# -----------------------------------------------------------------------------
32-
# GUI
33-
# -----------------------------------------------------------------------------
34-
35-
state.trame__title = "MD Viewer"
38+
def _build_ui(self):
39+
self.state.trame__title = "MD Viewer"
3640

37-
with SinglePageLayout(server) as layout:
38-
layout.title.set_text("Markdown Viewer")
41+
with SinglePageLayout(self.server) as self.ui:
42+
self.ui.title.set_text("Markdown Viewer")
3943

40-
with layout.toolbar:
41-
vuetify3.VSpacer()
42-
vuetify3.VSelect(
43-
v_model=("file_name", "demo.md"),
44-
items=("options", ["demo.md", "sample.md", "module.md"]),
45-
hide_details=True,
46-
dense=True,
47-
)
44+
with self.ui.toolbar:
45+
v3.VSpacer()
46+
v3.VSelect(
47+
v_model=("file_name", "demo.md"),
48+
items=("options", ["demo.md", "sample.md", "module.md"]),
49+
hide_details=True,
50+
dense=True,
51+
)
4852

49-
with layout.content:
50-
md = markdown.Markdown(classes="pa-4 mx-2")
51-
ctrl.md_update = md.update
53+
with self.ui.content:
54+
md = markdown.Markdown(classes="pa-4 mx-2")
55+
self.ctrl.md_update = md.update
5256

5357

5458
# -----------------------------------------------------------------------------
5559
# Main
5660
# -----------------------------------------------------------------------------
5761

62+
63+
def main():
64+
mdApp = MarkdownApp()
65+
mdApp.server.start()
66+
67+
5868
if __name__ == "__main__":
59-
server.start()
69+
main()

0 commit comments

Comments
 (0)