table / pages /all_scores_250110.py
ytaek-oh's picture
commit
28c0435
import ast
import pandas as pd
import streamlit as st
FILE_MAP = {
"Tag Scores": "data/tag_summary.csv",
"Context Level Scores": "data/context_summary.csv"
}
def render_mi_table(df, level0_cols):
# HTML 스타일 정의
table_style = """
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid black;
text-align: center;
padding: 8px;
}
th {
background-color: #262730;
}
</style>
"""
# 상위 헤더 (레벨 0)
header_html = "<tr>"
for col in level0_cols:
colspan = len(df.xs(col, axis=1, level=0).columns) if col else 1
header_html += f'<th colspan="{colspan}" style="text-align: center;">{col if col else ""}</th>'
header_html += "</tr>"
# 하위 헤더 (레벨 1)
sub_header_html = "<tr>"
for col in df.columns:
sub_header_html += f"<th style='text-align: center;'>{col[1] if len(col) > 1 else col[0]}</th>"
sub_header_html += "</tr>"
# 데이터 HTML 생성
def map_val(value):
try:
value = f"{float(value):.1f}"
except:
value = value
return value
rows_html = ""
for _, row in df.iterrows():
rows_html += "<tr>" + "".join(f"<td>{map_val(value)}</td>" for value in row) + "</tr>"
# 최종 HTML 합치기
table_html = f"""
{table_style}
<table>
{header_html}
{sub_header_html}
{rows_html}
</table>
"""
return table_html
# Streamlit app
def main():
st.title("Interface")
selected = st.selectbox("Select a results data:", list(FILE_MAP.keys()))
df = pd.read_csv(FILE_MAP[selected])
df.columns = pd.MultiIndex.from_tuples([ast.literal_eval(col) for col in df.columns])
level0_cols = []
for col in df.columns:
if col[0] not in level0_cols:
level0_cols.append(col[0])
st.markdown(render_mi_table(df, level0_cols), unsafe_allow_html=True)
if __name__ == "__main__":
main()