Notifications¶
Display different types of notifications to users including persistent, timed, and loading indicators.
Features:
viser.ClientHandle.add_notification()
for user alertsPersistent notifications that stay until dismissed
Timed notifications with automatic removal
Loading notifications with progress indication
Per-client notification management
Source: examples/02_gui/06_notifications.py

Code¶
1import time
2
3import viser
4
5
6def main() -> None:
7 server = viser.ViserServer()
8
9 persistent_notif_button = server.gui.add_button(
10 "Show persistent notification (default)"
11 )
12 timed_notif_button = server.gui.add_button("Show timed notification")
13 controlled_notif_button = server.gui.add_button("Show controlled notification")
14 loading_notif_button = server.gui.add_button("Show loading notification")
15
16 remove_controlled_notif = server.gui.add_button("Remove controlled notification")
17
18 @persistent_notif_button.on_click
19 def _(event: viser.GuiEvent) -> None:
20 client = event.client
21 assert client is not None
22
23 client.add_notification(
24 title="Persistent notification",
25 body="This can be closed manually and does not disappear on its own!",
26 with_close_button=True,
27 )
28
29 @timed_notif_button.on_click
30 def _(event: viser.GuiEvent) -> None:
31 client = event.client
32 assert client is not None
33
34 client.add_notification(
35 title="Timed notification",
36 body="This disappears automatically after 5 seconds!",
37 auto_close_seconds=5.0,
38 )
39
40 @controlled_notif_button.on_click
41 def _(event: viser.GuiEvent) -> None:
42 client = event.client
43 assert client is not None
44
45 controlled_notif = client.add_notification(
46 title="Controlled notification",
47 body="This cannot be closed by the user and is controlled in code only!",
48 with_close_button=False,
49 auto_close_seconds=False,
50 )
51
52 @remove_controlled_notif.on_click
53 def _(_) -> None:
54 controlled_notif.remove()
55
56 @loading_notif_button.on_click
57 def _(event: viser.GuiEvent) -> None:
58 client = event.client
59 assert client is not None
60
61 loading_notif = client.add_notification(
62 title="Loading notification",
63 body="This indicates that some action is in progress! It will be updated in 3 seconds.",
64 loading=True,
65 with_close_button=False,
66 )
67 time.sleep(3.0)
68
69 loading_notif.title = "Updated notification"
70 loading_notif.body = "This notification has been updated!"
71 loading_notif.loading = False
72 loading_notif.with_close_button = True
73 loading_notif.auto_close_seconds = 5.0
74 loading_notif.color = "green"
75
76 @server.on_client_connect
77 def _(client: viser.ClientHandle) -> None:
78 client.add_notification(
79 "Connected",
80 "You are now connected to the server!",
81 auto_close_seconds=30.0,
82 )
83
84 while True:
85 time.sleep(1.0)
86
87
88if __name__ == "__main__":
89 main()