Quellcode durchsuchen

new on_connect example

Rodja Trappe vor 2 Jahren
Ursprung
Commit
90fd269c99
1 geänderte Dateien mit 6 neuen und 8 gelöschten Zeilen
  1. 6 8
      website/reference.py

+ 6 - 8
website/reference.py

@@ -494,15 +494,13 @@ When NiceGUI is shut down or restarted, the startup tasks will be automatically
     def lifecycle_example():
         from nicegui import Client
 
-        async def increment(client: Client):
-            counter.value += 1
+        def handle_connect():
+            if watch.value:
+                count.set_text(str(int(count.text or 0) + 1))
 
-        async def decrement(client: Client):
-            counter.value -= 1
-
-        counter = ui.number('connections', value=0).props('readonly').classes('w-24')
-        ui.on_connect(increment)
-        ui.on_disconnect(decrement)
+        watch = ui.checkbox('count new connections')
+        count = ui.label('').classes('mt-8 self-center text-5xl')
+        ui.on_connect(handle_connect)
 
     @example(ui.timer)
     def timer_example():