refreshable.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from typing import Callable, List
  2. from typing_extensions import Self
  3. from ..dependencies import register_component
  4. from ..element import Element
  5. register_component('refreshable', __file__, 'refreshable.js')
  6. class refreshable:
  7. def __init__(self, func: Callable) -> None:
  8. """Refreshable UI functions
  9. The `@ui.refreshable` decorator allows you to create functions that have a `refresh` method.
  10. This method will automatically delete all elements created by the function and recreate them.
  11. """
  12. self.func = func
  13. self.instance = None
  14. self.containers: List[Element] = []
  15. def __get__(self, instance, _) -> Self:
  16. self.instance = instance
  17. return self
  18. def __call__(self) -> None:
  19. with Element('refreshable') as container:
  20. self.func() if self.instance is None else self.func(self.instance)
  21. self.containers.append(container)
  22. def refresh(self) -> None:
  23. for container in self.containers:
  24. container.clear()
  25. with container:
  26. self.func() if self.instance is None else self.func(self.instance)