我使用yattag
生成HTML:
doc, tag, text = Doc().tagtext()
with tag("p"):
text("My paragraph.")
虽然它运行良好,但当我查看HTML源代码时,它会在一行中出现。是否有某种切换或其他技巧使yattag
创建更可读的源?
答案 0 :(得分:2)
使用indent
功能。
from yattag import Doc, indent
doc, tag, text = Doc().tagtext()
with tag("p"):
text("My paragraph.")
print(indent(doc.getvalue())) # will indent the tags but not the text directly contained between <tag> and </tag>
print(indent(doc.getvalue(), indent_text = True)) # will also indent the text directly contained between <tag> and </tag>