NiceGUI with async classes
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
These notes build upon the class based NiceGUI app written about here, and modify it to be async. It may help to read those notes first.
Why use async for NiceGUI? §
The NiceGUI FAQ has some good answers.
Async init for Python classes §
Typically the init for a Python class look as follows:
However, if there are async methods that need to be called during init, we cannot place them in the __init__
method because it is not async.
Instead, we can use the factory pattern to add an async create
class method:
Then it is permissable to use await
inside the create
method.
Using async classes with NiceGUI §
Applying the above to our async class, we can do the following:
This enables us to use async methods within the create
method of MyGUI
, e.g.: