Bladeren bron

added documentation for ui.on_startup

Rodja Trappe 3 jaren geleden
bovenliggende
commit
17ce583569
1 gewijzigde bestanden met toevoegingen van 19 en 0 verwijderingen
  1. 19 0
      main.py

+ 19 - 0
main.py

@@ -8,6 +8,7 @@ import sys
 from typing import Union
 import docutils.core
 import re
+import asyncio
 
 # add docutils css to webpage
 wp.head_html += docutils.core.publish_parts('', writer_name='html')['stylesheet']
@@ -108,6 +109,24 @@ with (example(binding)):
         ui.toggle({1: 'a', 2: 'b', 3: 'c'}).bind_value(demo.number)
         ui.number().bind_value(demo.number)
 
+lifecycle = '''### Lifecycle
+
+You can run a coroutine on startup as a parallel task by passing it to `ui.on_startup`.
+'''
+with (example(lifecycle)):
+
+    count_label = ui.label('count: 0')
+    count = 0
+
+    async def update_count():
+        global count
+        while True:
+            count_label.text = f'count: {count}'
+            count += 1
+            await asyncio.sleep(1)
+
+    ui.on_startup(update_count())
+
 
 with example(ui.timer):
     from datetime import datetime