alert_handler module¶
- class AlertHandler¶
Bases:
object
Manages alerts for the application.
This class provides functionality for: - Displaying a modal with all alerts, which can be filtered and dismissed. - Showing ephemeral alerts at the top-middle of the screen for 4 seconds without removing them from the store.
In order to add alerts to the AlertHandler, you need to modify your callback to include an extra State and Output and append your alert to the list of existing alerts.
Example
- @callback(
Output(“alert_store”, “data”, allow_duplicate=True), State(“alert_store”, “data”),
) def callback_function_with_alert(alert_log):
- alert_log.append(
- create_alert(
f”Your message”, “info”, # The type of alert ephemeral=True, # If true, pops up as a notification
)
) return alert_log
- callbacks()¶
Registers Dash callbacks for the AlertHandler functionality.
This method defines callbacks for: - Toggling the alert modal. - Setting the alert filter based on user input. - Displaying alerts in the modal, filtered by type. - Removing dismissed alerts from the store. - Displaying ephemeral alerts.
- Return type:
None
Notes
Alerts must be added to each callback to ensure proper functionality.
- layout()¶
Creates the layout for the AlertHandler.
The layout includes: - dcc.Store components for storing all alerts and the current filter. - A fixed container for displaying ephemeral alerts. - An interval component to drive ephemeral updates. - A modal with filter buttons and a dismissable alert container. - A button to open the modal.
- Returns:
A Dash HTML Div component containing the layout for the AlertHandler.
- Return type:
html.Div
- create_alert(message, color='info', ephemeral=False)¶
Creates a standardized alert record.
- Parameters:
message (str) – The alert message to display.
color (str, optional) – The color of the alert, typically ‘info’, ‘warning’, or ‘danger’. Defaults to ‘info’.
ephemeral (bool, optional) – If True, the alert appears at the top-center for 4 seconds but remains in the store for the modal. Defaults to False.
- Returns:
A dictionary containing the alert details, including timestamp, message, color, and ephemeral status.
- Return type:
dict[str, Any]