Python中Pandas Dataframe中的超链接

时间:2017-09-13 18:57:04

标签: python html pandas dataframe

我有以下代码:

class summary_tables():
    def create_table(self, summary):
        formatting = formatter()
        table = pybloqs.HTMLJinjaTableBlock(summary,formatters=formatting.summary_format(), use_default_formatters=False)
        final_summary = pybloqs.VStack([table])
        return final_summary

class formatter():
    def summary_format(self):
        table_center = pybloqs.FmtAlignTable(alignment='center')
        align_cells = pybloqs.FmtAlignCellContents(alignment='center', apply_to_header_and_index=False)
        header = pybloqs.FmtHeader(fixed_width='auto', column_width='5cm', index_width='1cm', top_padding='2cm')
        heatmap1 = pybloqs.FmtHeatmap(axis=1, max_color=colors.HEATMAP_RED, min_color=colors.HEATMAP_RED, threshold=0.01)
        return [table_center, align_cells, header, heatmap1]

这是为了帮助我从名为Pandas dataframe的{​​{1}}构建和格式化html文件:

summary_df

所以当我跑:

        mkt    price_diff
    0   APPL   '<a href="http://~price/APPL_breakdown.html">3</a>' 
    1   GOOG   '<a href="http://~price/GOOG_breakdown.html">0</a>'
    2   MSFT   '<a href="http://~price/MSFT_breakdown.html">2</a>'

除了突出显示之外,当我打开tables = summary_tables() summary_block = tables.create_table(summary_df) file = os.path.join(os.path.dirname('/home/jbloggs/'), 'Summary.html') block_final = pybloqs.VStack([summary_block]) block_final.save(file) 文件时,我看到了我所期待的一切。在html中查看def summary_format(),您可以看到我正在尝试构建热图:

class formatter()

问题是..我的heatmap1 = pybloqs.FmtHeatmap(axis=1, max_color=colors.HEATMAP_RED, min_color=colors.HEATMAP_RED, threshold=0.01) 将数字附加到超链接:

DatFrame

您可以在此处看到'<a href="http://~price/APPL_breakdown.html">3</a>'` 文件的链接与APPL_breakdown.html号码相关联。

如何解决这个问题,以便格式化程序函数知道查看数字而不是超链接?

1 个答案:

答案 0 :(得分:0)

我曾经通过将任务分成两个步骤来实现非常相似的东西:

1)在原始数据帧中,只有数值。然后热图格式化程序应该很容易找到它。

2)然后将单元格内容更改为单独的格式化程序中的字符串,该格式化程序将单元格值(数字)与超链接组合在一起。这类似于将浮点数转换为漂亮的打印格式,因此您可以使用例如FmtDecimals作为模板(请参阅this)。

由于您的转换为超链接格式化程序非常适合您的应用程序,因此您可以将该格式化程序代码保留在模块中本地,并且您无需自行修改pybloqs。只需将格式化程序添加到格式化程序列表的末尾即可。这是必要的,因为它将修改单元格内容,类似于漂亮打印浮动的格式化程序。