lets say I have a list like so:
['one','two','three','four','five','six','seven','eight','nine']
and I want to experiment with turning this data into a HTML table of various dimensions:
one two three four five six seven eight nine
or
one four seven two five eight three six nine
or
one two three four five six seven eight nine
Is there a library that can handle this without needing to do crazy list splicing or nested for loops? My google searches reveal that there are a few HTML libraries, but I don't have the time to go through each one to see if they can handle tables very well. Has anyone ever had to do this? If so how did you do it?
To create table in HTML, use the <table> tag. A table consist of rows and columns, which can be set using one or more <tr>, <th>, and <td> elements. A table row is defined by the <tr> tag. To set table header, use the <th> tag.
This lesson uses Python to create and view an HTML file. If you write programs that output HTML, you can use any browser to look at your results.
I would decompose your problem into two parts:
I think the two tasks are really very distinct and there's nothing to gain (and much to lose) in mushing them up, so I would be astonished if any well-designed library did such mushing.
For point 1, row-major is easy:
def row_major(alist, sublen): return [alist[i:i+sublen] for i in range(0, len(alist), sublen)]
and column-major not that bad:
def col_major(alist, sublen): numrows = (len(alist)+sublen-1) // sublen return [alist[i::sublen] for i in range(numrows)]
for example...:
L = ['one','two','three','four','five','six','seven','eight','nine'] for r in row_major(L, 3): print r print for r in col_major(L, 3): print r for r in row_major(L, 4): print r
produces your three desired results (one list per row, not in HTML form yet;-).
The second half of the problem -- produce an HTML table from a list of lists of strings:
def html_table(lol): print '<table>' for sublist in lol: print ' <tr><td>' print ' </td><td>'.join(sublist) print ' </td></tr>' print '</table>'
If you want to get it as a single string rather than print it out, change each print
into yield
and use '\n'.join(html_table(lol))
.
Now you have two simple, useful, usable and reusable building blocks -- having them separate will come in handy whenever you want to present your data as anything BUT an HTML table, and also whenever the list-of-lists to present as an HTML table comes from any other way of building it. Putting them together is easy to do in your application code, but of course it's also easy to do a simple "glue routine", e.g., assuming the yield
-based version of html_table
and that a single string result is desired:
def list_to_html_table(alist, sublength, column_major=False): if column_major: lol = col_major(alist, sublength) else: lol = row_major(alist, sublength) return ''.join(html_table(lol))
Isn't this building-blocks approach really nicer and more pleasant, as well as more productive, than programming in terms of big blobs of mushed-up glue...?-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With