Skip to content

Page

src.drawpyo.page.Page

Source code in src/drawpyo/page.py
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
class Page:
    def __init__(self, file: Optional[Any] = None, **kwargs: Any) -> None:
        """
        Args:
            file (Optional[File]): Parent File object this page belongs to.

        Keyword Args:
            objects (list[Any]): Initial list of objects on the page.

            name (str): Page name. Default: "Page-{page_num}"
            page_num (int): Page index within the file. Default: inferred from file.

            dx (int | float): Horizontal translation of the page. Default: 2037
            dy (int | float): Vertical translation of the page. Default: 830
            scale (int | float): Page scale factor. Default: 1

            grid (int): Whether grid is enabled. Default: 1
            grid_size (int): Grid spacing. Default: 10
            guides (int): Whether alignment guides are enabled. Default: 1
            tooltips (int): Whether tooltips are enabled. Default: 1
            connect (int): Whether connections are enabled. Default: 1
            arrows (int): Whether arrows are enabled. Default: 1
            fold (int): Whether page folding is enabled. Default: 1

            size_preset (PageSize): Optional predefined page size. Overrides width and height.
            width (int | float): Page width. Default: 850
            height (int | float): Page height. Default: 1100

            math (int): Whether math rendering is enabled. Default: 0
            shadow (int): Whether shadows are enabled. Default: 0
        """
        super().__init__()
        self.id: int = id(self)

        self.file: Optional[File] = file
        self.objects: List[Any] = kwargs.get("objects", [])

        # There are two empty top level objects in every Draw.io diagram
        self.objects.append(XMLBase(id=0, xml_class="mxCell"))
        self.objects.append(XMLBase(id=1, xml_class="mxCell", xml_parent=0))

        # Properties

        if self.file is not None:
            page_num = len(self.file.pages)
        else:
            page_num = 1
        self.name: str = kwargs.get("name", f"Page-{page_num}")
        self.page_num: int = kwargs.get("page_num", page_num)

        self.dx: Union[int, float] = kwargs.get("dx", 2037)
        self.dy: Union[int, float] = kwargs.get("dy", 830)
        self.grid: int = kwargs.get("grid", 1)
        self.grid_size: int = kwargs.get("grid_size", 10)
        self.guides: int = kwargs.get("guides", 1)
        self.tooltips: int = kwargs.get("tooltips", 1)
        self.connect: int = kwargs.get("connect", 1)
        self.arrows: int = kwargs.get("arrows", 1)
        self.fold: int = kwargs.get("fold", 1)
        self.scale: Union[int, float] = kwargs.get("scale", 1)
        self.size_preset: Optional[PageSize] = kwargs.get("size_preset", None)
        if self.size_preset and ("width" in kwargs or "height" in kwargs):
            logger.warning(
                "Page created with size_preset - This overrides width/height arguments."
            )
        self.width: Union[int, float] = (
            kwargs.get("width", 850) if not self.size_preset else self.size_preset[0]
        )
        self.height: Union[int, float] = (
            kwargs.get("height", 1100) if not self.size_preset else self.size_preset[1]
        )
        self.math: int = kwargs.get("math", 0)
        self.shadow: int = kwargs.get("shadow", 0)

        # In the Draw.io file format, each page is actually three nested XML
        # tags. These are defined as XMLBase subclasses below
        self.diagram: Diagram = Diagram(name=self.name)
        self.mxGraph: mxGraph = mxGraph(page=self)
        self.root: Root = Root()

        logger.info(f"📄 Page created: '{self.__repr__()}'")

    def __repr__(self) -> str:
        return f"drawpyo Page - {self.name}"

    def remove(self) -> None:
        """This function removes the Page from its linked File object then deletes itself."""
        if self.file is not None:
            self.file.remove_page(self)
        del self

    def add_object(self, obj: Any) -> None:
        if obj not in self.objects:
            self.objects.append(obj)

    def remove_object(self, obj: Any) -> None:
        self.objects.remove(obj)

    @property
    def file(self) -> Optional[Any]:
        return self._file

    @file.setter
    def file(self, f: Optional[Any]) -> None:
        if f is not None:
            f.add_page(self)
        self._file = f

    @file.deleter
    def file(self) -> None:
        self._file.remove_page(self)
        self._file = None

    ###########################################################
    # XML Generation
    ###########################################################
    @property
    def xml(self) -> str:
        xml_string = self.xml_open_tag
        for obj in self.objects:
            xml_string = xml_string + "\n        " + obj.xml
        xml_string = xml_string + "\n" + self.xml_close_tag
        return xml_string

    @property
    def xml_open_tag(self) -> str:
        tag = (
            self.diagram.xml_open_tag
            + "\n    "
            + self.mxGraph.xml_open_tag
            + "\n      "
            + self.root.xml_open_tag
        )
        return tag

    @property
    def xml_close_tag(self) -> str:
        tag = (
            "      "
            + self.root.xml_close_tag
            + "\n    "
            + self.mxGraph.xml_close_tag
            + "\n  "
            + self.diagram.xml_close_tag
        )
        return tag

