File: 02_trigger_client.py

package info (click to toggle)
python-trame 3.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 101,620 kB
  • sloc: python: 13,515; sh: 183; javascript: 93; makefile: 7
file content (76 lines) | stat: -rw-r--r-- 2,415 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
from trame.app import get_server
from trame.ui.html import DivLayout
from trame.widgets import html, trame

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

server = get_server()
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")


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

layout = DivLayout(server)

with layout:
    html.Div("State a={{ a }} - template timestep {{ tts }}", style="padding: 20px;")
    client_triggers = trame.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)",
    )
    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("")

    trame.LifeCycleMonitor(type="error", events=("['created', 'destroyed']",))


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

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