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]

Dani AI

Generated

The code has a few small but important mistakes that stop it from producing a valid page. is on the right track: build the table string then return it from the WSGI app, but note these common problems seen in the thread and replies from and :

  • Python syntax: use if __name__ == "__main__": (not if name = 'main':).
  • WSGI signature: the app must be application(environ, start_response) (not headers, start_response), and it must return bytes, e.g. [body.encode('utf-8')].
  • Data/typing: a stray trailing comma can turn your tabledata into a tuple instead of a list.
  • HTML building: each row should be a <tr> with multiple <td> cells; headers use <th>. Avoid malformed format strings like "<th>%</th>".
  • Safety: escape cell text to avoid injecting broken HTML.

A concise, working approach (Python 3) — build safe header and row fragments and join them, then return the HTML as bytes:

import html
from wsgiref.simple_server import make_server

def generate_table(headers, rows):
    head_html = ''.join('<th>{}</th>'.format(html.escape(h)) for h in headers)
    rows_html = ''.join('<tr>{}</tr>'.format(''.join('<td>{}</td>'.format(html.escape(c)) for c in r)) for r in rows)
    return '<html><head><meta charset="utf-8"></head><body><table><tr>{}</tr>{}</table></body></html>'.format(head_html, rows_html)

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')])
    body = generate_table(['First','Last','Age'], [['Steve','Cassidy','21'], ['John','Smith','22']])
    return [body.encode('utf-8')]

if __name__ == '__main__':
    print('Serving on http://localhost:8080')
    make_server('localhost', 8080, application).serve_forever()

Troubleshooting checklist: test generate_table in the REPL and print() its output before wiring into WSGI; verify the structure is one header <tr> and multiple data <tr>s; remove extra commas in your data literals; and use html.escape() for cell content. Follow ’s advice about posting properly indented code when you need further help — that makes spotting indentation and syntax mistakes much faster.

Recommended Answers

All 2 Replies

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.