Falko Schindler 3 gadi atpakaļ
vecāks
revīzija
dca0b4a864
3 mainītis faili ar 20 papildinājumiem un 7 dzēšanām
  1. 13 3
      main.py
  2. 6 3
      nicegui/timer.py
  3. 1 1
      nicegui/utils.py

+ 13 - 3
main.py

@@ -113,9 +113,19 @@ with (example(binding)):
 with example(ui.timer):
 with example(ui.timer):
     from datetime import datetime
     from datetime import datetime
 
 
-    clock = ui.label()
-    t = ui.timer(interval=0.1, callback=lambda: clock.set_text(datetime.now().strftime("%X")))
-    ui.checkbox('active').bind_value(t.active)
+    with ui.row().classes('items-center'):
+        clock = ui.label()
+        t = ui.timer(interval=0.1, callback=lambda: clock.set_text(datetime.now().strftime("%X.%f")[:-5]))
+        ui.checkbox('active').bind_value(t.active)
+
+    with ui.row():
+        def lazy_update():
+            new_text = datetime.now().strftime('%X.%f')[:-5]
+            if lazy_clock.text[:8] == new_text[:8]:
+                return False
+            lazy_clock.text = new_text
+        lazy_clock = ui.label()
+        ui.timer(interval=0.1, callback=lazy_update)
 
 
 with example(ui.label):
 with example(ui.label):
 
 

+ 6 - 3
nicegui/timer.py

@@ -17,9 +17,11 @@ class Timer:
         """Timer
         """Timer
 
 
         One major drive behind the creation of NiceGUI was the necessity to have a simple approach to update the interface in regular intervals, for example to show a graph with incomming measurements.
         One major drive behind the creation of NiceGUI was the necessity to have a simple approach to update the interface in regular intervals, for example to show a graph with incomming measurements.
+        A timer will execute a callback repeatedly with a given interval.
+        The parent view container will be updated automatically, as long as the callback does not return `False`.
 
 
         :param interval: the interval in which the timer is called
         :param interval: the interval in which the timer is called
-        :param callback: function to execute when interval elapses
+        :param callback: function to execute when interval elapses (can return `False` to prevent view update)
         :param active: whether the callback should be executed or not
         :param active: whether the callback should be executed or not
         :param once: whether the callback is only executed once after a delay specified by `interval`; default is `False`
         :param once: whether the callback is only executed once after a delay specified by `interval`; default is `False`
         """
         """
@@ -39,8 +41,9 @@ class Timer:
                 try:
                 try:
                     start = time.time()
                     start = time.time()
                     if self.active:
                     if self.active:
-                        handle_exceptions(callback)()
-                        await parent.update()
+                        needs_update = handle_exceptions(callback)()
+                        if needs_update != False:
+                            await parent.update()
                     dt = time.time() - start
                     dt = time.time() - start
                     await asyncio.sleep(interval - dt)
                     await asyncio.sleep(interval - dt)
                 except CancelledError:
                 except CancelledError:

+ 1 - 1
nicegui/utils.py

@@ -19,7 +19,7 @@ def provide_arguments(func, *keys):
 def handle_exceptions(func):
 def handle_exceptions(func):
     def inner_function(*args, **kwargs):
     def inner_function(*args, **kwargs):
         try:
         try:
-            func(*args, **kwargs)
+            return func(*args, **kwargs)
         except Exception:
         except Exception:
             traceback.print_exc()
             traceback.print_exc()
     return inner_function
     return inner_function