-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlifecycle.py
More file actions
42 lines (31 loc) · 1.15 KB
/
lifecycle.py
File metadata and controls
42 lines (31 loc) · 1.15 KB
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
from htag import Tag,States,ChromeApp
from time import time
import asyncio
class LifeCycleApp(Tag.App):
statics = [Tag.title("Life Cycle Demo")]
def init(self):
# initial states (executed one time)
self.states = States(cpt=0, time=0)
with self:
# manual update
Tag.button(self.states.cpt,"+", _onclick=self.on_click)
# task update
Tag.div( self.states.time )
# computed
Tag.div( lambda: f"computed=({self.states.cpt} & {self.states.time})" )
def on_mount(self):
# at each mount (at 1st and following F5), this code is executed
# to mount things
async def process():
while 1:
self.states.time += 1
await asyncio.sleep(0.5)
self.task = asyncio.create_task(process())
def on_unmount(self):
# at each onmount (at end and following F5), this code is executed
# to unmount things
self.task.cancel()
def on_click(self, o):
self.states.cpt += 1
if __name__ == "__main__":
ChromeApp(LifeCycleApp).run(reload=False)