浏览代码

Notification element

-Adds a Notify element
-Extends the button element to accept an after event handler
Christoph Trappe 3 年之前
父节点
当前提交
c171747c8e
共有 5 个文件被更改,包括 40 次插入0 次删除
  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/
 .*.swp
 dist
+/test.py

+ 5 - 0
main.py

@@ -288,6 +288,11 @@ with example(ui.dialog):
 
     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
 
 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 = '',
                  *,
                  on_click: Callable = None,
+                 after: Callable = None,
                  ):
         """Button Element
 
         :param text: the label of the button
         :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')
@@ -21,6 +23,9 @@ class Button(Element):
         if on_click is not None:
             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)
 
     @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.log import Log as log
     from .elements.markdown import Markdown as markdown
+    from .elements.notify import Notify as notify
     from .elements.number import Number as number
     from .elements.radio import Radio as radio
     from .elements.select import Select as select