Python Source Code: table.py
#!/usr/bin/env python
import cgi
import textwrap
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", "table", "py");
print " <div id='content'>"
print " <h2>Python: Dynamic Table Data</h2>"
state_data = [
"NY|New York|19 m|3|Albany",
"NJ|New Jersey|8.7 m|10|Trenton",
"VT|Vermont|620 k|49|Montpelier",
"IN|Indiana|6.2 m|15|Indianapolis",
"OH|Ohio|11 m|7|Columbus",
"TX|Texas|22 m|2|Austin",
"NM|New Mexico|1.9 m|36|Santa Fe",
"MA|Massachusetts|6.3 m|13|Boston",
"CO|Colorado|4.6 m|22|Denver",
"TN|Tennessee|5.9 m|16|Nashville",
"ND|North Dakota|630 k|48|Bismarck",
"WY|Wyoming|500 k|50|Cheyenne",
"OK|Oklahoma|3.5 m|29|Oklahoma City",
"WV|West Virginia|1.8 m|37|Charleston",
"NV|Nevada|2.4 m|35|Carson City",
"PA|Pennsylvania|12 m|6|Harrisburg",
"CA|California|36 m|1|Sacramento",
"WA|Washington|6.2 m|14|Olympia",
"GA|Georgia|9.0 m|9|Atlanta",
"MO|Missouri|5.8 m|18|Jefferson City",
"NH|New Hampshire|1.3 m|41|Concord",
"HI|Hawaii|1.2 m|42|Honolulu",
"MD|Maryland|5.6 m|19|Annapolis",
"DE|Delaware|840 k|45|Dover",
"AK|Alaska|660 k|47|Juneau",
"MN|Minnesota|5.1 m|21|Saint Paul",
"NC|North Carolina|8.6 m|11|Raleigh",
"KY|Kentucky|4.1 m|26|Frankfort",
"MT|Montana|930 k|44|Helena",
"LA|Louisiana|4.5 m|24|Baton Rouge",
"NE|Nebraska|1.7 m|38|Lincoln",
"WI|Wisconsin|5.5 m|20|Madison",
"AL|Alabama|4.5 m|23|Montgomery",
"IA|Iowa|2.9 m|30|Des Moines",
"VA|Virginia|7.5 m|12|Richmond",
"ME|Maine|1.3 m|40|Augusta",
"ID|Idaho|1.4 m|39|Boise",
"AR|Arkansas|2.7 m|32|Little Rock",
"UT|Utah|2.4 m|34|Salt Lake City",
"MS|Mississippi|2.9 m|31|Jackson",
"RI|Rhode Island|1.0 m|43|Providence",
"FL|Florida|17 m|4|Tallahassee",
"OR|Oregon|3.6 m|27|Salem",
"SD|South Dakota|770 k|46|Pierre",
"MI|Michigan|10 m|8|Lansing",
"KS|Kansas|2.7 m|33|Topeka",
"CT|Connecticut|3.5 m|28|Hartford",
"SC|South Carolina|4.2 m|25|Columbia",
"IL|Illinois|12 m|5|Springfield",
"AZ|Arizona|5.9 m|17|Phoenix" ];
#print state_data[1]
state_data_2d = [];
for i in range (0, len(state_data)) :
data = state_data[i].split ("|");
# http://www.perlmonks.org/?node_id=166572
# pushing multi-dimensional arrays onto each other
state_data_2d.append(data);
for i in range (0, len(state_data_2d)) :
state_data_2d[i][3] = int(state_data_2d[i][3])
#print state_data_2d
#----------------------------------------------------
def popcompare (a, b) :
#----------------------------------------------------
if (a[2].find ('m') > -1 and b[2].find('k') > -1) :
return 1;
if (a[2].find ('k') > -1 and b[2].find('m') > -1) :
return -1;
#return float(b[2]) - float(a[2])
aa = a[2][:a[2].find(' ')]
bb = b[2][:b[2].find(' ')]
if (float(bb) - float(aa) > 0) :
return -1
if (float(bb) - float(aa) < 0) :
return 1
return 0
#----------------------------------------------------
def draw_main_menu () :
#----------------------------------------------------
print textwrap.dedent("""\
<p class='choose'>Choose a table:
<a href='table.py'>Multiplication</a> •
<a href='table.py?q=ascii'>ASCII</a> •
<a href='table.py?q=geo'>US States</a>
</p>
""")
#----------------------------------------------------
def draw_geo (sort) :
#----------------------------------------------------
print " <h3>US States, Capitals, and Population</h3>"
draw_main_menu ();
print textwrap.dedent("""\
<p class='choose'>Choose a sort:
<a href='table.py?q=geo'>Abbreviation</a> •
<a href='table.py?q=geo&sort=state'>State</a> •
<a href='table.py?q=geo&sort=pop'>Population</a> •
<a href='table.py?q=geo&sort=rank'>Rank</a> •
<a href='table.py?q=geo&sort=cap'>Capital</a>
</p>
<table class='results geo'>
<tr><th>Abbr</th><th>State</th><th>Population</th><th>Rank</th><th>Capital</th></tr>
""")
#print sort;
if (sort == 'rank') :
states_sorted = sorted (state_data_2d, key=lambda rank: rank[3] )
elif (sort == 'pop') :
states_sorted = sorted (state_data_2d, cmp=popcompare )
elif (sort == 'cap') :
states_sorted = sorted (state_data_2d, key=lambda rank: rank[4] )
elif (sort == 'state') :
states_sorted = sorted (state_data_2d, key=lambda rank: rank[1] )
else :
states_sorted = sorted (state_data_2d, key=lambda rank: rank[0] )
for i in range (0, len(states_sorted)) :
print "<tr>";
for j in range (0,len(states_sorted[i])) :
#print "<td>{}</td>".format(states_sorted[i][j]) #Python 2.6+ only
print "<td>%s</td>" % (states_sorted[i][j])
print "</tr>\n";
print "</table>\n";
#----------------------------------------------------
def draw_mult () :
#----------------------------------------------------
print "<h3>Multiplication Table</h3>"
draw_main_menu();
print "<table class='mult'>"
print "<tr><th></th>"
for i in range (1, 12) :
#print ("<th>{}</th>".format(i)) #Python 2.6+ only
print ("<th>%d</th>" % (i) )
print ("</tr>");
for r in range (1, 12) :
#print ("<tr><th>{}</th>".format(r)); #Python 2.6+ only
print "<tr><th>%d</th>" % (r)
for c in range (1, 12) :
p = r*c;
#print ("<td>{}</td>".format(p)); #Python 2.6+ only
print ("<td>%d</td>" % (p));
print ("</tr>");
print ("</table>")
#----------------------------------------------------
def draw_ascii () :
#----------------------------------------------------
print "<h3>ASCII Table</h3>"
draw_main_menu();
print "<table class='mult ascii'>"
print "<tr><th></th>"
for i in range (0, 16) :
#print ("<th>{:x}</th>".format(i)) #Python 2.6+ only
print ("<th>%X</th>" % (i))
print ("</tr>");
for r in range (2, 8) :
#print ("<tr><th>{}</th>".format(r)); #Python 2.6+ only
print ("<tr><th>%d</th>" % (r));
for c in range (0, 16) :
p = 16*r+c;
#print ("<td>{}</td>".format(chr(p))); #Python 2.6+ only
print ("<td>%c</td>" % (chr(p)));
print ("</tr>");
print ("</table>")
#----------------------------------------------------
# The main Python code is here.
#----------------------------------------------------
form = cgi.FieldStorage()
q = form.getvalue('q', '')
sort = form.getvalue ('sort', '')
if (q == "ascii") :
draw_ascii();
elif (q == "geo") :
draw_geo(sort);
else :
draw_mult();
print " <p><a href='source.py?f=6'>Show Python source code »</a>"
print " </div>"
common.footers();