simple_gallery

Generate simple, static HTML/CSS gallery
Log | Files | Refs | README

commit 83126e443ba0c939feb91edaa44e952ea0582ebd
parent 83198efbe133ee2f8927faf391cf47aeff1f9e71
Author: sej <sej@sejdt.localhost>
Date:   Sun, 29 Sep 2024 22:45:25 +0200

Added argparser and changed to format strings instead of concatenations.

Diffstat:
Mjinjarender.py | 61+++++++++++++++++++++++++++++--------------------------------
1 file changed, 29 insertions(+), 32 deletions(-)

diff --git a/jinjarender.py b/jinjarender.py @@ -21,16 +21,23 @@ class image_info: if __name__ == "__main__": - if len( sys.argv ) == 3: - albumpath = sys.argv[1] # sti til json-fil der beskriver albums, altså fortæller hvilke billeder der er i hvilke albums. - outputpath = sys.argv[2] # sti til output af html-filer. det forventes at der her ligger en undermappe med metadata på csv-format for hvert inputbillede og thumbnails til hvert inputbillede - #imginpath = sys.argv[3] # sti til de originale input-billeder. - #imgmetapath = sys.argv[4] # sti til mappe med img thumbs og metadata - else: - print( "insufficient arguments" ) - exit() + parser = argparse.ArgumentParser( + prog="Simple Gallery", + description="Generate a static multi-album image gallery HTML/CSS site from a JSON description.", + ) + + parser.add_argument( + "input", + help="JSON file containing the album descriptions." + ) + parser.add_argument( + "output_path", + help="Path to the folder for the rendered HTML." + ) + args = parser.parse_args() + env = Environment( loader=PackageLoader( "jinjarender", package_path="templates" ), autoescape=select_autoescape() @@ -40,17 +47,16 @@ if __name__ == "__main__": viewer = env.get_template( "viewer2.html" ) album = env.get_template( "album2.html" ) - ls = os.listdir( outputpath + "img/metadata/" ) + ls = os.listdir( f"{ args.output_path }/img/metadata/" ) m = dict() for metadata in ls: - with open( outputpath + "img/metadata/" + metadata ) as f: + with open( f"{ args.output_path }/img/metadata/{ metadata }" ) as f: r = csv.reader( f ) row = next(r) m[ row[0] ] = row[1:] - #imgs = os.listdir( "web/img" ) - with open( albumpath ) as jsonfile: + with open( args.input ) as jsonfile: jsonarray = json.load( jsonfile ) allimgsdict = dict() @@ -68,22 +74,22 @@ if __name__ == "__main__": #generate album files for i in range( nalbums ): json = jsonarray[i]; - nextalbum = "{}.html".format( jsonarray[ (i+1)%nalbums ]["name"] ) - prevalbum = "{}.html".format( jsonarray[ (i-1)%nalbums ]["name"] ) - with open( outputpath + json["name"] + ".html", "x" ) as f: + nextalbum = "{ jsonarray[ (i+1)%nalbums ]['name'] }.html" + prevalbum = "{ jsonarray[ (i-1)%nalbums ]['name'] }.html" + with open( f"{ args.output_path }/{ json['name'] }.html", "x" ) as f: f.write( album.render( nextalbum = nextalbum, prevalbum = prevalbum, title = json["name"], imgs = json["imgs"], csspath = "css/stylesheet.css", navbar = True ) ) #generate viewer files for this album - os.makedirs( outputpath + "img/viewer/{}".format( json["name"] ) ) + os.makedirs( f"{ args.output_path }/img/viewer/{ json['name'] }" ) n = len( json["imgs"] ) for i in range( n ): img = json["imgs"][i] exif = m[img] exifstring = "{} || {}s | f/{} | {} | ISO{} || {} + {} | {}".format( exif[2], exif[3], exif[4][1:], exif[5], exif[6], exif[7], exif[8], exif[9] ) - nexti = str( "{}.html".format( json["imgs"][ (i+1)%n ] ) ) - previ = str( "{}.html".format( json["imgs"][ (i-1)%n ] ) ) - with open( outputpath + "img/viewer/{}/{}.html".format( json["name"], img ), "x" ) as f: + nexti = f"{ str( allimgs[ (i+1)%n ] ) }.html" + previ = f"{ str( allimgs[ (i-1)%n ] ) }.html" + with open( f"{ args.output_path }/img/viewer/{ json['name'] }/{ img }.html", "x" ) as f: f.write( viewer.render( navbar = True, fullrespath = "/img/fullres/", previewpath = "/img/preview/" , img = img, exif = exifstring, nexti = nexti, previ = previ ) ) #generate index file @@ -95,7 +101,7 @@ if __name__ == "__main__": imgs.append( json["imgs"][0] ) desc.append( json["name"] ) - with open( outputpath + "index.html", "x" ) as f: + with open( f"{ args.output_path }/index.html", "x" ) as f: f.write( index.render( title = "index", imgs = imgs, hrefs = albums, desc = desc, bodyclass = "index" ) ) #generate viewer files for all imgs @@ -104,16 +110,7 @@ if __name__ == "__main__": img = allimgs[i] exif = m[img] exifstring = "{} || {}s | f/{} | {} | ISO{} || {} + {} | {}".format( exif[2], exif[3], exif[4][1:], exif[5], exif[6], exif[7], exif[8], exif[9] ) - nexti = str( allimgs[ (i+1)%n ] ) + ".html" - previ = str( allimgs[ (i-1)%n ] ) + ".html" - with open( outputpath + "img/viewer/{}.html".format( img ), "x" ) as f: + nexti = f"{ str( allimgs[ (i+1)%n ] ) }.html" + previ = f"{ str( allimgs[ (i-1)%n ] ) }.html" + with open( f"{ args.output_path}/img/viewer/{ img }.html", "x" ) as f: f.write( viewer.render( navbar = True, fullrespath = "/img/fullres/", previewpath = "/img/preview/", img = img, exif = exifstring, path = "/img/", nexti = nexti, previ = previ) ) - - - - -""" - for img in imgs: - f = open( img + ".html", "x" ) - f.write( viewer.render( img = img, exif = exif ) ) -"""