在Python使用 tinyhtml 模块生成 HTML
创建 HTML 有时可能是一项乏味的任务,并且难以调试且容易出错。解决这个问题的一种方法是使用一些可以处理打开和关闭 div 等的库,这可以减少出错的机会。为此,我们将使用tinyhtml模块。
该模块提供了一组有助于使用Python代码呈现 html5 的类,并允许将多个 HTML 标签组合在一起。它还有助于发布原始未转义的 HTML,使用构建器提供循环或类型转换的功能。
安装
要安装此模块,请在终端中键入以下命令。
pip install tinyhtml
使用的功能
- html() :标记 html 代码的开始。
- h() :大多数实用函数,允许渲染属性、普通元素和空/自关闭元素。
- raw() :用于打印未转义的 html字符串。
- frag() :将几个 HTML 标签组合在一起。
- render() :处理和转换输入的 html。
示例 1:
Python3
from tinyhtml import html, h
# Constructing HTML using html() and h()
# nested h() is also supported
html_content = html(lang="en")(
h("head")(
(h("h1")("hello Geeks!!")),
),
).render()
# printing html formed on console.
print(html_content)
Python3
from tinyhtml import html, h, frag, raw
# using frag() to group to h fncs.
print("Working of frag() : ")
html_content = html(lang="en")(
frag(h("h1")("Welcome to GFG"), h("p")("This\
is one among best ever coding site you've been\
to.."))).render()
print(html_content)
print("\n")
# prints raw unescaped HTML.
print("The unescaped HTML raw content : ")
print(raw('Printing Raw HTML
Dont escape <<>>>>
'))
Python3
from tinyhtml import h
# using klass to initialize class
print("Working with Class : ")
class_inp = h("div", klass="gfg")()
print(class_inp)
# using _ to escape for loop operator
print("Working with label and escaping keyword : ")
label_inp = h("label", for_="geeksforgeeks")("GFG")
print(label_inp)
Python3
from tinyhtml import h
# initializing loop elements
print("Using loop elements : ")
looped_element = h("ul")(h("li")(idx) for idx in range(5))
print(looped_element)
print("\n")
# using conditionals
print("Using conditional elements : ")
conditional_element = h("ul")(
h("li")("Gfg") if False else "GFG", h("li")("Geeks"))
print(conditional_element)
Python3
from tinyhtml import h, html, frag
# function to create layout.
# advantage is that this can be reused.
def create_layout(title, body):
return html()(
h("head")(
h("title")(title),
),
h("body")(body)
)
# calling function to create layout.
layout = create_layout("Gfg Templating", frag(
h("h1")("Demo Heading"),
h("p")("Making fragment to demo templating layout"),
))
print("Created layout : ")
print(layout)
输出:
示例 2:使用 raw() 和 frag()
蟒蛇3
from tinyhtml import html, h, frag, raw
# using frag() to group to h fncs.
print("Working of frag() : ")
html_content = html(lang="en")(
frag(h("h1")("Welcome to GFG"), h("p")("This\
is one among best ever coding site you've been\
to.."))).render()
print(html_content)
print("\n")
# prints raw unescaped HTML.
print("The unescaped HTML raw content : ")
print(raw('Printing Raw HTML
Dont escape <<>>>>
'))
输出 :
示例 3:将类和标签用作 HTML
在这里,我们使用“ klass ”运算符来初始化一个类。对于其他可能与Python保留关键字命名一致的标签,会附加一个尾随下划线。
蟒蛇3
from tinyhtml import h
# using klass to initialize class
print("Working with Class : ")
class_inp = h("div", klass="gfg")()
print(class_inp)
# using _ to escape for loop operator
print("Working with label and escaping keyword : ")
label_inp = h("label", for_="geeksforgeeks")("GFG")
print(label_inp)
输出 :
示例 4:使用循环和条件
需要循环(如列表元素)和条件的 HTML 内容也可以通过基本的Python循环和条件来呈现。
蟒蛇3
from tinyhtml import h
# initializing loop elements
print("Using loop elements : ")
looped_element = h("ul")(h("li")(idx) for idx in range(5))
print(looped_element)
print("\n")
# using conditionals
print("Using conditional elements : ")
conditional_element = h("ul")(
h("li")("Gfg") if False else "GFG", h("li")("Geeks"))
print(conditional_element)
输出 :
示例 5:使用函数模板化 HTML
蟒蛇3
from tinyhtml import h, html, frag
# function to create layout.
# advantage is that this can be reused.
def create_layout(title, body):
return html()(
h("head")(
h("title")(title),
),
h("body")(body)
)
# calling function to create layout.
layout = create_layout("Gfg Templating", frag(
h("h1")("Demo Heading"),
h("p")("Making fragment to demo templating layout"),
))
print("Created layout : ")
print(layout)
输出 :