python修改html表格,使用styles和css更改pandas dataframe html表python中...
這需要幾個步驟:
首先導入HTML并重新輸入
from IPython.display import HTML
import re
你可以通過to_html方法得到html pandas.
df_html = df.to_html()
接下來,我們將為html表和我們要創建的樣式生成隨機標識符.
random_id = 'id%d' % np.random.choice(np.arange(1000000))
因為我們要插入一些樣式,所以我們需要注意指定此樣式僅適用于我們的表.現在讓我們將其插入到df_html中
df_html = re.sub(r'
并創建一個樣式標記.這真的取決于你.我剛添加了一些懸停效果.
style = """
table#{random_id} tr:hover {{background-color: #f5f5f5}}
""".format(random_id=random_id)
最后,顯示它
HTML(style + df_html)
功能齊全.
def HTML_with_style(df, style=None, random_id=None):
from IPython.display import HTML
import numpy as np
import re
df_html = df.to_html()
if random_id is None:
random_id = 'id%d' % np.random.choice(np.arange(1000000))
if style is None:
style = """
table#{random_id} {{color: blue}}
""".format(random_id=random_id)
else:
new_style = []
s = re.sub(r'?style>', '', style).strip()
for line in s.split('\n'):
line = line.strip()
if not re.match(r'^table', line):
line = re.sub(r'^', 'table ', line)
new_style.append(line)
new_style = ['']
style = re.sub(r'table(#\S+)?', 'table#%s' % random_id, '\n'.join(new_style))
df_html = re.sub(r'
return HTML(style + df_html)
像這樣使用它:
HTML_with_style(df.head())
HTML_with_style(df.head(), '')
style = """
tr:nth-child(even) {color: green;}
tr:nth-child(odd) {color: aqua;}
"""
HTML_with_style(df.head(), style)
學習CSS并瘋狂!
總結
以上是生活随笔為你收集整理的python修改html表格,使用styles和css更改pandas dataframe html表python中...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Fashion-MNIST数据集离线加载
- 下一篇: Python算法教程:找出图的连通分量