Browse Source

Merge branch 'main' into globals

Falko Schindler 3 năm trước cách đây
mục cha
commit
41c3d075e0
5 tập tin đã thay đổi với 53 bổ sung6 xóa
  1. 12 0
      main.py
  2. 11 6
      nicegui/elements/button.py
  3. 14 0
      nicegui/elements/menu_separator.py
  4. 1 0
      nicegui/ui.py
  5. 15 0
      nicegui/utils.py

+ 12 - 0
main.py

@@ -129,6 +129,17 @@ with example(ui.button):
     ui.button('Button', on_click=button_increment)
     button_result = ui.label('pressed: 0')
 
+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')
+
+    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))
     with ui.row():
@@ -242,6 +253,7 @@ with example(ui.menu):
     with ui.menu() as menu:
         ui.menu_item('Menu item 1', lambda: choice.set_text('Selected item 1.'))
         ui.menu_item('Menu item 2', lambda: choice.set_text('Selected item 2.'))
+        ui.menu_separator()
         ui.menu_item('Close', on_click=menu.close)
 
     ui.button('Open menu', on_click=menu.open).props('color=secondary')

+ 11 - 6
nicegui/elements/button.py

@@ -1,14 +1,17 @@
-from typing import Callable
+import asyncio
+from typing import Awaitable, Callable, Union
+
 import justpy as jp
+
 from .element import Element
-from ..utils import handle_exceptions, provide_arguments
+from ..utils import handle_exceptions, provide_arguments, async_provide_arguments
 
 class Button(Element):
 
     def __init__(self,
                  text: str = '',
                  *,
-                 on_click: Callable = None,
+                 on_click: Union[Callable, Awaitable] = None,
                  ):
         """Button Element
 
@@ -17,11 +20,13 @@ class Button(Element):
         """
 
         view = jp.QButton(label=text, color='primary')
+        super().__init__(view)
 
         if on_click is not None:
-            view.on('click', handle_exceptions(provide_arguments(on_click)))
-
-        super().__init__(view)
+            if asyncio.iscoroutinefunction(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)))
 
     @property
     def text(self):

+ 14 - 0
nicegui/elements/menu_separator.py

@@ -0,0 +1,14 @@
+import justpy as jp
+
+from .element import Element
+
+
+class MenuSeparator(Element):
+
+    def __init__(self):
+        """Menu Item Separator
+
+        A separator for menus.
+        """
+        view = jp.QSeparator()
+        super().__init__(view)

+ 1 - 0
nicegui/ui.py

@@ -17,6 +17,7 @@ class Ui:
     from .elements.markdown import Markdown as markdown
     from .elements.menu import Menu as menu
     from .elements.menu_item import MenuItem as menu_item
+    from .elements.menu_separator import MenuSeparator as menu_separator
     from .elements.notify import Notify as notify
     from .elements.number import Number as number
     from .elements.page import Page as page

+ 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