python - 将数据框从 Python 写入 html 时修复表头
问题描述
我试图headers
在写我的时候修复我dataframe
的 to HTML
。我浏览了这个似乎有类似问题的链接 ,但这并没有真正帮助,因为我正在创建styles
in Python
。
这是我的工作代码
import numpy as np
import pandas as pd
from IPython.display import HTML
df.index = np.arange(1, len(df)+1)
def hover(hover_color="#FF5733"):
return dict(selector="tbody tr:hover",
props=[("background-color", "%s" % hover_color)])
styles = [
#table properties
dict(selector=" ",
props=[("margin","0"),
("font-family",'calibri'),
("border-collapse", "collapse"),
("border","none"),
("border", "2px solid #ccf")
]),
#header color - optional
dict(selector="thead",
props=[("background-color","#489AF3"),
("position", "sticky")
]),
#background shading
dict(selector="tbody tr:nth-child(even)",
props=[("background-color", "#fff")]),
dict(selector="tbody tr:nth-child(odd)",
props=[("background-color", "#eee")]),
#cell spacing
dict(selector="td",
props=[("padding", ".5em"),
("overflow", "hiddeb"),
("text-overflow", "ellipsis")]),
#header cell properties
dict(selector="tblTitle",
props=[("font-size", "95%"),
("text-align", "center"),
("position", "sticky"),
("top", 0)]),
#caption placement
dict(selector="caption",
props=[("caption-side", "bottom")]),
dict(selector="body_panel",
props=[("display", "inline-block"),
("overflow-y", "scroll")]),
#render hover last to override background-color
hover()
]
# this forces grey background on header
styler = df.style.set_properties(**{'text-align': 'center'}).set_table_styles(styles)
# HTML string
html = styler.render()
# save html
with open('wwx.html', 'w') as f:
f.write(html)
解决方案
推荐阅读
- r - R - 间歇性 Missig 条形图 ggplot
- python - Python 实时浮点打印
- r - 在 R 中并行运行循环以进行 tar.gz 提取、NetCDF 处理和 CSV 文件输出
- batch-file - Windows 批处理脚本将最新文件从 Windows 的特定目录传输到每 30 分钟运行一次的 Linux 服务器
- google-colaboratory - 为什么 Google colab 会说:chmod: cannot access 'RDP.sh': No such file or directory
- python - 使用 Beautiful Soup 在网站中使用两个表进行 Web 抓取
- sql - 如何在 ibm db2 中执行事务
- math - 关于SVD,奇异值分解的问题
- java - Appium:appium无法识别chromeOptions
- asp.net-mvc - 如何在 ASP.NET mvc 上使用 npm 安装包