File: trigger.py

package info (click to toggle)
python-trame-client 3.11.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,128 kB
  • sloc: python: 9,609; javascript: 3,897; sh: 9; makefile: 6
file content (82 lines) | stat: -rw-r--r-- 2,514 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from trame.app import get_server
from trame.widgets import html, client
from trame.ui.html import DivLayout

# -----------------------------------------------------------------------------
# Trame app
# -----------------------------------------------------------------------------

server = get_server(client_type="vue3")

state, ctrl = server.state, server.controller

# -----------------------------------------------------------------------------
# State setup
# -----------------------------------------------------------------------------

state.a = 1

# -----------------------------------------------------------------------------
# Methods call
# -----------------------------------------------------------------------------


def monitor_life_cycles(life_cycle):
    print(f"Life cycle: {life_cycle}")


def call_method_1():
    print("Server: call_method_1")
    server.js_call("ref_name", "emit", "method1", "a")


def call_method_2():
    print("Server: call_method_2")
    ctrl.call("method2", "b")


def call_method_3():
    print("Server: call_method_3")
    ctrl.call("method3")


def client_exited():
    print("client exited")


# -----------------------------------------------------------------------------
# UI setup
# -----------------------------------------------------------------------------

layout = DivLayout(server)

with layout:
    html.Div("State a={{ a }} - template timestep {{ tts }}", style="padding: 20px;")
    client_triggers = client.ClientTriggers(
        ref="ref_name",
        created=(monitor_life_cycles, "['created']"),
        mounted=(monitor_life_cycles, "['mounted']"),
        beforeDestroy=(monitor_life_cycles, "['beforeDestroy']"),
        method1="window.console.log('method 1', $event)",
        method2="window.console.log('method 2', $event)",
        method3="window.console.log('method 3', $event)",
        exit=client_exited,
    )
    ctrl.call = client_triggers.call

    with html.Div(style="padding: 10px;") as div:
        html.Button("Method 1", click=call_method_1)
        html.Button("Method 2", click=call_method_2)
        html.Button("Method 3", click=call_method_3)
        html.Button("a+", click="a+=1")
        div.add_child("")

    client.LifeCycleMonitor(type="info", events=("['created', 'destroyed']",))


# -----------------------------------------------------------------------------
# start server
# -----------------------------------------------------------------------------

if __name__ == "__main__":
    server.start()