-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_components.py
More file actions
158 lines (127 loc) · 4.95 KB
/
Copy pathhtml_components.py
File metadata and controls
158 lines (127 loc) · 4.95 KB
1
2
3
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
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
153
154
155
156
157
158
"""
This module contains the classes for the HTMLComponents
used to build the resulting html document with nested objects
"""
from typing import List, Tuple
from dataclasses import dataclass
@dataclass
class HTMLComponent:
"""Abstract class used for an abstract representation of a html component object"""
def __init__(self):
pass
def __str__(self):
raise Exception("Class does not yet override the __str__ method")
class HTMLContainer(HTMLComponent):
"""Class used to represent a html div/container object inheriting from html component object"""
def __init__(self):
super().__init__()
self.children: List[HTMLComponent] = []
def add_component(self, child: HTMLComponent):
"""Adds handed html component object to the list of children in this object in order to nest html objects"""
self.children.append(child)
def __str__(self) -> str:
return f'<div class="container">{"".join([str(child) for child in self.children])}</div>'
@dataclass
class HTMLTableCell(HTMLComponent):
"""Class used to represent an html table cell object inheriting from html component object"""
def __init__(self, data, is_td=True):
super().__init__()
self.data = data
self.is_td = is_td
def __str__(self) -> str:
if self.is_td:
return f"<td>{self.data}</td>"
return f"<th>{self.data}</td>"
@dataclass
class HTMLTableRow(HTMLComponent):
"""Class used to represent a html table row object inheriting from html component object"""
def __init__(self):
super().__init__()
self.cells = []
def add_cell(self, cell: HTMLTableCell):
"""Add handed html cell object to the cells list of the object"""
self.cells.append(cell)
def __str__(self) -> str:
return f'<tr>{"".join([str(cell) for cell in self.cells])}</tr>'
@dataclass
class HTMLTable(HTMLComponent):
"""Class used to represent a html table object inheriting from html component object"""
def __init__(self, head: List):
super().__init__()
self.head: HTMLTableRow = HTMLTableRow()
for item in head:
self.head.add_cell(HTMLTableCell(item, is_td=False))
self.body: List[HTMLTableRow] = []
def add_row(self, data: Tuple):
"""Converts haded one dimensional data list to table row object and stored in this objects body list"""
row: HTMLTableRow = HTMLTableRow()
for item in data:
row.add_cell(HTMLTableCell(item))
self.body.append(row)
def __str__(self) -> str:
str_body = "\n".join([str(component) for component in self.body])
return f"""
<table>
<thead>
<tr>{str(self.head)}</tr>
</thead>
<tbody>
{str_body}
</tbody>
</table>
"""
@dataclass
class HTMLFile(HTMLComponent):
"""Class used to represent an html file object inheriting from html component"""
def __init__(self, title: str):
super().__init__()
self.title = title
self.components = []
def add_component(self, component: HTMLComponent) -> None:
"""Adds handed html component object to components list of this instance in order to nest html components"""
self.components.append(component)
def __str__(self) -> str:
str_body = "\n".join([str(component) for component in self.components])
return f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>{self.title}</title>
</head>
<body>
{str_body}
</body>
</html>
"""
@dataclass
class HTMLParagraph(HTMLComponent):
"""Class use to represent a html paragraph object inheriting from html component object"""
def __init__(self, text, bold=False):
super().__init__()
self.text = text
self.bold = bold
def __str__(self) -> str:
if self.bold:
return f'<p><strong>{self.text}</strong></p>'
return f'<p>{self.text}</p>'
@dataclass
class HTMLHeadline(HTMLComponent):
"""Class used to represent a html headline object inheriting from html component object"""
def __init__(self, text, level=1):
super().__init__()
self.text = text
self.level = level
def __str__(self) -> str:
return f'<h{self.level}>{self.text}</h{self.level}>'
@dataclass
class HTMLiFrame(HTMLComponent):
"""Class used to represent a html iframe object inheriting from html component object"""
def __init__(self, src: str):
super().__init__()
self.src = src
def __str__(self):
return f'<iframe src="{self.src}" frameborder="0" height="400"></iframe>'