Search code examples
htmlpython-3.xline-breaksyattag

How to make yattag source better formatted?


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?


Solution

  • 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>