yahyalogde
yahyalogde

Reputation: 83

How to make links in pandas dataframe clickable?

I'm currently using beautifulsoup to scrape a table on a site, this table includes links, I am then converting this table into a pandas dataframe and converting it to html using pandas 'to_html' option, this is all running in Django.

This is how I'm creating the table in Python:

res = []
                for row in table.find_all('tr'):
                    row_data = []
                    for td in row.find_all('td'):
                        td_check = td.find('a')
                        if td_check is not None:
                            link = td.find('a')
                            row_data.append(link)
                        else:
                            not_link = ''.join(td.stripped_strings)
                            if not_link == '':
                                not_link = None
                            row_data.append(not_link)
                    res.append(row_data)

I then convert it to HTML using this:

sangerDF = sangerDF.to_html(classes=["table-bordered", "table-striped", "table-hover",], index=False, justify="initial")

But it outputs the table on my site like this:

Table

I don't understand why it isn't clickable? If I inspect a cell in the table using my browser the HTML is:

<td>
   &lt;a href="https://www.sanger.ac.uk/htgt/wge/crispr/1006029202"&gt;1006029202&lt;/a&gt;
</td>

So something is going wrong with the formatting somewhere, how would I fix this?

Thanks!

Upvotes: 0

Views: 2111

Answers (2)

inquam
inquam

Reputation: 12932

I would recommend that you set render_links to True. So:

sangerDF = sangerDF.to_html(classes=["table-bordered", "table-striped", "table-hover",], index=False, justify="initial", render_links = True)

Upvotes: 0

yahyalogde
yahyalogde

Reputation: 83

I figured it out, to my 'to_html' I had to add 'escape=False' in brackets at the end.

so my code before:

sangerDF = sangerDF.to_html(classes=["table-bordered", "table-striped", "table-hover",], index=False, justify="initial")

and after:

sangerDF = sangerDF.to_html(classes=["table-bordered", "table-striped", "table-hover",], index=False, justify="initial", escape=False)

Hope this helps.

Upvotes: 4

Related Questions