Generating HTML documents in python

前端 未结 7 1191
花落未央
花落未央 2020-11-28 03:43

In python, what is the most elegant way to generate HTML documents. I currently manually append all of the tags to a giant string, and write that to a file. Is there a mor

7条回答
  •  囚心锁ツ
    2020-11-28 04:21

    I find yattag to be the most elegant way of doing this.

    from yattag import Doc
    
    doc, tag, text = Doc().tagtext()
    
    with tag('html'):
        with tag('body'):
            with tag('p', id = 'main'):
                text('some text')
            with tag('a', href='/my-url'):
                text('some link')
    
    result = doc.getvalue()
    

    It reads like html, with the added benefit that you don't have to close tags.

提交回复
热议问题