【问题标题】:How to make yattag source better formatted?如何使 yattag 源更好地格式化?
【发布时间】:2015-05-14 22:27:54
【问题描述】:
我正在使用yattag 来生成 HTML:
doc, tag, text = Doc().tagtext()
with tag("p"):
text("My paragraph.")
虽然它工作得很好,但当我查看 HTML 源代码时,它全部出现在一行中。是否有某种开关或其他技巧使yattag 创建更具可读性的源代码?
【问题讨论】:
标签:
html
python-3.x
line-breaks
yattag
【解决方案1】:
使用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>