We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,124 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

generate page using python

I want to make a program that it can create a HTML page. It should be able to make a table (with data provided) using the HTML by the program itself.

This code also uses wsgi so when the program runs you will need to type the localhost on a browser.

But there are some errors in this code and I am not exactly sure what is wrong with them. Any suggestions?

[code]def table(headings, data):

tableString = """<html>
<title>This is the Title</title>
<table>
    <tr>
        """ + getHeaders(headings) + """
    </tr>
    """ + getRows(data) + """
</table>
</html>
"""

def getHeaders(headings):
headers = ""
count = 0
while count <len(headings):
head = headings[count]
headers += "<th>%</th>\n" % head
count = count +1

def getRows(data):
rowentry = " "
for row in data:
#rowentry += "<tr>%s..."
rowitem = ""
count = 0
while count <len(row):
cell = row[count]
rowitem += "<tr>%s</tr>" % cell
count = count + 1
rowentry += "<tr>%s</tr>" % (rowitem)
return rowentry

from wsgiref import simple_server

def application(headers, start_response):
headers = [('content-type', 'text/html')]
start_response('200 OK', headers)

    tablehead = ['First Name', 'Last Name', 'Age'}
    tabledata = [['Steve', 'Cassidy', '21'],
                ['John', 'Smith', '22'],
                ['Mary', 'Margaret', '23']],
    print table(tablehead, tabledata)
    return [table(tablehead, tabledata)]

if name = 'main':
server = simple_server.make_server('localhost', 8080, application)
print 'Running server...'[/code]

3
Contributors
2
Replies
10 Hours
Discussion Span
1 Year Ago
Last Updated
3
Views
boiishuvo
Junior Poster in Training
76 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Could you repost with new code markup, which is indention by at least 4 spaces, after empty line, so you could do in your python editor:

ctrl-A, indent, ctrl-C, undo (or close without saving)

and paste it to your post. Or make empty line, paste code, select it by mouse and clic the Code from menu ribbon.

pyTony
pyMod
Moderator
6,307 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26

Start by testing your functions. This is very basic stuff. So in the function "table", print tableString at the end. Also you should be getting a syntax error on this line
if name = 'main':

Try one of the many tutorials on the web. The correct way for "name = main" is towards the bottom.

woooee
Posting Maven
2,706 posts since Dec 2006
Reputation Points: 827
Solved Threads: 779
Skill Endorsements: 9

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0667 seconds using 2.71MB