HTML接收html数据的自定义字符串。没有人禁止您传入带有自定义CSS样式的样式标签
.dataframe(该
to_html方法将其添加到表中)。
因此,最简单的解决方案是仅添加样式并将其与的输出连接
df.to_html:
style = '<style>.dataframe td { text-align: right; }</style>'HTML( style + df.to_html( formatters=frmt ) )但我建议为Dataframe定义一个自定义类,因为这将更改笔记本中所有表的样式(样式为“全局”)。
style = '<style>.right_aligned_df td { text-align: right; }</style>'HTML(style + df.to_html(formatters=frmt, classes='right_aligned_df'))您还可以在前面的单元格之一中定义样式,然后只需设置方法的
classes参数即可
to_html:
# Some cell at the begining of the notebookIn [2]: HTML('''<style> .right_aligned_df td { text-align: right; } .left_aligned_df td { text-align: right; } .pink_df { background-color: pink; } </style>''')...# Much later in your notebookIn [66]: HTML(df.to_html(classes='pink_df'))


