Notifications

#| standalone: true
#| components: [viewer]
#| viewerHeight: 200

from shiny import App, reactive, ui

types = ["default", "message", "warning", "error"]

app_ui = ui.page_fluid(
    ui.input_radio_buttons("type", "Notification Type", types, inline=True),
    ui.input_action_button("show", "Show Notification"),
)

def server(input, output, session):
    @reactive.effect
    @reactive.event(input.show)
    def _():
        type_txt = "notification" if input.type() == "default" else input.type()

        ui.notification_show(
            f"This {type_txt} will disappear after 2 seconds.",
            type=input.type(),
            duration=2,
        )

app = App(app_ui, server)
from shiny import reactive
from shiny.express import input, ui

types = ["default", "message", "warning", "error"]

ui.input_radio_buttons("type", "Notification Type", types, inline=True)
ui.input_action_button("show", "Show Notification")

@reactive.effect
@reactive.event(input.show)
def show_notification():
    type_txt = "notification" if input.type() == "default" else input.type()
    ui.notification_show(
        f"This {type_txt} will disappear after 2 seconds.",
        type=input.type(),
        duration=2,
    )
from shiny import App, reactive, ui

types = ["default", "message", "warning", "error"]

app_ui = ui.page_fluid(
    ui.input_radio_buttons("type", "Notification Type", types, inline=True),
    ui.input_action_button("show", "Show Notification"),
)

def server(input, output, session):
    @reactive.effect
    @reactive.event(input.show)
    def _():
        type_txt = "notification" if input.type() == "default" else input.type()

        ui.notification_show(
            f"This {type_txt} will disappear after 2 seconds.",
            type=input.type(),
            duration=2,
        )

app = App(app_ui, server)
No matching items

Relevant Functions

No matching items

Details

A notification is a message that appears near the bottom corner of the app. Notifications normally disappear after a short period of time, and should multiple notifications appear together, they will stack on top of one another.

To create a notification, call ui.notification_show(). Typically, you will want to create a reactive effect to call ui.show_notification() whenever a particular event occurs. For example, the reactive effect below will create a notification whenever the value of input.show() changes.

@reactive.effect
@reactive.event(input.show)
def _():
    ui.notification_show("You've been notified.")

You can call ui.notification_remove() to remove a notification programatically, but usually app developers will let notifications expire on their own. Also, notifications come by default with a button that the user can click to close the notification prematurely.

Duration

By default, Shiny notifications will disappear after five seconds. To change how long a notification appears for, set the duration argument of ui.notification_show() to an integer number of seconds. Set duration to None to have the notification appear until the user closes it.

Type

Shiny notifications come in four types: default, messages, warnings and errors. To set the type of a notification, use the type argument of ui.notification_show().

See Also: Modal messages provide a similar, but alternative way to display information to the user.

Variations

Replace/update a notification

Assign a notification an id to replace any existing notification with the same id. In the example below, a persistant notification is created with duration=None and updated each time you click the notification button.

#| standalone: true
#| components: [viewer]
#| viewerHeight: 300

from shiny import App, reactive, ui

app_ui = ui.page_fluid(
    ui.input_action_button("show", "Show Notification"),
)

def server(input, output, session):
    @reactive.effect
    @reactive.event(input.show)
    def show_or_update_notification():
        ui.notification_show(
            f"You clicked the Show button {input.show()} times.",
            duration=None,
            # compare to what happens if you comment out the line below
            id="message",
        )

app = App(app_ui, server)
from shiny import reactive
from shiny.express import input, ui

ui.input_action_button("show", "Show Notification")

@reactive.effect
@reactive.event(input.show)
def show_or_update_notification():
    ui.notification_show(
        f"You clicked the Show button {input.show()} times.",
        duration=None,
        # compare to what happens if you comment out the line below
        id="message",
    )
from shiny import App, reactive, ui

app_ui = ui.page_fluid(
    ui.input_action_button("show", "Show Notification"),
)

def server(input, output, session):
    @reactive.effect
    @reactive.event(input.show)
    def show_or_update_notification():
        ui.notification_show(
            f"You clicked the Show button {input.show()} times.",
            duration=None,
            # compare to what happens if you comment out the line below
            id="message",
        )

app = App(app_ui, server)

Track and remove notifications

Track notifications, and use ui.notification_remove() to remove notifications one at a time.

#| standalone: true
#| components: [viewer]
#| viewerHeight: 225

from shiny import App, reactive, ui

app_ui = ui.page_fluid(
    ui.input_action_button("show", "Show Notification"),
    ui.input_action_button("remove", "Remove Notification"),
)

def server(input, output, session):
    ids: list[str] = []
    n: int = 0

    @reactive.effect
    @reactive.event(input.show)
    def _():
        nonlocal ids
        nonlocal n
        # Save the ID for removal later
        id = ui.notification_show(
            f"Notification {n}", duration=None, close_button=False
        )
        ids.append(id)
        n += 1

    @reactive.effect
    @reactive.event(input.remove)
    def _():
        nonlocal ids
        if ids:
            ui.notification_remove(ids.pop())

app = App(app_ui, server)
from shiny import reactive
from shiny.express import input, ui

ui.input_action_button("show", "Show Notification")
ui.input_action_button("remove", "Remove Notification")

ids: list[str] = []
n: int = 0

@reactive.effect
@reactive.event(input.show)
def _():
    global ids
    global n
    # Save the ID for removal later
    id = ui.notification_show("Message " + str(n), duration=None)
    ids.append(id)
    n += 1

@reactive.effect
@reactive.event(input.remove)
def _():
    global ids
    if ids:
        ui.notification_remove(ids.pop())
from shiny import App, reactive, ui

app_ui = ui.page_fluid(
    ui.input_action_button("show", "Show Notification"),
    ui.input_action_button("remove", "Remove Notification"),
)

def server(input, output, session):
    ids: list[str] = []
    n: int = 0

    @reactive.effect
    @reactive.event(input.show)
    def _():
        nonlocal ids
        nonlocal n
        # Save the ID for removal later
        id = ui.notification_show(
            f"Notification {n}", duration=None, close_button=False
        )
        ids.append(id)
        n += 1

    @reactive.effect
    @reactive.event(input.remove)
    def _():
        nonlocal ids
        if ids:
            ui.notification_remove(ids.pop())

app = App(app_ui, server)
No matching items