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.
There are three entry points:
Alert.show: fire-and-forget one-button notice (no return value).Alert.confirm: awaitable two-button yes/no, resolves to abool.Alert.choose: awaitable multi-button picker / action sheet, resolves to the selected label (orNoneif dismissed).
Imperative, awaitable system alerts.
Inspired by React Native's Alert.alert() but designed around
async / await instead of per-button callbacks. There are three
entry points:
Alert.show: fire-and-forget one-button notice (no return value).Alert.confirm: awaitable two-button yes/no, resolves to abool.Alert.choose: awaitable multi-button picker / action sheet, resolves to the selected label (orNoneif dismissed).
Example
Classes:
| Name | Description |
|---|---|
Alert |
Imperative alert / action-sheet helper. |
Alert
¶
Imperative alert / action-sheet helper.
All methods are static. Use show
for a fire-and-forget single-button notice,
confirm for an awaitable
yes/no dialog, and
choose for a multi-option
picker.
Methods:
| Name | Description |
|---|---|
set_test_response |
Queue indices to return from upcoming test alerts. |
show |
Display a simple, one-button alert and return immediately. |
confirm |
Present a two-button yes/no dialog and wait for the choice. |
choose |
Present a multi-option picker and wait for the user's choice. |
set_test_response
staticmethod
¶
set_test_response(*indices: int) -> None
Queue indices to return from upcoming test alerts.
Use in async tests to script the user's choices: each pending
call to confirm or
choose pops the next
queued index. Pass -1 to simulate a dismiss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*indices
|
int
|
Sequence of button indices to deliver, oldest
first. Calls beyond the queue length resolve to |
()
|
show
staticmethod
¶
Display a simple, one-button alert and return immediately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
Dialog title. |
required |
message
|
Optional[str]
|
Optional body text. |
None
|
button
|
str
|
Label for the single dismiss button (default
|
'OK'
|
This is fire-and-forget. To know what the user did, use
confirm or
choose and await
the result.
confirm
async
staticmethod
¶
confirm(title: str, message: Optional[str] = None, *, confirm_label: str = 'OK', cancel_label: str = 'Cancel') -> bool
Present a two-button yes/no dialog and wait for the choice.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
Dialog title. |
required |
message
|
Optional[str]
|
Optional body text. |
None
|
confirm_label
|
str
|
Label for the "yes" button (default
|
'OK'
|
cancel_label
|
str
|
Label for the "no" button (default
|
'Cancel'
|
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
for the cancel button or a dismiss. |
choose
async
staticmethod
¶
choose(title: str, options: Sequence[str], *, message: Optional[str] = None, cancel_label: Optional[str] = None, style: str = 'action_sheet', destructive_labels: Sequence[str] = ()) -> Optional[str]
Present a multi-option picker and wait for the user's choice.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
Dialog title. |
required |
options
|
Sequence[str]
|
Sequence of option labels (in display order). |
required |
message
|
Optional[str]
|
Optional body text. |
None
|
cancel_label
|
Optional[str]
|
If provided, adds a "cancel" button with
this label. Selecting it resolves to |
None
|
style
|
str
|
|
'action_sheet'
|
destructive_labels
|
Sequence[str]
|
Labels in |
()
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The selected label, or |
Optional[str]
|
tapped the cancel button. |
Patterns¶
- Confirm before destructive actions:
await pn.Alert.confirm(...)inside anasync def, then branch on the boolean result. - Pick from options:
await pn.Alert.choose(title, options=[...])returns the selected label. - Pickers: the built-in
Pickercomponent is implemented on top of action sheets; use it for select/dropdown widgets.
Testing¶
When running off-device (e.g., in unit tests), the alert dispatch
records each call to Alert._test_log instead of presenting a
dialog. Use
Alert.set_test_response(*indices)
to script the user's choices for upcoming confirm / choose
calls. Reset the log with Alert._test_log.clear() between cases.