def table(self, data, border=True): elems = [] table_arr = None if isinstance(data, pd.DataFrame): table_arr = list(data.values.tolist()) elif isinstance(data, list): table_arr = data else: raise ValueError("Table should be array") for row in table_arr: res = [] if isinstance(row, list): for i in range(len(row)): cell = row[i] if isinstance(data, pd.DataFrame): col = list(data.columns)[i] res.append(tags.Td(self.__convert(cell, col))) else: res.append( tags.Td(tags.Text(cell) if isinstance(cell, str) else cell) ) else: res.append(tags.Td(tags.Text(row) if isinstance(row, str) else row)) elems.append(tags.Tr(res)) tab = tags.Table( attributes.InlineStyle(width="100%"), attributes.Class("table_border") if border else None, elems, ) return tags.P(tab)
def address(self, title, content): return tags.Address( attributes.InlineStyle( font_size="16px", font_style="normal", font_weight="400", line_height="24px", ), tags.Strong(tags.Text(title)), tags.Text(content), )
def table(self, data, border=True): self.deprecatedPrint() elems = [] table_arr = None row_link = False row_link_index = -1 if isinstance(data, pd.DataFrame): table_arr = list(data.values.tolist()) row_link = True if "row_link" in data.columns else False try: row_link_index = list(data.columns).index("row_link") except ValueError: pass elif isinstance(data, list): table_arr = data else: error_text = f"Table should be array, not {data.__name__}" self.print_error(error_text) return None for row in table_arr: res = [] if isinstance(row, list): for i in range(len(row)): cell = row[i] if isinstance(data, pd.DataFrame): col = list(data.columns)[i] if row_link: if col != "row_link": link = row[row_link_index] res.append( tags.Td( tags.A( attributes.Href(link), self.__convert(cell, col), ))) else: res.append(tags.Td(self.__convert(cell, col))) else: res.append( tags.Td( tags.Text(cell) if isinstance(cell, str ) else cell)) else: res.append( tags.Td(tags.Text(row) if isinstance(row, str) else row)) elems.append(tags.Tr(res)) tab = tags.Table( attributes.InlineStyle(width="100%"), attributes.Class("table_border") if border else None, elems, ) return tags.P(tab)
def button( self, link, text="Open", width="auto", color="white", background_color="black", ): self.deprecatedPrint() return tags.Center( tags.Div( attributes.InlineStyle(margin="48px 0"), tags.A( attributes.Class("button"), attributes.Href(link), attributes.InlineStyle( background_color=background_color, color=color, border_radius="4px", display="inline-block", font_family="sans-serif", font_size="18px", font_weight="bold", line_height="60px", text_align="center", text_decoration="none", width=width, max_width="300px", padding_left="10px", padding_right="10px", _webkit_text_size_adjust="none", ), tags.Text(text), ), ))
def link(self, link, title="Open", color="#B200FD"): self.deprecatedPrint() return tags.A( attributes.Href(link), attributes.InlineStyle(color=color, text_decoration="underline"), tags.Text(title), )
def button(self, text, link, color="#ffffff", background_color="#B200FD"): return tags.Center( tags.Div( attributes.InlineStyle(margin="48px 0"), tags.A( attributes.Class("button"), attributes.Href(link), attributes.InlineStyle( background_color=background_color, color=color, border_radius="4px", display="inline-block", font_family="sans-serif", font_size="18px", font_weight="bold", line_height="60px", text_align="center", text_decoration="none", width="300px", _webkit_text_size_adjust="none", ), tags.Text(text), ), ) )
def text(self, text, font_size="18px"): self.deprecatedPrint() return tags.P( tags.Text(text), attributes.InlineStyle(font_size=font_size, padding_left="10px", padding_right="10px"), )
def title(self, title, heading=None): self.deprecatedPrint() return tags.H1( attributes.InlineStyle( color="#000000", font_size="32px", font_weight="800", line_height="32px", margin="48px 0", text_align="center", ), tags.Text(title), tags.Br(), (tags.Span( attributes.InlineStyle( font_size="24px", font_weight="600", color="darkgray"), tags.Text(heading), ) if heading else None), )
def subtitle(self, subtitle): return tags.H2( attributes.InlineStyle( color="#000000", font_size="28px", font_weight="600", line_height="32px", margin="48px 0 24px 0", text_align="center", ), tags.Text(subtitle), )
def footer(self, text, first=None, *elems): one = [ attributes.InlineStyle( font_size="16px", font_weight="400", line_height="24px", margin_top="48px", ), tags.Text(text), first, ] return tags.Footer(tags.P(one), *elems)
def heading(self, text): self.deprecatedPrint() return tags.H2( attributes.InlineStyle( color="#000000", font_size="28px", font_weight="600", line_height="32px", margin="48px 0 24px 0", text_align="center", ), tags.Text(text), )
def title(self, title, subtitle=None): return tags.H1( attributes.InlineStyle( color="#000000", font_size="32px", font_weight="800", line_height="32px", margin="48px 0", text_align="center", ), tags.Text(title), tags.Br(), ( tags.Span( attributes.InlineStyle( font_size="24px", font_weight="600", color="darkgray" ), tags.Text(subtitle), ) if subtitle else None ), )
def table(self, cells, column=2, border=False): elems = [] if len(cells) > column: sliced = slice_per(cells, column) for row in sliced: res = [] for cell in row: res.append( tags.Td(tags.Text(cell) if isinstance(cell, str) else cell) ) elems.append(tags.Tr(res)) else: row = cells res = [] for cell in row: res.append(tags.Td(tags.Text(cell) if isinstance(cell, str) else cell)) elems.append(tags.Tr(res)) tab = tags.Table( attributes.InlineStyle(width="100%"), attributes.Class("table_border") if border else None, elems, ) return tags.P(tab)
def main( self, content=None, sub_title=None, cover=None, table=None, button=None, footer=None, ): return [ tags.Main( cover, self.subtitle(sub_title) if sub_title else None, tags.P(tags.Text(content) if isinstance(content, str) else content), table, button, ), footer, ]
def text(self, text, font_size="18px"): return tags.P(tags.Text(text), attributes.InlineStyle(font_size=font_size))
def link(self, link, title): return tags.A( attributes.Href(link), attributes.InlineStyle(color="#B200FD", text_decoration="underline"), tags.Text(title), )
def generate(self, title, logo=None, display="embed", footer=None, **kwargs): self.deprecatedPrint() gen_html = tags.Html( attributes.Lang("en"), tags.Head( tags.Meta( attributes.HttpEquiv("Content-Type"), attributes.Content("text/html; charset=utf-8"), ), tags.Meta( attributes.HttpEquiv("Content-Type"), attributes.Content("width=device-width, initial-scale=1"), ), tags.Meta( attributes.Name("viewport"), attributes.Content("width=device-width, initial-scale=1"), ), tags.Meta( attributes.HttpEquiv("X-UA-Compatible"), attributes.Content("IE=edge"), ), tags.Style(tags.Text(base_style)), tags.Title(tags.Text(title)), ), tags.Body( attributes.InlineStyle(margin="0 !important", padding="0 !important"), tags.Div( attributes.InlineStyle(display="none", max_height="0", overflow="hidden"), tags.Text(title), ), tags.Div( attributes.InlineStyle(display="none", max_height="0", overflow="hidden"), tags.Text(" " * 240), ), tags.Text(table_ie9), tags.Div( { "name": "role", "value": "article" }, { "name": "aria-label", "value": title }, attributes.Lang("en"), attributes.Class("basic_font"), attributes.InlineStyle( background_color="white", color="#2b2b2b", font_size="18px", font_weight="400", line_height="28px", margin="0 auto", max_width="720px", padding="40px 20px 40px 20px", ), self.header(logo, self.title(title)), self.main(**kwargs), footer, ), tags.Text(table_ie9_close), ), ) res = gen_html.render() self.__display(res, display) return res
def text(self, text, *agrs): return tags.P(*agrs, tags.Text(text))