__init__(file=None, **kwargs)

Parameters:

Name Type Description Default
file Optional[File]

Parent File object this page belongs to.

None

Other Parameters:

Name Type Description
objects list[Any]

Initial list of objects on the page.

name str

Page name. Default: "Page-{page_num}"

page_num int

Page index within the file. Default: inferred from file.

dx int | float

Horizontal translation of the page. Default: 2037

dy int | float

Vertical translation of the page. Default: 830

scale int | float

Page scale factor. Default: 1

grid int

Whether grid is enabled. Default: 1

grid_size int

Grid spacing. Default: 10

guides int

Whether alignment guides are enabled. Default: 1

tooltips int

Whether tooltips are enabled. Default: 1

connect int

Whether connections are enabled. Default: 1

arrows int

Whether arrows are enabled. Default: 1

fold int

Whether page folding is enabled. Default: 1

size_preset PageSize

Optional predefined page size. Overrides width and height.

width int | float

Page width. Default: 850

height int | float

Page height. Default: 1100

math int

Whether math rendering is enabled. Default: 0

shadow int

Whether shadows are enabled. Default: 0

Source code in src/drawpyo/page.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def __init__(self, file: Optional[Any] = None, **kwargs: Any) -> None:
    """
    Args:
        file (Optional[File]): Parent File object this page belongs to.

    Keyword Args:
        objects (list[Any]): Initial list of objects on the page.

        name (str): Page name. Default: "Page-{page_num}"
        page_num (int): Page index within the file. Default: inferred from file.

        dx (int | float): Horizontal translation of the page. Default: 2037
        dy (int | float): Vertical translation of the page. Default: 830
        scale (int | float): Page scale factor. Default: 1

        grid (int): Whether grid is enabled. Default: 1
        grid_size (int): Grid spacing. Default: 10
        guides (int): Whether alignment guides are enabled. Default: 1
        tooltips (int): Whether tooltips are enabled. Default: 1
        connect (int): Whether connections are enabled. Default: 1
        arrows (int): Whether arrows are enabled. Default: 1
        fold (int): Whether page folding is enabled. Default: 1

        size_preset (PageSize): Optional predefined page size. Overrides width and height.
        width (int | float): Page width. Default: 850
        height (int | float): Page height. Default: 1100

        math (int): Whether math rendering is enabled. Default: 0
        shadow (int): Whether shadows are enabled. Default: 0
    """
    super().__init__()
    self.id: int = id(self)

    self.file: Optional[File] = file
    self.objects: List[Any] = kwargs.get("objects", [])

    # There are two empty top level objects in every Draw.io diagram
    self.objects.append(XMLBase(id=0, xml_class="mxCell"))
    self.objects.append(XMLBase(id=1, xml_class="mxCell", xml_parent=0))

    # Properties

    if self.file is not None:
        page_num = len(self.file.pages)
    else:
        page_num = 1
    self.name: str = kwargs.get("name", f"Page-{page_num}")
    self.page_num: int = kwargs.get("page_num", page_num)

    self.dx: Union[int, float] = kwargs.get("dx", 2037)
    self.dy: Union[int, float] = kwargs.get("dy", 830)
    self.grid: int = kwargs.get("grid", 1)
    self.grid_size: int = kwargs.get("grid_size", 10)
    self.guides: int = kwargs.get("guides", 1)
    self.tooltips: int = kwargs.get("tooltips", 1)
    self.connect: int = kwargs.get("connect", 1)
    self.arrows: int = kwargs.get("arrows", 1)
    self.fold: int = kwargs.get("fold", 1)
    self.scale: Union[int, float] = kwargs.get("scale", 1)
    self.size_preset: Optional[PageSize] = kwargs.get("size_preset", None)
    if self.size_preset and ("width" in kwargs or "height" in kwargs):
        logger.warning(
            "Page created with size_preset - This overrides width/height arguments."
        )
    self.width: Union[int, float] = (
        kwargs.get("width", 850) if not self.size_preset else self.size_preset[0]
    )
    self.height: Union[int, float] = (
        kwargs.get("height", 1100) if not self.size_preset else self.size_preset[1]
    )
    self.math: int = kwargs.get("math", 0)
    self.shadow: int = kwargs.get("shadow", 0)

    # In the Draw.io file format, each page is actually three nested XML
    # tags. These are defined as XMLBase subclasses below
    self.diagram: Diagram = Diagram(name=self.name)
    self.mxGraph: mxGraph = mxGraph(page=self)
    self.root: Root = Root()

    logger.info(f"📄 Page created: '{self.__repr__()}'")

