File: thumbspage/examples/__prior-version-items/__prior-versions-code/thumbspage-1.2-preUnicode.py
#!/usr/bin/python3 """ ==================================================================== thumbspage.py, version 1.2, Aug-1-16 Synopsis: Make an HTML thumbnail links page for an images folder. Requires: Any Python 3.X, plus Pillow (PIL) image library available at https://pypi.python.org/pypi/Pillow. Launch: Run thumbspage.py, input run parameters in console. See also formatting option settings at start of code. Examples: See /examples, and learning-python.com/books/trnpix. License: Provided freely but with no warranties of any kind. Author: Copyright M. Lutz (learning-python.com), July, 2016. Given a folder having image files, generates a basic index page with thumbnail links for each image file in the folder. Skips non-image files, uses optional header and footer HTML inserts, makes an automatic bullet list for any subfolders in the images folder, and creates the output page in the images folder itself. To view results, open the output "index.html" (or other) index page created in your images folder. To publish the page, upload the entire images folder, including its generated "thumbs" (or other) subfolder, to your web server. Add custom HTML code to the top and bottom of the output file by placing it in files in the images folder named "HEADER.html" and "FOOTER.html" (both are optional). Otherwise, generic HTML and text is generated in the output file around the thumbs. ==================================================================== Version history: 1.2, Aug-1-16, added table layout options per settings in code: -uniform-width columns, not per content (_on_ by default) -stretch thumbs table to fill whole window (_off_ by default) -scrollbar if window too small (skipped: use window scroll) 1.1, Jul-27-16, added subfolder links list per setting in code 1.0, Jul-24-16, initial release ==================================================================== Notes: 1) Apart from header/footer inserts, this script makes fairly simplistic image and subfolder links only: edit either the output HTML page or the code here that creates it as desired. 2) (1.1) Makes a bullet list for any subfolders in the images folder (else they cannot be navigated to), but skips all other non-image content; put any doctype, readme, font, CSS code, etc. in header or footer. Run on each image tree subfolder manually; automating this and other content seems too clever and complex. 3) (1.2) Stretching the thumbs table to fill the whole window is off by default, as it can appear too spread out for short names in large windows. Enable this via the code setting below if desired. 4) (1.2) A table scrollbar was skipped, because it appears only at table bottom, which can be arbitrarily far away on large pages. Instead, use the window scroll created automatically by browsers. 5) See also pixindex, a related tool that adds images to a zip file and FTPs them to a sever: learning-python.com/pixindex. ==================================================================== Caveats and TBDs: 1) Python 3.X assumes local platform's Unicode encoding default for header/footer inserts and the output page: change as needed. 2) This script would probably run on Python 2.X too if the viewer_thumbs module didn't import "tkinter" (a book legacy). 3) A ".." parent link is not generated in automatic subfolder lists; should it be? 4) Creating the index file in the image file might preclude use of some page-generation tools (see trnpix template hardcoding). 5) Styling of the table is open ended. For example, a <table> style "border-spacing: 4px;" may help avoid names running into each other, though the default (2px?) seems adequate. ==================================================================== """ import os, sys, glob from viewer_thumbs import makeThumbs # courtesy of the book PP4E if sys.version[0] == 2: input = raw_input # 2.X compatibility (but unused) # folder/file names THUMBS = 'thumbs' # created subfolder (changeable) INDEX = 'index' # created page (or 'default'/'home') # cod generation options (hardcode for now) listSubfolders = True # auto folder list? (or via header) uniformColumns = True # same-width columns? (else content) spanFullWindow = False # stretch table to window's width? #------------------------------------------ # Configure: console inputs, Enter=default #------------------------------------------ # y or n => remove any exiting thumb files? cleanFirst = input('Clean thumbs? ').lower() in ['y', 'yes'] # int => fixed row size, irrespective of window reply = input('Thumbs per row? ') thumbsPerRow = int(reply) if reply else 5 # (int, int) => max (x, y) pixels limit, preserving original aspect ratio reply = input('Thumb max size? ') thumbMaxSize = eval(reply) if reply else (100, 100) # str => images folder path: images, header/footer, output imageDir = input('Images folder path? ') or '' # default='.' # the output page created in images folder indexPath = os.path.join(imageDir, INDEX + '.html') # optional inserts in images folder, else generic text headerPath = os.path.join(imageDir, 'HEADER.html') footerPath = os.path.join(imageDir, 'FOOTER.html') #------------------------------------------ # Make thumbnails in image folder subdir #------------------------------------------ if cleanFirst: for thumbpath in glob.glob(os.path.join(imageDir, THUMBS, '*')): print('Cleaning %s' % thumbpath) os.remove(thumbpath) makeThumbs(imageDir, size=thumbMaxSize, subdir=THUMBS) #------------------------------------------ # Create links text for each thumb->image #------------------------------------------ links = [] for thumbname in sorted(os.listdir(os.path.join(imageDir, THUMBS))): link = ('<A href="%s"><img src="%s" border=1></A>' % (thumbname, THUMBS + '/' + thumbname)) # Unix / for web! links.append((thumbname, link)) #------------------------------------------ # Create links for any subfolders here #------------------------------------------ subdirs = [] for item in sorted(os.listdir(imageDir)): if item != THUMBS and not item.startswith('_'): # skip thumbnails and '_x' itempath = os.path.join(imageDir, item) # uses local path here if os.path.isdir(itempath): subdirs.append('<A href="%s">%s</A>' % (item, item)) #------------------------------------------ # Generate full web page in images folder #------------------------------------------ sys.stdout = open(indexPath, 'w') # assume platform encoding/content # header section if os.path.exists(headerPath): print(open(headerPath, 'r').read()) # assume platform Unicode encoding else: foldername = os.path.basename(imageDir) print('<html>' '<head><title>Index of %s</title></head>' '<body><h1>Index of image folder "%s"</h1>' % ((foldername,) * 2)) # subfolders bullet list (skip other content) if subdirs and listSubfolders: print('<p><b>Subfolders here:</b><p><ul>') for link in subdirs: print('<li>' + link) print('</ul></p>') # thumb links table print('<p><hr><p>') # no <div style="overflow-x:auto;"> tabstyle = '' # window scroll is more visible if spanFullWindow: tabstyle += ' style="width: 100%;"' # expand table to entire window print('<table%s>' % tabstyle) # not used: "table-layout:fixed;" while links: row, links = links[:thumbsPerRow], links[thumbsPerRow:] print('<tr>') for (name, link) in row: colstyle = '' if uniformColumns: colstyle += ' style="width: %d%%;"' % (100 / thumbsPerRow) print('<td%s>%s<br>%s' % (colstyle, link, name)) print('<tr><tr>') print('</table></p><hr>') # footer section if os.path.exists(footerPath): print(open(footerPath, 'r').read()) # assume platform Unicode encoding else: print('<p><i>This page was generated by ' '<A HREF="http://learning-python.com/thumbspage.html">thumbspage.py' '</A></i></p>') print('</body></html>') sys.stdout.close()