Quellcode durchsuchen

Notification element

-Adds a Notify element
-Extends the button element to accept an after event handler
Christoph Trappe vor 3 Jahren
Ursprung
Commit
c171747c8e
5 geänderte Dateien mit 40 neuen und 0 gelöschten Zeilen
  1. 1 0
      .gitignore
  2. 5 0
      main.py
  3. 5 0
      nicegui/elements/button.py
  4. 28 0
      nicegui/elements/notify.py
  5. 1 0
      nicegui/ui.py

+ 1 - 0
.gitignore

@@ -2,3 +2,4 @@ __pycache__/
 *.egg-info/
 *.egg-info/
 .*.swp
 .*.swp
 dist
 dist
+/test.py

+ 5 - 0
main.py

@@ -288,6 +288,11 @@ with example(ui.dialog):
 
 
     ui.button('Open dialog', on_click=dialog.open)
     ui.button('Open dialog', on_click=dialog.open)
 
 
+with example(ui.notify):
+
+    notification = ui.notify(message='Notification')
+    ui.button('Get notification', on_click=lambda: notification.notify(True), after=lambda: notification.notify(False))
+
 lifecycle = '''### Lifecycle
 lifecycle = '''### Lifecycle
 
 
 You can run a function or coroutine on startup as a parallel task by passing it to `ui.on_startup`.
 You can run a function or coroutine on startup as a parallel task by passing it to `ui.on_startup`.

+ 5 - 0
nicegui/elements/button.py

@@ -9,11 +9,13 @@ class Button(Element):
                  text: str = '',
                  text: str = '',
                  *,
                  *,
                  on_click: Callable = None,
                  on_click: Callable = None,
+                 after: Callable = None,
                  ):
                  ):
         """Button Element
         """Button Element
 
 
         :param text: the label of the button
         :param text: the label of the button
         :param on_click: callback which is invoked when button is pressed
         :param on_click: callback which is invoked when button is pressed
+        :param after: callback to be executed aftern button is pressed, e.g. to clean up before the next page update
         """
         """
 
 
         view = jp.QButton(label=text, color='primary')
         view = jp.QButton(label=text, color='primary')
@@ -21,6 +23,9 @@ class Button(Element):
         if on_click is not None:
         if on_click is not None:
             view.on('click', handle_exceptions(provide_arguments(on_click)))
             view.on('click', handle_exceptions(provide_arguments(on_click)))
 
 
+        if after is not None:
+            view.on('after', handle_exceptions(provide_arguments(after)))
+
         super().__init__(view)
         super().__init__(view)
 
 
     @property
     @property

+ 28 - 0
nicegui/elements/notify.py

@@ -0,0 +1,28 @@
+import justpy as jp
+from .element import Element
+
+class Notify(Element):
+
+    def __init__(self,
+                 message: str = '',
+                 position: str = 'bottom',
+                 close_button: str = ''
+                 ):
+        """Notification Element
+
+        Displays a notification on the screen.
+
+        :param message: the content of the notification
+        :param position: possible position: 'top-left', 'top-right', 'bottom-left','bottom-right, 'top', 'bottom', 'left', 'right', 'center'
+        :param close_button:
+        """
+
+        view = jp.QNotify(message=message, position=position)
+
+        if close_button:
+            view.closeBtn = close_button
+
+        super().__init__(view)
+
+    def notify(self, state: bool):
+        self.view.notify = state

+ 1 - 0
nicegui/ui.py

@@ -15,6 +15,7 @@ class Ui:
     from .elements.link import Link as link
     from .elements.link import Link as link
     from .elements.log import Log as log
     from .elements.log import Log as log
     from .elements.markdown import Markdown as markdown
     from .elements.markdown import Markdown as markdown
+    from .elements.notify import Notify as notify
     from .elements.number import Number as number
     from .elements.number import Number as number
     from .elements.radio import Radio as radio
     from .elements.radio import Radio as radio
     from .elements.select import Select as select
     from .elements.select import Select as select