remove()

This function removes the Page from its linked File object then deletes itself.

Source code in src/drawpyo/page.py
92
93
94
95
96
def remove(self) -> None:
    """This function removes the Page from its linked File object then deletes itself."""
    if self.file is not None:
        self.file.remove_page(self)
    del self

Page Size Preset

src.drawpyo.utils.page_sizes.PageSize

Bases: tuple, Enum

Predefined page sizes expressed as (width, height) in Draw.io units.

Source code in src/drawpyo/utils/page_sizes.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class PageSize(tuple, Enum):
    """
    Predefined page sizes expressed as (width, height) in Draw.io units.
    """

    USLETTERLANDSCAPE = (1100, 850)
    USLEGALLANDSCAPE = (1400, 850)
    USTABLOIDLANDSCAPE = (1700, 1100)
    USEXECUTIVELANDSCAPE = (1000, 700)
    USLETTERPORTRAIT = (850, 1100)
    USLEGALPORTRAIT = (850, 1400)
    USTABLOIDPORTRAIT = (1100, 1700)
    USEXECUTIVEPORTRAIT = (700, 1000)
    A0LANDSCAPE = (4681, 3300)
    A1LANDSCAPE = (3300, 2336)
    A2LANDSCAPE = (2336, 1654)
    A3LANDSCAPE = (1654, 1169)
    A4LANDSCAPE = (1169, 827)
    A5LANDSCAPE = (827, 583)
    A6LANDSCAPE = (583, 413)
    A7LANDSCAPE = (413, 291)
    A0PORTRAIT = (3300, 4681)
    A1PORTRAIT = (2336, 3300)
    A2PORTRAIT = (1654, 2336)
    A3PORTRAIT = (1169, 1654)
    A4PORTRAIT = (827, 1169)
    A5PORTRAIT = (583, 827)
    A6PORTRAIT = (413, 583)
    A7PORTRAIT = (291, 413)
    B4LANDSCAPE = (1390, 980)
    B5LANDSCAPE = (980, 690)
    B4PORTRAIT = (980, 1390)
    B5PORTRAIT = (690, 980)
    ASPECT16BY9 = (1600, 900)
    ASPECT16BY10 = (1920, 1200)
    ASPECT4BY3 = (1600, 1200)

    @property
    def width(self) -> int:
        return self[0]

    @property
    def height(self) -> int:
        return self[1]