1

I am trying to add the web URL to the prettytable of python and generate this in an HTML table... As I am a Linux user, the terminal automatically detects the link present in the table but it is not in the case of HTML ...

and also I am trying to use the same to generate a hyperlink in the table... but I was not able to do it as I am getting many blockages...

my code to generate prettytable in python

diff_table = PrettyTable(["S.NO"," Commit Message", "Author", 'URL','Date','Time'])
for commit in response_differneces_in_json['commits']:
   url = commit['web_url']
   diff_table.add_row([count , commit['title'] , commit['author_name'], "<a href="+url+">click here</a>" ,commit['committed_date'][:10],commit['committed_date'][11:19]])
   text = diff_table.get_html_string(format=True)
   text = html.unescape(text)
   count = count + 1
#here imported the html module

here I am getting output as the same string as click here

and I am generating the html report for this table by :

f.write('<p>'+diff_table.get_html_string()+'</p>')

Here I cannot make a link clickable and how to make the link clickable in Html format and also how to create a hyperlink

or is that possible to manipulate all the columns of the HTML table --> to make it clickable?

ganesh tirumani
  • 35
  • 1
  • 10
  • if you want to display in terminal which detect links in text then you don't need HTML. But if you want to create HTML then open it in web browser, not in terminal. – furas Feb 15 '22 at 13:41
  • maybe better create minimal working code (with example data in code) which we could simply copy and run - to see what you really get in HTML. Maybe you should use some code. – furas Feb 15 '22 at 13:42
  • as for me problem is that you write in file `diff_table.get_html_string()` but yo should write `html.unescape(text)` – furas Feb 15 '22 at 13:51

1 Answers1

1

As for me all your problem is that you write diff_table.get_html_string() which converts < to &lt; and > to &gt; so you get HTML with

&lt;a href="url"&gt;click here&lt;/a&gt;

instead of

<a href="url">click here</a>

and later you see string <a href="url">click here</a> instead of link.


You should write in file html.unescape(text).

text = diff_table.get_html_string(format=True)
text = html.unescape(text)

with open('index.html', 'w') as f:
    f.write('<p>'+text+'</p>')

Minimal working code with example data in code.

BTW: I also add " " around url in '<a href="'+ commit['web_url'] + '">click here</a>'

from prettytable import PrettyTable
import html

diff_table = PrettyTable(['S.NO', 'Commit Message', 'Author', 'URL','Date', 'Time'])

#data = response_differneces_in_json['commits']
data = [
    {
        'title': 'a',
        'author_name': 'b',
        'web_url': 'c',
        'committed_date': '12345678901234567890',
    },
]    
     
for count, commit in enumerate(data, 1):
   diff_table.add_row([
       count,
       commit['title'],
       commit['author_name'],
       '<a href="'+ commit['web_url'] + '">click here</a>',
       commit['committed_date'][:10],
       commit['committed_date'][11:19]
   ])

# --- after loop ---

text = diff_table.get_html_string(format=True)
text = html.unescape(text)

with open('index.html', 'w') as f:
    f.write('<p>'+text+'</p>')

Screenshot from web browser Firefox

Firefox

Screenshot from text web browser Lynx in terminal (Linux)

Lynx

Screenshot from text web browser Links in terminal (Linux).

Top Menu is displayed after pressing ESC.

Links

Screenshot from text web browser W3M in terminal (Linux)

enter image description here

There is also browsh which can display: HTML5, CSS3, JS, video and even WebGL.
But it needs Firefox to render it and it may work much slower.

furas
  • 134,197
  • 12
  • 106
  • 148
  • Thank you @furas, I need to write it in an HTML file, it worked but it showed `commit url` on the terminal. It is ok for now but is there any possibility of making this not happen to show as same as the HTML link? – ganesh tirumani Feb 16 '22 at 05:24
  • I don't understand you. Why do you want display HTML in terminal. Terminal is NOT web browser and it doesn't know what to do with HTML. If you want to see it as link then open it in web browser. And if you want to display in terminal then don't create HTML. – furas Feb 16 '22 at 12:57
  • I check this file in Linux terminal using text web browser [Lynx](http://lynx.browser.org/) - see new image in answer. – furas Feb 16 '22 at 13:47
  • I edited the question by adding two outputs... is that possible? and thanks for recommending the browser – ganesh tirumani Feb 18 '22 at 12:25
  • terminals don't use HTML so you can't use the same table in terminal and browser. In terminal you can only use URL without HTML - and most terminals will recognize `https://...` and treat it as a link. – furas Feb 18 '22 at 12:56
  • Thanks I understood ... but getting to know is there any possibility. – ganesh tirumani Feb 18 '22 at 13:00
  • BTW: [lynx](http://lynx.browser.org/) is probably the oldest text web browser but there are others: [links](http://links.twibright.com/features.php), [w3m](http://w3m.sourceforge.net/), [browsh](https://www.brow.sh/) – furas Feb 18 '22 at 13:02