File: server.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 (47 lines) | stat: -rw-r--r-- 789 bytes parent folder | download | duplicates (2)
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
import asyncio
import random
from trame.app import get_server

server = get_server()
state, ctrl = server.state, server.controller

state.count = 1
state.play = False


@state.change("count")
def count_change(count, **_):
    print(f"count={count}")


@ctrl.trigger("add")
def add_to_count():
    state.count += 1


@ctrl.trigger("subtract")
def subtract_to_count():
    state.count -= 1


@ctrl.trigger("toggle_play")
def toggle_play():
    state.play = not state.play


@ctrl.trigger("random")
def random_count():
    server.js_call("counter", "reset", random.random())


async def animate(**kwargs):
    while True:
        await asyncio.sleep(0.5)
        if state.play:
            with state:
                state.count += 1


ctrl.on_server_ready.add_task(animate)

server.start()