From c171747c8edabf8b7898318df0fa1116052f1c77 Mon Sep 17 00:00:00 2001 From: Christoph Trappe Date: Fri, 13 Aug 2021 15:13:54 +0200 Subject: [PATCH 1/7] Notification element -Adds a Notify element -Extends the button element to accept an after event handler --- .gitignore | 1 + main.py | 5 +++++ nicegui/elements/button.py | 5 +++++ nicegui/elements/notify.py | 28 ++++++++++++++++++++++++++++ nicegui/ui.py | 1 + 5 files changed, 40 insertions(+) create mode 100644 nicegui/elements/notify.py diff --git a/.gitignore b/.gitignore index a85d6e375..a69520df3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ __pycache__/ *.egg-info/ .*.swp dist +/test.py diff --git a/main.py b/main.py index 26a9740a9..2a70188f2 100755 --- a/main.py +++ b/main.py @@ -288,6 +288,11 @@ def button_increment(): 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`. diff --git a/nicegui/elements/button.py b/nicegui/elements/button.py index 046ac14fe..c400ec177 100644 --- a/nicegui/elements/button.py +++ b/nicegui/elements/button.py @@ -9,11 +9,13 @@ def __init__(self, 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 @@ def __init__(self, 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 diff --git a/nicegui/elements/notify.py b/nicegui/elements/notify.py new file mode 100644 index 000000000..5f250933b --- /dev/null +++ b/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 diff --git a/nicegui/ui.py b/nicegui/ui.py index 5a91a518f..530084d70 100644 --- a/nicegui/ui.py +++ b/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 From e8ab2dc5a30c9167bc0a0f7ec74d23fe00e963bf Mon Sep 17 00:00:00 2001 From: Christoph Trappe Date: Fri, 13 Aug 2021 15:20:04 +0200 Subject: [PATCH 2/7] Notification element - Extends docstring of the notify element --- nicegui/elements/notify.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nicegui/elements/notify.py b/nicegui/elements/notify.py index 5f250933b..5983db55f 100644 --- a/nicegui/elements/notify.py +++ b/nicegui/elements/notify.py @@ -14,7 +14,7 @@ def __init__(self, :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: + :param close_button: label of the button to dismiss the notification """ view = jp.QNotify(message=message, position=position) From d6cb8b9fdb44d2721e9672789d695067fe1be02f Mon Sep 17 00:00:00 2001 From: Christoph Trappe Date: Fri, 20 Aug 2021 10:03:03 +0200 Subject: [PATCH 3/7] Notify element - Adds close button to the example --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 405413e43..bf48ebd9b 100755 --- a/main.py +++ b/main.py @@ -300,7 +300,7 @@ def button_increment(): with example(ui.notify): - notification = ui.notify(message='Notification') + notification = ui.notify(message='Notification', close_button='Close') ui.button('Get notification', on_click=lambda: notification.notify(True), after=lambda: notification.notify(False)) lifecycle = '''### Lifecycle From d9fed0cb18bb837af66b7c62f581d3240692828e Mon Sep 17 00:00:00 2001 From: Christoph Trappe Date: Fri, 20 Aug 2021 20:03:15 +0200 Subject: [PATCH 4/7] Removes the necessity of an after event from the notify component. --- main.py | 3 +-- nicegui/elements/button.py | 5 ----- nicegui/elements/notify.py | 12 ++++++++++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index bf48ebd9b..e20f07581 100755 --- a/main.py +++ b/main.py @@ -300,8 +300,7 @@ def button_increment(): with example(ui.notify): - notification = ui.notify(message='Notification', close_button='Close') - ui.button('Get notification', on_click=lambda: notification.notify(True), after=lambda: notification.notify(False)) + ui.button('Get notification', on_click=lambda: ui.notify(message='Notification', close_button='Close')) lifecycle = '''### Lifecycle diff --git a/nicegui/elements/button.py b/nicegui/elements/button.py index c400ec177..046ac14fe 100644 --- a/nicegui/elements/button.py +++ b/nicegui/elements/button.py @@ -9,13 +9,11 @@ def __init__(self, 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') @@ -23,9 +21,6 @@ def __init__(self, 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 diff --git a/nicegui/elements/notify.py b/nicegui/elements/notify.py index 5983db55f..f491183b1 100644 --- a/nicegui/elements/notify.py +++ b/nicegui/elements/notify.py @@ -1,5 +1,7 @@ import justpy as jp from .element import Element +import asyncio + class Notify(Element): @@ -23,6 +25,12 @@ def __init__(self, view.closeBtn = close_button super().__init__(view) + self.notify() + + async def notify_async(self): + self.view.notify = True + await self.wp.update() + self.view.notify = False - def notify(self, state: bool): - self.view.notify = state + def notify(self): + asyncio.get_event_loop().create_task(self.notify_async()) From 41ba8c8bf3da33d47e93b6342f95cab37e620a17 Mon Sep 17 00:00:00 2001 From: Rodja Trappe Date: Sat, 21 Aug 2021 05:09:24 +0200 Subject: [PATCH 5/7] simplification --- nicegui/elements/notify.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/nicegui/elements/notify.py b/nicegui/elements/notify.py index f491183b1..61345478d 100644 --- a/nicegui/elements/notify.py +++ b/nicegui/elements/notify.py @@ -25,12 +25,9 @@ def __init__(self, view.closeBtn = close_button super().__init__(view) - self.notify() + asyncio.get_event_loop().create_task(self.notify_async()) async def notify_async(self): self.view.notify = True await self.wp.update() self.view.notify = False - - def notify(self): - asyncio.get_event_loop().create_task(self.notify_async()) From 896ae065b9bb8bc21be6e9c90daeae4432bba814 Mon Sep 17 00:00:00 2001 From: Rodja Trappe Date: Sat, 21 Aug 2021 05:16:17 +0200 Subject: [PATCH 6/7] it seems updating the parent view is enough to show the notification --- nicegui/elements/notify.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nicegui/elements/notify.py b/nicegui/elements/notify.py index 61345478d..cb4f782a7 100644 --- a/nicegui/elements/notify.py +++ b/nicegui/elements/notify.py @@ -29,5 +29,5 @@ def __init__(self, async def notify_async(self): self.view.notify = True - await self.wp.update() + await self.parent_view.update() self.view.notify = False From f84db4fc07e8f0906c63d70004c8dc86bf48a3bd Mon Sep 17 00:00:00 2001 From: Rodja Trappe Date: Sat, 21 Aug 2021 05:23:22 +0200 Subject: [PATCH 7/7] minor changes in notify example --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index e20f07581..5ac40f1ae 100755 --- a/main.py +++ b/main.py @@ -287,7 +287,7 @@ def button_increment(): ui.button('Close', on_click=dialog.close) ui.button('Open dialog', on_click=dialog.open) - + with example(ui.menu): with ui.menu() as menu: @@ -300,7 +300,7 @@ def button_increment(): with example(ui.notify): - ui.button('Get notification', on_click=lambda: ui.notify(message='Notification', close_button='Close')) + ui.button('show notification', on_click=lambda: ui.notify(message='Some message', close_button='OK')) lifecycle = '''### Lifecycle