Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,17 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import plotly.graph_objects as go
|
| 4 |
-
import numpy as np
|
| 5 |
|
| 6 |
def load_data(uploaded_file):
|
| 7 |
"""載入並處理CSV檔案"""
|
| 8 |
try:
|
| 9 |
-
#
|
| 10 |
-
df = pd.read_csv(uploaded_file, encoding='utf-8'
|
| 11 |
|
| 12 |
# 移除空白列
|
| 13 |
df = df.dropna(how='all')
|
| 14 |
-
|
| 15 |
-
#
|
| 16 |
numeric_columns = ['平均', '總分', '國文', '英文', '數學', '自科', '社會', '地理', '歷史', '公民']
|
| 17 |
for col in numeric_columns:
|
| 18 |
df[col] = pd.to_numeric(df[col], errors='coerce')
|
|
@@ -24,101 +23,62 @@ def load_data(uploaded_file):
|
|
| 24 |
|
| 25 |
def create_radar_chart(df, selected_rows, selected_columns):
|
| 26 |
"""使用Plotly建立雷達圖"""
|
| 27 |
-
# 定義不同線段樣式
|
| 28 |
line_styles = ['solid', 'dot', 'dash', 'longdash', 'dashdot']
|
| 29 |
-
|
| 30 |
-
# 定義顏色調色盤
|
| 31 |
colors = ['#1F77B4', '#FF7F0E', '#2CA02C', '#D62728', '#9467BD']
|
| 32 |
|
| 33 |
fig = go.Figure()
|
| 34 |
|
| 35 |
-
# 依次加入選取的列
|
| 36 |
for i, row_name in enumerate(selected_rows):
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
row_data = df[df['姓名'].str.contains(row_name)][selected_columns].iloc[0]
|
| 40 |
-
else:
|
| 41 |
-
row_data = df[df['姓名'] == row_name][selected_columns].iloc[0]
|
| 42 |
-
|
| 43 |
fig.add_trace(go.Scatterpolar(
|
| 44 |
-
r
|
| 45 |
-
theta
|
| 46 |
-
fill
|
| 47 |
-
name
|
| 48 |
-
line
|
| 49 |
-
color
|
| 50 |
-
dash
|
| 51 |
-
width
|
| 52 |
-
)
|
| 53 |
-
marker = dict(symbol='circle')
|
| 54 |
))
|
| 55 |
|
| 56 |
fig.update_layout(
|
| 57 |
-
polar
|
| 58 |
-
radialaxis
|
| 59 |
-
visible
|
| 60 |
-
range
|
| 61 |
)
|
| 62 |
),
|
| 63 |
-
showlegend
|
| 64 |
-
title
|
| 65 |
)
|
| 66 |
-
|
| 67 |
return fig
|
| 68 |
|
| 69 |
def main():
|
| 70 |
st.title('學生成績雷達圖產生器')
|
| 71 |
-
|
| 72 |
-
# 上傳CSV檔案
|
| 73 |
uploaded_file = st.file_uploader("上傳CSV檔案", type=['csv'])
|
| 74 |
-
|
| 75 |
if uploaded_file is not None:
|
| 76 |
df = load_data(uploaded_file)
|
| 77 |
-
|
| 78 |
if df is not None:
|
| 79 |
-
# 定義固定的數值欄位
|
| 80 |
numeric_columns = ['平均', '總分', '國文', '英文', '數學', '自科', '社會', '地理', '歷史', '公民']
|
| 81 |
-
|
| 82 |
-
# 篩選出數值欄位
|
| 83 |
-
available_columns = [col for col in numeric_columns if col in df.columns]
|
| 84 |
-
|
| 85 |
-
# 準備行選擇選項
|
| 86 |
-
# 學生名稱 + 班級平均 + 區域均標
|
| 87 |
-
row_options = list(df['姓名'].dropna()) + [row for row in df['姓名'] if '班平均' in str(row) or '區標' in str(row)]
|
| 88 |
-
|
| 89 |
st.write("### 選擇要比較的欄位")
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
for column in available_columns:
|
| 93 |
-
if st.checkbox(column, key=column):
|
| 94 |
-
selected_columns.append(column)
|
| 95 |
-
|
| 96 |
st.write("### 選擇要比較的對象")
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
'選擇要比較的對象',
|
| 100 |
-
row_options
|
| 101 |
-
)
|
| 102 |
-
|
| 103 |
if selected_columns and selected_rows:
|
| 104 |
try:
|
| 105 |
-
# 建立雷達圖
|
| 106 |
fig = create_radar_chart(df, selected_rows, selected_columns)
|
| 107 |
-
|
| 108 |
-
# 顯示圖表
|
| 109 |
st.plotly_chart(fig, use_container_width=True)
|
| 110 |
-
|
| 111 |
-
# 下載圖表
|
| 112 |
-
fig_bytes = fig.to_image(format="png", width=1200, height=800)
|
| 113 |
-
st.download_button(
|
| 114 |
-
label="下載雷達圖",
|
| 115 |
-
data=fig_bytes,
|
| 116 |
-
file_name="student_scores_radar_chart.png",
|
| 117 |
-
mime="image/png"
|
| 118 |
-
)
|
| 119 |
-
|
| 120 |
except Exception as e:
|
| 121 |
st.error(f"生成雷達圖時發生錯誤:{e}")
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
| 124 |
-
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import plotly.graph_objects as go
|
|
|
|
| 4 |
|
| 5 |
def load_data(uploaded_file):
|
| 6 |
"""載入並處理CSV檔案"""
|
| 7 |
try:
|
| 8 |
+
# 直接載入檔案
|
| 9 |
+
df = pd.read_csv(uploaded_file, encoding='utf-8')
|
| 10 |
|
| 11 |
# 移除空白列
|
| 12 |
df = df.dropna(how='all')
|
| 13 |
+
|
| 14 |
+
# 將數值欄位轉換為數字類型
|
| 15 |
numeric_columns = ['平均', '總分', '國文', '英文', '數學', '自科', '社會', '地理', '歷史', '公民']
|
| 16 |
for col in numeric_columns:
|
| 17 |
df[col] = pd.to_numeric(df[col], errors='coerce')
|
|
|
|
| 23 |
|
| 24 |
def create_radar_chart(df, selected_rows, selected_columns):
|
| 25 |
"""使用Plotly建立雷達圖"""
|
|
|
|
| 26 |
line_styles = ['solid', 'dot', 'dash', 'longdash', 'dashdot']
|
|
|
|
|
|
|
| 27 |
colors = ['#1F77B4', '#FF7F0E', '#2CA02C', '#D62728', '#9467BD']
|
| 28 |
|
| 29 |
fig = go.Figure()
|
| 30 |
|
|
|
|
| 31 |
for i, row_name in enumerate(selected_rows):
|
| 32 |
+
row_data = df[df['姓名'] == row_name][selected_columns].iloc[0]
|
| 33 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
fig.add_trace(go.Scatterpolar(
|
| 35 |
+
r=row_data.values,
|
| 36 |
+
theta=selected_columns,
|
| 37 |
+
fill='toself',
|
| 38 |
+
name=row_name,
|
| 39 |
+
line=dict(
|
| 40 |
+
color=colors[i % len(colors)],
|
| 41 |
+
dash=line_styles[i % len(line_styles)],
|
| 42 |
+
width=2
|
| 43 |
+
)
|
|
|
|
| 44 |
))
|
| 45 |
|
| 46 |
fig.update_layout(
|
| 47 |
+
polar=dict(
|
| 48 |
+
radialaxis=dict(
|
| 49 |
+
visible=True,
|
| 50 |
+
range=[0, df[selected_columns].values.max() * 1.1]
|
| 51 |
)
|
| 52 |
),
|
| 53 |
+
showlegend=True,
|
| 54 |
+
title='學生成績雷達圖'
|
| 55 |
)
|
| 56 |
+
|
| 57 |
return fig
|
| 58 |
|
| 59 |
def main():
|
| 60 |
st.title('學生成績雷達圖產生器')
|
| 61 |
+
|
|
|
|
| 62 |
uploaded_file = st.file_uploader("上傳CSV檔案", type=['csv'])
|
| 63 |
+
|
| 64 |
if uploaded_file is not None:
|
| 65 |
df = load_data(uploaded_file)
|
| 66 |
+
|
| 67 |
if df is not None:
|
|
|
|
| 68 |
numeric_columns = ['平均', '總分', '國文', '英文', '數學', '自科', '社會', '地理', '歷史', '公民']
|
| 69 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
st.write("### 選擇要比較的欄位")
|
| 71 |
+
selected_columns = [col for col in numeric_columns if st.checkbox(col, key=col)]
|
| 72 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
st.write("### 選擇要比較的對象")
|
| 74 |
+
selected_rows = st.multiselect('選擇要比較的對象', df['姓名'].tolist())
|
| 75 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
if selected_columns and selected_rows:
|
| 77 |
try:
|
|
|
|
| 78 |
fig = create_radar_chart(df, selected_rows, selected_columns)
|
|
|
|
|
|
|
| 79 |
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
except Exception as e:
|
| 81 |
st.error(f"生成雷達圖時發生錯誤:{e}")
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|
| 84 |
+
main()
|