1

The HTML file that comes out of Dataframe.to_html() does not create hyperlinks when the string content of one of its columns matches an URI.

Is there a way to generate hyperlinks in html docs from a DataFrame?

K.-Michael Aye
  • 5,465
  • 6
  • 44
  • 56
  • perhaps this question can help: http://stackoverflow.com/questions/1727535/replace-urls-in-text-with-links-to-urls – Andy Hayden Jan 10 '13 at 17:37

3 Answers3

3

Setting escape=False allows you to input custom html and create hyperlinks as demonstrated below.

df = pd.DataFrame(data)
df['url'] = '<a href=' + df['url'] + '><div>' + df['name'] + '</div></a>'
df = df.to_html(escape=False)
Display name
  • 753
  • 10
  • 28
1

I don't think so. The HTMLFormatter used by DataFrame.to_html helps to pretty render a DataFrame in a IPython HTML Notebooks I think.

The method does not parse each element of your DataFrame, i.e. recognizes an URI pattern to write <a href="URI">Content</a> or something else.

I don't think that (1) it's planned and (2) it's not the purpose of this method. Maybe you can add an issue to the GitHub pandas issues page.

dag
  • 441
  • 4
  • 6
0

Depends on how dynamic your links have to be. I'm currently working on the same issue and tried to solve it with jQuery:

$(document).ready(function(){
  $('thead th').each(function(){
    $(this).html('<a href="{% url "cat_view" %}">' + $(this).html() + '</a>');
  });
  $('tbody tr th').each(function(){
    $(this).html('<a href="{% url "date_view" %}">' + $(this).html() + '</a>')
  });
});

This makes the headers links, but you could just use it on $('tobdy tr td') if that's what you need. I currently have django url-template tags for the hrefs but you could basically put anything in there. I'm still struggling to correctly construct the href's as soon as they get dynamic (e.g. {% url 'cat_view' cat=category.pk %})

Philä Bu
  • 95
  • 1
  • 3
  • 10