#!/usr/bin/env python
#=====================================================================================
# Get data from a server file - one record at a time
# Display to an html form
#=====================================================================================
import cgitb                                #cgi with traceback error handling
import re                                   #regular expressions
 
cgitb.enable()
 
print("Content-Type: text/html \n")          #required http response header (w/ extra line)

'''===================================================================================
read_and_display: read data from a server file and display on web page
==================================================================================='''
def read_and_display():
 
    file  = '/home/sultans/data/cust_order.file';   	#server file
    input = open(file,'r')                             	#Open file for reading
    
    print("""
        <html>
        <head>
        <title>Retrieve data from database</title>    
        <style>
            a     {text-decoration:none; color:brown}
            table {font:10pt arial; border:solid 2px black; border-collapse:collapse; margin-left:70px; background-color:#eeeeee; } 
            th,td {border:solid 1px black; padding-left:5px; padding-right:5px; vertical-align:top;}
        </style>
        <body bgcolor=lightyellow>
        <h2><center>Ice Cream Orders - Read one rec at a time</center></h2>
        <table bgcolor=lightyellow border=1>
        <tr bgcolor=tan>
    """)                           
        
    print("<th> Name      </th>")
    print("<th>Address    </th>")
    print("<th>Flavors    </th>")
    print("<th>Toppings   </th>")
    print("<th>Credit Card</th>")

    for rec in input:                                  #loop thru the data rows
        [fn,ln,addr,flav,top,cc] = rec.split('||')     #split the record on ||
        print("<tr>",end='')

        name = fn +' '+ ln
        addr = re.sub(r"__", "<br>", addr)             #replace _ with <br>
        flav = re.sub(r",",  "<br>", flav)             #replace , with <br>
        top  = re.sub(r",",  "<br>", top)              #replace , with <br>
        
        print("<td>"+ name +"<td>"+ addr +"<td>"+ flav +"<td>"+ top +"<td>"+ cc)
        print("</tr>")
        
    input.close()                                      #close the file
    
    print("</table>")
    print("</body>")
    print("</html>")

'''===================================================================================
main code
==================================================================================='''
read_and_display()


#=== link to see the python code =================================================
import os, sys
sys.path.insert(0,'/home/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
#=================================================================================