File: busy-task.py

package info (click to toggle)
python-trame 3.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 101,620 kB
  • sloc: python: 13,515; sh: 183; javascript: 93; makefile: 7
file content (67 lines) | stat: -rw-r--r-- 1,745 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import time

import trame.widgets.vuetify3 as v3
from trame.app import asynchronous, get_server
from trame.ui.vuetify3 import VAppLayout

# -------------------------------------------------------------------
# Helper
# -------------------------------------------------------------------


class AsyncTracker:
    def __init__(self, server, name):
        self.name = name
        self.server = server
        self.state = server.state

    async def __aenter__(self):
        with self.state:
            self.state[self.name] = True
        await self.server.network_completion

    async def __aexit__(self, exc_t, exc_v, exc_tb):
        with self.state:
            self.state[self.name] = False
        await self.server.network_completion


# -------------------------------------------------------------------


def long_task():
    time.sleep(2)
    print("Finished")


async def run_long_task():
    async with AsyncTracker(server, "loading"):
        long_task()


def run_task():
    asynchronous.create_task(run_long_task())


# -------------------------------------------------------------------

server = get_server()
state = server.state

with VAppLayout(server) as layout:
    with layout:
        with v3.Template():
            with v3.VOverlay(
                v_model=("loading", False),
                classes="align-center justify-center",
                close_on_content_click=False,
                persistent=True,
                style="z-index: 10000;",
            ):
                v3.VProgressCircular(color="primary", size=64, indeterminate=True)
        v3.VBtn("Long task (no tracking)", click=long_task)
        v3.VBtn("Long task (tracking)", click=run_task)


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