0

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!

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This totally helped me. I was using a formatter to turn a TD value into a link; it would not render until i added the escape=False flag.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.