Package: tagz
tagztagz– is an extremely simple library for building html documents without using templates,
just with python code.fromtagzimportPage,StyleSheet,Style,htmlpage=Page(lang="en",body_element=html.body(html.h1("Hello"),html.div(html.strong("world"),),html.a("example link",html.i("with italic text"),href="https://example.com/"),),head_elements=(html.meta(charset="utf-8"),html.meta(name="viewport",content="width=device-width, initial-scale=1"),html.title("tagz example page"),html.link(href="/static/css/bootstrap.min.css",rel="stylesheet"),html.script(src="/static/js/bootstrap.bundle.min.js"),html.style(StyleSheet({"body":Style(padding="0",margin="0"),(".container",".container-fluid"):Style(transition="opacity 600ms ease-in"),}))),)# `pretty=False` should be faster but performs not a human-readable resultprint(page.to_html5(pretty=True))writes something like this:<!doctype html><htmllang="en"><head><metacharset="utf-8"/><metaname="viewport"content="width=device-width, initial-scale=1"/><title>tagz example page</title><linkhref="/static/css/bootstrap.min.css"rel="stylesheet"/><scriptsrc="/static/js/bootstrap.bundle.min.js"></script><style>body{padding:0;margin:0;}.container,.container-fluid{transition:opacity600msease-in;}</style></head><body><h1>Hello</h1><div><strong>world</strong></div><ahref="https://example.com/">example link<i>with italic text</i></a></body></html>FeaturesAny custom tags is supported:fromtagzimporthtmlassertstr(html.my_custom_tag("hello"))=="<my-custom-tag>hello</my-custom-tag>"Pretty printing htmlfromtagzimporthtmlprint(html.div("Hello",html.strong("world"),).to_string(pretty=True))#<div>#	Hello#	<strong>#		world#	</strong>#</div>Stylehelper object:fromtagzimportStyleassertstr(Style(color="#ffffff"))=="color: #ffffff;"StyleSheethelper objectfromtagzimportStyle,StyleSheet# body {padding: 0;margin: 0}# a, div {transition: opacity 600ms ease-in}print(str(StyleSheet({"body":Style(padding="0",margin="0"),("div","a"):Style(transition="opacity 600ms ease-in"),})))More examplesBuilding page from partsYou can reuse the code, and assemble the page piece by piece,
to do this you can modify elements already added to the tags:fromtagzimporthtml,Page# Make an content elementcontent=html.div(id='content')page=Page(lang="en",body_element=html.body(html.h1("Example page"),html.hr(),# Adding it to the pagecontent,),head_elements=(html.meta(charset="utf-8"),html.title("tagz partial page"),),)content.append("Example page content")print(page.to_html5(pretty=True))This prints something like this:<!doctype html><htmllang="en"><head><metacharset="utf-8"/><title>tagz example page</title></head><body><h1>Example page</h1><hr/><divid="content">Example page content</div></body></html>Convert CSV to html tablefromioimportStringIOfromurllib.requestimporturlopenfromcsvimportreaderfromtagzimporthtml,Page,Styleurl=('https://media.githubusercontent.com/media/datablist/''sample-csv-files/main/files/organizations/''organizations-10000.csv')csv=reader(StringIO(urlopen(url).read().decode()))table=html.table(border='1',style=Style(border_collapse="collapse"))content=list(csv)# Make table headertable.append(html.tr(*map(html.th,content[0])))# Add table rowsforcsv_rowincontent[1:]:table.append(html.tr(*map(html.td,csv_row)))page=Page(lang="en",body_element=html.body(html.h1("Converted CSV"),table,"Content of this page has been automatically converted from",html.a(url,href=url),),head_elements=(html.meta(charset="utf-8"),html.title("tagz csv example page"),),)withopen("/tmp/csv.html","w")asfp:fp.write(page.to_html5())