Web Programming Languages

Python Source Code: images.py

#!/usr/bin/env python
import cgi
import textwrap
import os
import common

#----------------------------------------------------------
#   The Web Language Project
#   Mark Brautigam
#   May-June 2015
#   http://www.mixed-up.com/markb/
#----------------------------------------------------------

common.headers1();
common.headers2();
print common.sidebarX("python", "images", "py");

print textwrap.dedent("""\
  <div id="content">
    <h2>Python: Directory Read</h2>
    <p>Read the contents of a directory of images. Exclude the non-image directory entries.
       Create a caption by deleting the file extension and capitalizing the file name.</p>
    <p>The Python <tt><b>os.listdir</b></tt> function appears to show only the visible files, not the
       dot files, so we don't have to manually exclude the invisibles.</p>
    <p class='choose'>Choose a directory:
      <a href='images.py?dir=comics'>Comics</a> • 
      <a href='images.py?dir=embroidery'>Embroidery</a> • 
      <a href='images.py?dir=stone'>Carving</a> • 
      <a href='images.py?dir=tech'>Tech</a>
    </p>
""")

#----------------------------------------------------
def getCaption (s) :
#----------------------------------------------------
  caption = s[:-4].capitalize()
  if (caption.find("_") > -1) :
    captions = caption.split ("_");
    for j in range (0, len(captions)) :
      captions[j] = captions[j].capitalize()
    caption = "<br />".join (captions[1:])
  return caption

#----------------------------------------------------
#  The main Python code is here.
#----------------------------------------------------
dir = "../images/tech"
form = cgi.FieldStorage()
askdir = form.getvalue('dir', '')
if (askdir != '') :
  dir = "../images/" + askdir

images = os.listdir (dir)
nimages = len(images)
for i in range (0, nimages) :
  if (images[i][0] != '.' and images[i][0] != '_') :
    caption = getCaption (images[i])
    #caption = images[i][:-4].capitalize()

    print "<figure>"
    print "  <img src='%s/%s' />" % (dir, images[i])
    print "  <figcaption>%s</figcaption>" % caption
    print "</figure>";

#----------------------------------------------------

print textwrap.dedent("""\
    <p>We can change the order of the files (for example, birth order instead of alphabetical
       order) by prepending each file name with a number and removing the number from the
       caption string.</p>
    <p>Images courtesy of:<br />
    <a target='_blank'
       href='http://www.worldofjudaica.com/jewish-home/stationery/p_jerusalem_stone_paperweight_with_twelve_tribes_and_menorah'>
       Stone Paperweight at World of Judaica</a><br />
     <a target='_blank'
        href='http://4-hobby.com/store/product.php?productid=17686&cat=320&page=1'>
        Embroidery Designs at 4-Hobby.com</a><br />
     <a target='_blank' href='http://www.dumbingofage.com'>Dumbing of Age</a><br />
     <a target='_blank' href='http://www.scarygoround.com'>Scary Go Round</a><br />
     <a target='_blank' href='http://www.questionablecontent.net'>Questionable Content</a>
    </p>
    <p><a href='source.py?f=5'>Show Python source code »</a>
  </div>
""")

common.footers();