I am creating a table in python using pretty-table and trying to have one column which are the urls have clickable links:
Eg:
filepath = "pretty_table.html"
from prettytable import PrettyTable
x = PrettyTable()
x.format = True
x = PrettyTable(["City name", "Area", "Url"])
x.add_row(["Adelaide",1295, "<a href='https://en.wikipedia.org/wiki/Adelaide'>https://en.wikipedia.org/wiki/Adelaide</a>" ])
x.add_row(["Brisbane",5905, "<a href='https://en.wikipedia.org/wiki/Brisbane'>https://en.wikipedia.org/wiki/Brisbane</a>"])
result = []
result.append(x.get_html_string())
with open(filepath, 'w') as f:
for line in result:
f.write("{}\n".format(line))
However, with the above, the table doesn't generate clickable links for the wiki pages. Can someone please help.
Here is the answer
import html
url = 'https://www.baidu.com' # link to point to
pt.add_row(['2018-10-10','<a href=\"' + url + '\">' + url + '</a>'])
text = pt.get_html_string(format=True)
text = html.unescape(text) # crucial step
More information can be found here
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