3

From the documentation I realize that it is possible to create styles over pandas dataframes. However, I would like to know if it is possible to create a link style. So far this is what I tried:

def linkify(data_frame_col):
 list_words = ['<a href="http://this_is_url_{}">{}</a>'.format(a,a) for a in data_frame_col]
 return list_words

The problem is that when I visualize the pandas dataframe into a flask website I get the following style:

['<a href="http://this_is_url_hi">hi</a>',
 '<a href="http://this_is_url_quick fox brown">quick fox brown</a>',
 '<a href="http://this_is_url_fall apart">fall apart</a>',
 '<a href="http://this_is_url_hi">hi</a>',
 '<a href="http://this_is_url_no">no</a>']

Instead of links. Any idea of how to get hrefs like this stye: this inside the pandas dataframe?.

Update

My template looks like this:

view.html:

<!doctype html>
<title>title</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
<div class=page>
  <h1>Title</h1>
  {% for table in tables %}
    <h2>{{titles[loop.index]}}</h2>
    {{ table|safe }}
  {% endfor %}
</div>
john doe
  • 2,233
  • 7
  • 37
  • 58
  • 1
    What does your template look like? – dirn Sep 23 '16 at 20:23
  • 1
    When using pandas with a web applicaiton, please be aware of thread-safety issues: https://stackoverflow.com/questions/25782912/pandas-and-numpy-thread-safety – Boa Sep 23 '16 at 20:35
  • @dim Thanks for the help!... I updated the status of the question – john doe Sep 24 '16 at 01:34
  • How do you render the template? What is `tables`? Does that contain the return value from `linkify`? Or is that in `table`? – dirn Sep 24 '16 at 14:11

1 Answers1

2

You can use the IPython's display.HTML type

from IPython.display import HTML
import pandas as pd

df = pd.DataFrame(['<a href="http://example.com">example.com</a>'])
HTML(df.to_html(escape=False))
Talmaj
  • 217
  • 3
  • 6