Coverage for src/ipyvizzustory/env/st/story.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-25 14:00 +0100

1"""A module for working with presentation stories in Streamlit environment.""" 

2 

3from typing import Optional 

4 

5from streamlit.components.v1 import html 

6 

7from ipyvizzu import Data, Style 

8 

9from ipyvizzustory.storylib.story import Story as StoryLib 

10 

11 

12class Story(StoryLib): 

13 """A class for representing a presentation story in Streamlit environment.""" 

14 

15 def __init__(self, data: Data, style: Optional[Style] = None): 

16 """ 

17 Presentation Story constructor. 

18 

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 (optional): Style settings for the presentation story. 

24 `style` can be changed at each presentation step. 

25 """ 

26 

27 super().__init__(data=data, style=style) 

28 self.set_size(800, 480) 

29 

30 def set_size( # type: ignore # pylint: disable=signature-differs 

31 self, width: int, height: int 

32 ) -> None: 

33 """ 

34 A method for overwriting `ipyvizzustory.storylib.story.Story.set_size()` method. 

35 In Streamlit environment `width` and `height` must be specified in pixels. 

36 

37 Args: 

38 width: Width of the presentation story in pixels. 

39 height: Height of the presentation story in pixels. 

40 

41 Raises: 

42 ValueError: If `width` or `height` is not instance of `int`. 

43 """ 

44 

45 if any([not isinstance(width, int), not isinstance(height, int)]): 

46 raise ValueError("width and height should be in pixels as int") 

47 super().set_size(width=str(width) + "px", height=str(height) + "px") 

48 

49 def play(self) -> None: 

50 """A method for displaying the assembled html code in Streamlit environment.""" 

51 

52 html( 

53 self.to_html(), 

54 width=int(self._size.width[:-2]), # type: ignore 

55 height=int(self._size.height[:-2]), # type: ignore 

56 )