How to make yattag source better formatted?

2.6k Views Asked by At

I'm using yattag to generate HTML:

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

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

While it works well enough, when I look at the HTML source it comes out all on one line. Is there some kind of switch or other trick that makes yattag create more readable source?

1

There are 1 best solutions below

0
On BEST ANSWER

Use the indent function.

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>