Coverage for src/ipyvizzustory/env/st/story.py: 100%
19 statements
« prev ^ index » next coverage.py v7.5.4, created at 2024-07-10 10:22 +0000
« prev ^ index » next coverage.py v7.5.4, created at 2024-07-10 10:22 +0000
1"""A module for working with presentation stories in `Streamlit` environment."""
3from typing import Optional, Tuple
5from streamlit.components.v1 import html
7from ipyvizzu import Data, Style
9from ipyvizzustory.storylib.story import StorySize, Story as StoryLib
12class Story(StoryLib):
13 """A class for representing a presentation story in `Streamlit` environment."""
15 def __init__(self, data: Data, style: Optional[Style] = None):
16 """
17 Presentation Story constructor.
19 Args:
20 data: Data set for the whole presentation story.
21 After initialization `data` can not be modified,
22 but it can be filtered.
23 style: Style settings for the presentation story.
24 `style` can be changed at each presentation step.
25 """
27 super().__init__(data=data, style=style)
29 def _get_width_height(self) -> Tuple[Optional[int], int]:
30 if self._size.width == "100%" and StorySize.is_pixel(self._size.height):
31 return None, int(float(self._size.height[:-2])) # type: ignore
32 try:
33 return self._size.get_width_height_in_pixels()
34 except ValueError as error:
35 if str(error) == StorySize.ERROR_MSG_WIDTH_AND_HEIGHT:
36 raise ValueError(
37 f"{StorySize.ERROR_MSG_WIDTH_AND_HEIGHT} or width should be 100%"
38 ) from error
39 raise error
41 def play(self) -> None:
42 """A method for displaying the assembled `HTML` code in `Streamlit` environment."""
44 _width, _height = self._get_width_height()
46 html(
47 self.to_html(),
48 width=_width,
49 height=_height,
50 )