video.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from pathlib import Path
  2. from typing import Union
  3. from .. import core
  4. from ..element import Element
  5. class Video(Element, component='video.js'):
  6. def __init__(self, src: Union[str, Path], *,
  7. controls: bool = True,
  8. autoplay: bool = False,
  9. muted: bool = False,
  10. loop: bool = False,
  11. ) -> None:
  12. """Video
  13. Displays a video.
  14. :param src: URL or local file path of the video source
  15. :param controls: whether to show the video controls, like play, pause, and volume (default: `True`)
  16. :param autoplay: whether to start playing the video automatically (default: `False`)
  17. :param muted: whether the video should be initially muted (default: `False`)
  18. :param loop: whether the video should loop (default: `False`)
  19. See `here <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video#events>`_
  20. for a list of events you can subscribe to using the generic event subscription `on()`.
  21. """
  22. super().__init__()
  23. if Path(src).is_file():
  24. src = core.app.add_media_file(local_file=src)
  25. self._props['src'] = src
  26. self._props['controls'] = controls
  27. self._props['autoplay'] = autoplay
  28. self._props['muted'] = muted
  29. self._props['loop'] = loop
  30. def seek(self, seconds: float) -> None:
  31. """Seek to a specific position in the video.
  32. :param seconds: the position in seconds
  33. """
  34. self.run_method('seek', seconds)
  35. def play(self) -> None:
  36. """Play video."""
  37. self.run_method('play')
  38. def pause(self) -> None:
  39. """Pause video."""
  40. self.run_method('pause')