NotificationsΒΆ

Examples of adding notifications per client in Viser.

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