NiceGUI: Show a confirmation popup
All notes in this series:
- NiceGUI: Always show main scrollbar
- NiceGUI: Show a confirmation popup
- NiceGUI: File upload and download
- FastAPI: Pretty print JSON
- NiceGUI with Click, Poetry, auto-reload and classes
- NiceGUI: tkinter error when updating pyplot
- NiceGUI: Bind visibility to arbitrary value
- NiceGUI: Change threshold for binding propagation warning
- NiceGUI with async classes
The NiceGUI dialog element can be used to create a confirmation popup or modal. With an awaitable dialog, it is easy to create an “Are you sure?” popup, to let a user confirm before doing an action. The official documentation provides a good example of a single dialog, but if you were to follow the same approach for multiple dialogs then the boilerplate code quickly multiplies.
To minimise the boilerplate code, you can re-use the same popup in multiple places.
Following is a simple example of one popup being used by two menus items:
This code works by defining a single dialog, binding to the text that is shown in the dialog, and updating the bound text each time the dialog is used. The action functions are only called if “Yes” is chosen on the popup.