Creating HTML in python

后端 未结 5 1083
情歌与酒
情歌与酒 2020-11-30 02:45

I am looking for a way to create html files dynamically in python. I am writing a gallery script, which iterates over directories, collecting file meta data. I intended to

5条回答
  •  野性不改
    2020-11-30 03:26

    Dominate is a Python library for creating HTML documents and fragments directly in code without using templating. You could create a simple image gallery with something like this:

    import glob
    from dominate import document
    from dominate.tags import *
    
    photos = glob.glob('photos/*.jpg')
    
    with document(title='Photos') as doc:
        h1('Photos')
        for path in photos:
            div(img(src=path), _class='photo')
    
    
    with open('gallery.html', 'w') as f:
        f.write(doc.render())
    

    Output:

    
    
      
        Photos
      
      
        

    Photos

    Disclaimer: I am the author of dominate

提交回复
热议问题