#!/usr/bin/python3
###################################################################################
# print a loop (for both a list and a dictionary) in html tables 
###################################################################################
print("Content-Type: text/html \n")  #http header with newline char (required for web)


print("<html> ")
print("<body> ")
print("<h2>Loop Through List/Dictionary and  Display Content in HTML Table</h2> ")

list1 = [ 'Sam', 'John', 'Joe', 'Sue', 'Steve', 'Stephanie' ]

dict1 = { 'Mon':'chicken', 'Tue':'beef',  'Wed':'fish', 
              'Thu':'veggie',  'Fri':'pizza',  'Sat':'pasta', 'Sun':'leftovers' }

print(list1, "<br><br>")
print(dict1, "<br><br>")

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

print("<table border='2' width='100px'> ")                  #create an HTML table
print("<tr bgcolor=gray ><th> NAMES </th></tr> ")           #print table header

for name  in  list1 :                                       #for loop in list
    print("<tr><td>", name,  "</td></tr> ")                 #print each value

print("</table> <br>")

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

print("<table border='2' width='100px'> ")                          #create an HTML table
print("<tr bgcolor=gray><th> INDEX </th><th> NAMES </th></tr> ")    #print table header

for idx  in  range( len(list1) ):                                   #for loop in list index
    print("<tr><th>", idx, "</th><td>", list1[idx], "</td></tr> ")  #print each index & value

print("</table> <br>")

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

print("<table border='2' width='200px'> ")                          #create an HTML table
print("<tr bgcolor=gray ><th> DAY </th><th> MEAL </th></tr> ")      #print table header

for day  in dict1 :                                                 #for loop in dictionary
    print("<tr><th>", day, "</th><td>", dict1[day], "</td></tr>")   #print each name & value

print("</table> <br>")
print("</body>")
print("</html>")




#=== link to see the python code =================================================
import os, sys
sys.path.insert(0,'/home/s/sultans/web/python/demo')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#=================================================================================