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.: