大家好,在我们的Streamlit交流群中经常听到小伙伴们吐槽Streamlit自带的表格样式太不友好了,不仅布局不好,如表格内容无法全部显示出来、每列的内容无法居中显示、主题色也不好看、表格行数信息从0开始不符合大家习惯等。
针对这个问题,作者今天花了一上午的时间,摸索了一下基于Bootrap框架的表格美化方案,支持直接显示pandas.dataframe对象,在这里把实现方法分享给大家,如果大家觉得还不错的话,快快分享给身边的小伙伴哦!
今天的实现效果
今天的实现代码
import streamlit as st
import pandas as pd
import streamlit.components.v1 as components
st.set_page_config(page_title="基于Bootstrap的表格展示案例", page_icon="🅱️", layout="wide")
def draw_table(df, theme, table_height):
columns = df.columns
thead1="""<thead><th scope="col"></th>"""
thead_temp = []
for k in range(len(list(columns))):
thead_temp.append("""<th scope="col" class="text-white">"""+str(list(columns)[k])+"""</th>""")
header = thead1+"".join(thead_temp)+"""</tr></thead>"""
rows = []
rows_temp = []
for i in range(df.shape[0]):
rows.append("""<th scope="row">"""+str(i+1)+"""</th>""")
rows_temp.append(df.iloc[i].values.tolist())
td_temp = []
for j in range(len(rows_temp)):
for m in range(len(rows_temp[j])):
td_temp.append("""<td class="text-white">"""+str(rows_temp[j][m])+"""</td>""")
td_temp2 = []
for n in range