Skip to content

Alerts

The Alert class provides imperative access to the host platform's alert dialogs and action sheets. Alerts are not part of the element tree — they're fire-and-forget calls that present a native dialog and dispatch button callbacks.

Imperative system alerts.

Modeled on React Native's Alert.alert(). Alerts are not part of the element tree — they're imperative, fire-and-forget calls that trigger a native dialog.

Example
import pythonnative as pn

def confirm_delete():
    pn.Alert.show(
        title="Delete item?",
        message="This action cannot be undone.",
        buttons=[
            {"label": "Cancel", "style": "cancel"},
            {
                "label": "Delete",
                "style": "destructive",
                "on_press": delete_item,
            },
        ],
    )

Classes:

Name Description
Alert

Imperative alert / action-sheet helper.

Alert

Imperative alert / action-sheet helper.

All methods are static. Use Alert.show() for an alert dialog and pass style="action_sheet" for an iOS-style action sheet.

Methods:

Name Description
show

Present a native alert dialog or action sheet.

confirm

Convenience wrapper for two-button confirm/cancel dialogs.

show staticmethod

show(*, title: str, message: Optional[str] = None, buttons: Optional[List[Dict[str, Any]]] = None, style: str = 'alert') -> None

Present a native alert dialog or action sheet.

Parameters:

Name Type Description Default
title str

Dialog title (required).

required
message Optional[str]

Optional body text.

None
buttons Optional[List[Dict[str, Any]]]

Each button is {"label": str, "style": "default"|"cancel"|"destructive", "on_press": callable}. Defaults to a single "OK" button.

None
style str

"alert" (default) or "action_sheet".

'alert'

On iOS this uses UIAlertController; on Android it uses AlertDialog.Builder. On the test backend the call is a no-op so unit tests don't need to mock UIKit/AndroidX.

confirm staticmethod

confirm(*, title: str, message: Optional[str] = None, confirm_label: str = 'OK', cancel_label: str = 'Cancel', on_confirm: Optional[Callable[[], None]] = None, on_cancel: Optional[Callable[[], None]] = None) -> None

Convenience wrapper for two-button confirm/cancel dialogs.

Patterns

  • Confirm before destructive actions: pair a "destructive" button with a "cancel" button via Alert.confirm.
  • Action sheets: pass style="action_sheet" to render an iOS-style bottom sheet; on Android this falls back to a regular dialog.
  • Pickers: the built-in Picker component is implemented on top of action sheets — use it for select/dropdown widgets.

Testing

When running off-device (e.g., in unit tests), Alert.show records each call to Alert._test_log instead of presenting a dialog. Reset the log with Alert._test_log.clear() between cases.