I have the following code that generates the desired HTML. But it prints it at the top of the page, instead of where I want it.
def fixText(self,text):
row = []
z = text.find(',')
if z == 0: row.append('')
else: row.append(text[:z])
for x in range(len(text)):
if text[x] != ',': pass
else:
if x == (len(text)-1): row.append('')
else:
if ',' in text[(x+1):]:
y = text.find(',', (x+1))
c = text[(x+1):y]
else:
c = text[(x+1):]
row.append(c)
return row
def output(self):
output = ""
fob=open('files/contacts.txt','r')
tup = []
while 1:
text = fob.readline()
if text == "": break
else: pass
if text[-1] == '\n':
text = text[:-1]
else: pass
row = self.fixText(text)
tup.append(row)
output = tup.sort(key=lambda x: x[0].lower())
_list1 = len(tup)
i = 0
table = ""
while i < _list1:
j = 0 #need to reset j on each iteration of i
_list2 = len(tup[i])
print("<tr>")
while j < _list2:
print("<td>" + tup[i][j] + "</td>")
j += 1
print("</tr>")
i += 1
return output
As you can see, I have a nested loop that prints out the HTML code. This works fine, but when I try to inject it into my modular site it prints it at the top of the page. My hunch is that the following code is the part I need to change.
#!/usr/local/bin/python3
print('Content-type: text/html\n')
import os, sys
import cgitb; cgitb.enable()
sys.path.append("classes")
from Pagedata import Pagedata
Page = Pagedata()
print(Page.doctype())
print(Page.head())
print(Page.title("View Contact"))
print(Page.css())
html='''</head>
<body>
<div id="wrapper">
<div id="header">
<div id="textcontainer">{1}</div>
</div>
<div id="nav">{2}</div>
<div id="content">
<h2>Heading</h2>
<h3>Sub Heading Page 2</h3>
<table>
<tbody>
{0}
</tbody>
</table>
</div>
<div>
<div id="footer">{3}</div>
</body>
</html>'''.format(Page.output(),Page.header(),Page.nav(),Page.footer())
print(html)
There are other functions on my class page, such as header
, footer
, etc., that work fine.
For example, the footer is properly inserted into div#footer
. But the generated table si not inserted where {0}
is.
You can view the broken code here.