Use tabulate
from tabulate import tabulate
table = [['one','two','three'],['four','five','six'],['seven','eight','nine']]
print(tabulate(table, tablefmt='html'))
Which produces the following output.
<table>
<tbody>
<tr><td>one </td><td>two </td><td>three</td></tr>
<tr><td>four </td><td>five </td><td>six </td></tr>
<tr><td>seven</td><td>eight</td><td>nine </td></tr>
</tbody>
</table>
Answer from Tomasz on Stack OverflowUse tabulate
from tabulate import tabulate
table = [['one','two','three'],['four','five','six'],['seven','eight','nine']]
print(tabulate(table, tablefmt='html'))
Which produces the following output.
<table>
<tbody>
<tr><td>one </td><td>two </td><td>three</td></tr>
<tr><td>four </td><td>five </td><td>six </td></tr>
<tr><td>seven</td><td>eight</td><td>nine </td></tr>
</tbody>
</table>
I would decompose your problem into two parts:
- given a "flat list", produce a list of sublists where the sublists are of a given length and the overall list may be walked into either a "row major" order (your first and third example) or "column major" (your second example);
- given a list of sublists with string items, produce an HTML table out of it.
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...?-)
Videos
What kind of data can HTML tables contain?
HTML tables can contain a wide variety of data types. They are commonly used to display structured information such as text, numbers, dates, images, links, and even nested tables. This makes them suitable for representing data like financial reports, product listings, and statistical data – which we can then scrape for analysis.
What are the best tools to scrape HTML tables?
Some of the best tools for scraping HTML tables include Python libraries like BeautifulSoup and Pandas. These tools allow you to easily extract table data and manipulate it. For more complex scenarios, Scraping APIs like ScraperAPI can handle dynamic content and bypass anti-scraping measures.
How Can I Scrape Dynamic Tables?
To scrape dynamic tables that are generated or updated using JavaScript, you need tools that can render JavaScript content. Tools like ScraperAPI can fetch the fully loaded page, allowing you to extract the dynamic table data. Alternatively, you can use webdrivers like Selenium or PlayWright, which can simulate a real user interacting with the browser, loading dynamic content before scraping.
» pip install pretty-html-table
Hello
So, I come primarily from R programming and there, I relied heavily on the {reactable} package to create interactive tables and generate an html output with R Markdown. Some examples from the main gallery are this one (note that the rows are expandable) and this (possibility to add images and plots to each row). reactable relies on React Table from React.
In Python, from what I've seen so far, I could use Quarto to replace R Markdown in generating html reports, but I still couldn't find a way to create flexible, interactive tables such as the ones above. I even found this related question here on Reddit, but the replies didn't help me.
Do you have any suggestions on which would be the best way to deal with this? I suspect that perhaps I won't find a solution purely in Python, but maybe even some "indirect" solution requiring a little JS would work well for my use case
Thank you!
» pip install simple-html-table
» pip install html-table-extractor