栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

让我们的streamlit网页表格美起来的方法

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

让我们的streamlit网页表格美起来的方法

大家好,在我们的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 = """
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/754382.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号