大家好,在我们的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_temp = []
for k in range(len(list(columns))):
thead_temp.append(""""""+str(list(columns)[k])+""" """)
header = thead1+"".join(thead_temp)+""""""
rows = []
rows_temp = []
for i in range(df.shape[0]):
rows.append(""""""+str(i+1)+""" """)
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(""""""+str(rows_temp[j][m])+""" """)
td_temp2 = []
for n in range(len(td_temp)):
td_temp2.append(td_temp[n:n+df.shape[1]])
td_temp3 = []
for x in range(len(td_temp2)):
if int(x % (df.shape[1])) == 0:
td_temp3.append(td_temp2[x])
td_temp4 = []
for xx in range(len(td_temp3)):
td_temp4.append("".join(td_temp3[xx]))
td_temp5 = []
for v in range(len(td_temp4)):
td_temp5.append(""""""+str(v+1)+""" """+str(td_temp4[v])+""" """)
table_html = """


