Notifications

Display different types of notifications to users including persistent, timed, and loading indicators.

Features:

  • viser.ClientHandle.add_notification() for user alerts

  • Persistent notifications that stay until dismissed

  • Timed notifications with automatic removal

  • Loading notifications with progress indication

  • Per-client notification management

Source: examples/02_gui/07_notifications.py

Notifications

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            loading=False,
27            with_close_button=True,
28            auto_close=False,
29        )
30
31    @timed_notif_button.on_click
32    def _(event: viser.GuiEvent) -> None:
33        client = event.client
34        assert client is not None
35
36        client.add_notification(
37            title="Timed notification",
38            body="This disappears automatically after 5 seconds!",
39            loading=False,
40            with_close_button=True,
41            auto_close=5000,
42        )
43
44    @controlled_notif_button.on_click
45    def _(event: viser.GuiEvent) -> None:
46        client = event.client
47        assert client is not None
48
49        controlled_notif = client.add_notification(
50            title="Controlled notification",
51            body="This cannot be closed by the user and is controlled in code only!",
52            loading=False,
53            with_close_button=False,
54            auto_close=False,
55        )
56
57        @remove_controlled_notif.on_click
58        def _(_) -> None:
59            controlled_notif.remove()
60
61    @loading_notif_button.on_click
62    def _(event: viser.GuiEvent) -> None:
63        client = event.client
64        assert client is not None
65
66        loading_notif = client.add_notification(
67            title="Loading notification",
68            body="This indicates that some action is in progress! It will be updated in 3 seconds.",
69            loading=True,
70            with_close_button=False,
71            auto_close=False,
72        )
73
74        time.sleep(3.0)
75
76        loading_notif.title = "Updated notification"
77        loading_notif.body = "This notification has been updated!"
78        loading_notif.loading = False
79        loading_notif.with_close_button = True
80        loading_notif.auto_close = 5000
81        loading_notif.color = "green"
82
83    @server.on_client_connect
84    def _(client: viser.ClientHandle) -> None:
85        client.add_notification(
86            "Connected", "You are now connected to the server!", auto_close=30_000
87        )
88
89    while True:
90        time.sleep(1.0)
91
92
93if __name__ == "__main__":
94    main()