Web Programming Languages

Python Source Code: fread.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", "fread", "py");

print textwrap.dedent("""\
  <div id="content">
    <h2>Python: Display File Data Contents</h2>
    <table class='results'>
      <tr><th>College</th><th>Dept.</th><th>Course</th><th>Description</th><th>Edit</th></tr>
""")

#----------------------------------------------------
#  The main Python code is here.
#----------------------------------------------------
f = open ('../data/courses.txt', 'r')
courses = f.readlines()
ncourses = len(courses)
for i in range (0, ncourses) :
  coursedata = courses[i].split("|")
  print "<tr>"
  #print "<td>{}</td>".format(coursedata[0]) #Python 2.6+ only
  #print "<td>{}</td>".format(coursedata[1]) #Python 2.6+ only
  #print "<td>{}</td>".format(coursedata[2]) #Python 2.6+ only
  #print "<td>{}</td>".format(coursedata[3]) #Python 2.6+ only
  print "<td>%s</td>" % (coursedata[0])
  print "<td>%s</td>" % (coursedata[1])
  print "<td>%s</td>" % (coursedata[2])
  print "<td>%s</td>" % (coursedata[3])
  #print "<td><a href='fedit.py?id={}'><img src='../images/pencil.svg' /></a></td>\n".format(i)
  print "<td><a href='fedit.py?id=%d'><img src='../images/pencil.svg' /></a></td>\n" % (i)
  print "</tr>"
#----------------------------------------------------

print textwrap.dedent("""\
    </table>
    <p><a href='fwrite.py'>Write to this file »</a></p>
    <p><a href='source.py?f=2'>Show Python source code »</a>
  </div>
""")

common.footers();