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

Sup*_*est 3 html line-breaks python-3.x yattag

我正在使用yattag生成 HTML:

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

with tag("p"):
    text("My paragraph.")
Run Code Online (Sandbox Code Playgroud)

虽然它工作得很好,但当我查看 HTML 源代码时,它全部出现在一行中。是否有某种开关或其他技巧可以yattag创建更具可读性的源代码?

Joh*_*nal 6

使用该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>
Run Code Online (Sandbox Code Playgroud)