Skip to content

Commit 8a44759

Browse files
feat: add timeout method for ui.notification (#4444)
This PR is based on #4437 , which explores a way to make notifications dismiss after a certain period of time. This PR provides a method called `timeout` that allows people to directly override the value of timeout (instead of manually modifying the value of the props) I did a simple test.It worked well. ```python # notification.py (This PR did) class Notification(Element, component='notification.js'): ... @Property def timeout(self) -> Optional[float]: """The timout value of the notification.""" return self._props['options']['timeout'] @timeout.setter def timeout(self,value: Optional[float]) -> None: self._props["options"]["timeout"] = (value or 0) *1000 self.update() # test.py import asyncio from nicegui import ui async def test_set_timeout(): n = ui.notification("Test Notification",timeout=None) n.on_dismiss(lambda:print("Notification Dismiss.")) await asyncio.sleep(3) n.message="Test Done" n.timeout = 3 # This is a new method ui.button("Test").on_click(test_set_timeout) ui.run() ``` That's all. If you have any suggestions for changes to this PR, please let me know. Thanks a lot. --------- Co-authored-by: Falko Schindler <[email protected]>
1 parent 44265b0 commit 8a44759

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

nicegui/elements/notification.py

+13
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@ def spinner(self, value: bool) -> None:
177177
self._props['options']['spinner'] = value
178178
self.update()
179179

180+
@property
181+
def timeout(self) -> float:
182+
"""Timeout of the notification in seconds.
183+
184+
*Added in version 2.13.0*
185+
"""
186+
return self._props['options']['timeout'] / 1000
187+
188+
@timeout.setter
189+
def timeout(self, value: Optional[float]) -> None:
190+
self._props['options']['timeout'] = (value or 0) * 1000
191+
self.update()
192+
180193
@property
181194
def close_button(self) -> Union[bool, str]:
182195
"""Whether the notification has a close button."""

0 commit comments

Comments
 (0)