如何使yattag源更好地格式化?

时间:2015-05-14 22:27:54

标签: html python-3.x line-breaks yattag

我使用yattag生成HTML:

doc, tag, text = Doc().tagtext()

with tag("p"):
    text("My paragraph.")

虽然它运行良好,但当我查看HTML源代码时,它会在一行中出现。是否有某种切换或其他技巧使yattag创建更可读的源?

1 个答案:

答案 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>