标签z是一个HTML标签构建器
项目描述
标签z
标签z
– 是一个极其简单的库,用于仅使用Python代码构建HTML文档,而无需使用模板。
from tagz import Page, StyleSheet, Style, html
page = 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 result
print(page.to_html5(pretty=True))
写入类似以下内容
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>
tagz example page
</title>
<link href="/static/css/bootstrap.min.css" rel="stylesheet"/>
<script src="/static/js/bootstrap.bundle.min.js">
</script>
<style>
body {padding: 0; margin: 0;}
.container, .container-fluid {transition: opacity 600ms ease-in;}
</style>
</head>
<body>
<h1>
Hello
</h1>
<div>
<strong>
world
</strong>
</div>
<a href="https://example.com/">
example link
<i>
with italic text
</i>
</a>
</body>
</html>
特性
- 支持任何自定义标签
from tagz import html assert str(html.my_custom_tag("hello")) == "<my-custom-tag>hello</my-custom-tag>"
- 美观打印HTML
from tagz import html print( html.div( "Hello", html.strong("world"), ).to_string(pretty=True) ) #<div> # Hello # <strong> # world # </strong> #</div>
样式
辅助对象from tagz import Style assert str(Style(color="#ffffff")) == "color: #ffffff;"
样式表
辅助对象from tagz import Style, 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"), }) ) )
更多示例
从部分构建页面
您可以重用代码,并逐块组装页面,为此您可以修改已添加到标签中的元素
from tagz import html, Page
# Make an content element
content = html.div(id='content')
page = Page(
lang="en",
body_element=html.body(
html.h1("Example page"),
html.hr(),
# Adding it to the page
content,
),
head_elements=(
html.meta(charset="utf-8"),
html.title("tagz partial page"),
),
)
content.append("Example page content")
print(page.to_html5(pretty=True))
这会打印出类似以下内容
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>
tagz example page
</title>
</head>
<body>
<h1>
Example page
</h1>
<hr/>
<div id="content">
Example page content
</div>
</body>
</html>
将CSV转换为HTML表格
from io import StringIO
from urllib.request import urlopen
from csv import reader
from tagz import html, Page, Style
url = (
'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 header
table.append(html.tr(*map(html.th, content[0])))
# Add table rows
for csv_row in content[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"),
),
)
with open("/tmp/csv.html", "w") as fp:
fp.write(page.to_html5())
项目详情
下载文件
下载适用于您平台的应用程序。如果您不确定选择哪个,请了解更多关于安装包的信息。
源代码分发
tagz-0.3.1.tar.gz (5.3 kB 查看散列)
构建分发
tagz-0.3.1-py3-none-any.whl (5.5 kB 查看散列)