Christoph Trappe 3 jaren geleden
bovenliggende
commit
c5b4acfa23
3 gewijzigde bestanden met toevoegingen van 26 en 23 verwijderingen
  1. 8 5
      main.py
  2. 3 18
      nicegui/elements/button.py
  3. 15 0
      nicegui/utils.py

+ 8 - 5
main.py

@@ -128,13 +128,16 @@ with example(ui.button):
     ui.button('Button', on_click=button_increment)
     button_result = ui.label('pressed: 0')
 
-with example(ui.button):
-    async def async_button_increment():
-        ui.notify('Asynchronous task started', close_button='OK')
+async_button = '''### Button with asynchronous action
+The button element does also support asynchronous action.
+'''
+with example(async_button):
+    async def async_task():
+        ui.notify('Asynchronous task started')
         await asyncio.sleep(5)
-        ui.notify('Asynchronous task finished', close_button='OK')
+        ui.notify('Asynchronous task finished')
 
-    ui.button('Start asynchronous task', on_click=async_button_increment)
+    ui.button('Start asynchronous task', on_click=async_task)
 
 with example(ui.checkbox):
     ui.checkbox('check me', on_change=lambda e: checkbox_state.set_text(e.value))

+ 3 - 18
nicegui/elements/button.py

@@ -4,7 +4,7 @@ from typing import Awaitable, Callable, Union
 import justpy as jp
 
 from .element import Element
-from ..utils import handle_exceptions, provide_arguments, EventArguments
+from ..utils import handle_exceptions, provide_arguments, async_provide_arguments
 
 class Button(Element):
 
@@ -20,29 +20,14 @@ class Button(Element):
         """
 
         view = jp.QButton(label=text, color='primary')
+        super().__init__(view)
 
         if on_click is not None:
             if asyncio.iscoroutinefunction(on_click):
-
-                def async_provide_arguments(func, *keys):
-                    def inner_function(sender, event):
-                        async def execute_function():
-                            try:
-                                await func()
-                            except TypeError:
-                                await func(EventArguments(sender, **{key: event[key] for key in keys}))
-
-                            await self.parent_view.update()
-
-                        asyncio.get_event_loop().create_task(execute_function())
-                    return inner_function
-
-                view.on('click', handle_exceptions(async_provide_arguments(on_click)))
+                view.on('click', handle_exceptions(async_provide_arguments(func=on_click, update_function=view.update)))
             else:
                 view.on('click', handle_exceptions(provide_arguments(on_click)))
 
-        super().__init__(view)
-
     @property
     def text(self):
         return self.view.label

+ 15 - 0
nicegui/utils.py

@@ -34,3 +34,18 @@ def handle_awaitable(func):
         else:
             return func(*args, **kwargs)
     return inner_function
+
+
+def async_provide_arguments(func, update_function, *keys):
+    def inner_function(sender, event):
+        async def execute_function():
+            try:
+                await func()
+            except TypeError:
+                await func(EventArguments(sender, **{key: event[key] for key in keys}))
+
+            await update_function()
+
+        asyncio.get_event_loop().create_task(execute_function())
+
+    return inner_function