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/06_notifications.py

Notifications

Code

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