context.py 975 B

12345678910111213141516171819202122232425262728293031323334
  1. from __future__ import annotations
  2. from typing import TYPE_CHECKING, List
  3. from .slot import Slot
  4. if TYPE_CHECKING:
  5. from .client import Client
  6. class Context:
  7. @property
  8. def slot_stack(self) -> List[Slot]:
  9. """Return the slot stack of the current asyncio task."""
  10. return Slot.get_stack()
  11. @property
  12. def slot(self) -> Slot:
  13. """Return the current slot."""
  14. slot_stack = self.slot_stack
  15. if not slot_stack:
  16. raise RuntimeError('The current slot cannot be determined because the slot stack for this task is empty.\n'
  17. 'This may happen if you try to create UI from a background task.\n'
  18. 'To fix this, enter the target slot explicitly using `with container_element:`.')
  19. return slot_stack[-1]
  20. @property
  21. def client(self) -> Client:
  22. """Return the current client."""
  23. return self.slot.parent.client
  24. context = Context()