【问题标题】:How to create a pandas "link" (href) style?如何创建熊猫“链接”(href)样式?
【发布时间】:2017-02-01 18:59:08
【问题描述】:

从documentation 我意识到可以在 pandas 数据帧上创建样式。但是,我想知道是否可以创建链接样式。到目前为止,这是我尝试过的:

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

问题是,当我将 pandas 数据框可视化为烧瓶网站时,我得到以下样式:

['<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>']

而不是链接。知道如何在 pandas 数据框中获取像这样的 stye:this 的 href 吗?

更新

我的模板如下所示:

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>

【问题讨论】:

  • 你的模板是什么样的?
  • 在 web 应用程序中使用 pandas 时,请注意线程安全问题:stackoverflow.com/questions/25782912/…
  • @dim 感谢您的帮助!...我更新了问题的状态
  • 如何渲染模板? tables 是什么?是否包含来自linkify 的返回值?还是在table?

标签: python html python-3.x pandas flask


【解决方案1】:

可以使用 IPython 的 display.HTML 类型

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))

【讨论】:

  • 感谢帮助,但我不在Ipython notebook中!
  • 只使用同样适用于您的 df.html,因为浏览器会为您呈现特定列的 Html 代码
猜你喜欢
  • 2021-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-10
  • 2018-09-06
  • 1970-01-01
  • 2012-09-25
  • 1970-01-01
相关资源
最近更新 更多