Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,34 +3,38 @@ import pandas as pd
|
|
| 3 |
import plotly.graph_objects as go
|
| 4 |
import os
|
| 5 |
from zipfile import ZipFile
|
| 6 |
-
import plotly.io as pio
|
| 7 |
-
|
| 8 |
-
# 強制設定中文字型(例如 Noto Sans CJK)
|
| 9 |
-
pio.kaleido.scope.default_layout.font.family = "Microsoft JhengHei, Noto Sans CJK, Arial"
|
| 10 |
|
| 11 |
try:
|
| 12 |
import kaleido
|
| 13 |
except ImportError:
|
| 14 |
st.error("請安裝 'kaleido' 套件以啟用圖像導出功能:\n\n $ pip install kaleido")
|
| 15 |
|
|
|
|
| 16 |
def load_data(uploaded_file):
|
| 17 |
"""載入並處理CSV檔案"""
|
| 18 |
try:
|
|
|
|
| 19 |
df = pd.read_csv(uploaded_file, encoding='utf-8')
|
|
|
|
|
|
|
| 20 |
df = df.dropna(how='all')
|
|
|
|
|
|
|
| 21 |
numeric_columns = ['平均', '總分', '國文', '英文', '數學', '自科', '社會', '地理', '歷史', '公民']
|
| 22 |
for col in numeric_columns:
|
| 23 |
df[col] = pd.to_numeric(df[col], errors='coerce')
|
|
|
|
| 24 |
return df
|
| 25 |
except Exception as e:
|
| 26 |
st.error(f"載入檔案時發生錯誤:{e}")
|
| 27 |
return None
|
| 28 |
|
|
|
|
| 29 |
def create_radar_chart(df, selected_rows, selected_columns):
|
| 30 |
"""使用Plotly建立雷達圖"""
|
| 31 |
line_styles = ['solid', 'dot', 'dash', 'longdash', 'dashdot']
|
| 32 |
colors = ['#1F77B4', '#FF7F0E', '#2CA02C', '#D62728', '#9467BD']
|
| 33 |
-
|
| 34 |
fig = go.Figure()
|
| 35 |
|
| 36 |
for i, row_name in enumerate(selected_rows):
|
|
@@ -46,7 +50,7 @@ def create_radar_chart(df, selected_rows, selected_columns):
|
|
| 46 |
dash=line_styles[i % len(line_styles)],
|
| 47 |
width=2
|
| 48 |
),
|
| 49 |
-
marker=dict(opacity=0.5)
|
| 50 |
))
|
| 51 |
|
| 52 |
fig.update_layout(
|
|
@@ -56,20 +60,22 @@ def create_radar_chart(df, selected_rows, selected_columns):
|
|
| 56 |
range=[0, df[selected_columns].values.max() * 1.1]
|
| 57 |
),
|
| 58 |
angularaxis=dict(
|
| 59 |
-
tickfont=dict(size=16, color='black')
|
| 60 |
)
|
| 61 |
),
|
| 62 |
showlegend=True,
|
| 63 |
legend=dict(
|
| 64 |
-
font=dict(size=14, color='black')
|
| 65 |
),
|
| 66 |
title='學生成績雷達圖',
|
| 67 |
-
plot_bgcolor='white',
|
| 68 |
-
paper_bgcolor='white'
|
|
|
|
| 69 |
)
|
| 70 |
|
| 71 |
return fig
|
| 72 |
|
|
|
|
| 73 |
def save_all_radar_charts(df, selected_columns, comparison_rows):
|
| 74 |
"""批次生成並儲存每個學生與指定比較對象的雷達圖"""
|
| 75 |
output_dir = "radar_charts"
|
|
@@ -80,15 +86,18 @@ def save_all_radar_charts(df, selected_columns, comparison_rows):
|
|
| 80 |
continue
|
| 81 |
|
| 82 |
fig = create_radar_chart(df, [student] + comparison_rows, selected_columns)
|
|
|
|
|
|
|
| 83 |
fig.update_layout(
|
| 84 |
-
font=dict(
|
|
|
|
|
|
|
| 85 |
)
|
|
|
|
| 86 |
file_path = os.path.join(output_dir, f"{student}_comparison.png")
|
| 87 |
-
|
| 88 |
-
fig.write_image(file_path, engine="kaleido")
|
| 89 |
-
except Exception as e:
|
| 90 |
-
st.error(f"保存圖片時發生錯誤:{e}")
|
| 91 |
|
|
|
|
| 92 |
zip_path = "radar_charts.zip"
|
| 93 |
with ZipFile(zip_path, 'w') as zipf:
|
| 94 |
for file_name in os.listdir(output_dir):
|
|
@@ -96,6 +105,7 @@ def save_all_radar_charts(df, selected_columns, comparison_rows):
|
|
| 96 |
|
| 97 |
return zip_path
|
| 98 |
|
|
|
|
| 99 |
def main():
|
| 100 |
st.title('學生成績雷達圖產生器')
|
| 101 |
|
|
@@ -136,5 +146,7 @@ def main():
|
|
| 136 |
mime="application/zip"
|
| 137 |
)
|
| 138 |
|
|
|
|
| 139 |
if __name__ == "__main__":
|
| 140 |
main()
|
|
|
|
|
|
| 3 |
import plotly.graph_objects as go
|
| 4 |
import os
|
| 5 |
from zipfile import ZipFile
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
try:
|
| 8 |
import kaleido
|
| 9 |
except ImportError:
|
| 10 |
st.error("請安裝 'kaleido' 套件以啟用圖像導出功能:\n\n $ pip install kaleido")
|
| 11 |
|
| 12 |
+
|
| 13 |
def load_data(uploaded_file):
|
| 14 |
"""載入並處理CSV檔案"""
|
| 15 |
try:
|
| 16 |
+
# 直接載入檔案
|
| 17 |
df = pd.read_csv(uploaded_file, encoding='utf-8')
|
| 18 |
+
|
| 19 |
+
# 移除空白列
|
| 20 |
df = df.dropna(how='all')
|
| 21 |
+
|
| 22 |
+
# 將數值欄位轉換為數字類型
|
| 23 |
numeric_columns = ['平均', '總分', '國文', '英文', '數學', '自科', '社會', '地理', '歷史', '公民']
|
| 24 |
for col in numeric_columns:
|
| 25 |
df[col] = pd.to_numeric(df[col], errors='coerce')
|
| 26 |
+
|
| 27 |
return df
|
| 28 |
except Exception as e:
|
| 29 |
st.error(f"載入檔案時發生錯誤:{e}")
|
| 30 |
return None
|
| 31 |
|
| 32 |
+
|
| 33 |
def create_radar_chart(df, selected_rows, selected_columns):
|
| 34 |
"""使用Plotly建立雷達圖"""
|
| 35 |
line_styles = ['solid', 'dot', 'dash', 'longdash', 'dashdot']
|
| 36 |
colors = ['#1F77B4', '#FF7F0E', '#2CA02C', '#D62728', '#9467BD']
|
| 37 |
+
|
| 38 |
fig = go.Figure()
|
| 39 |
|
| 40 |
for i, row_name in enumerate(selected_rows):
|
|
|
|
| 50 |
dash=line_styles[i % len(line_styles)],
|
| 51 |
width=2
|
| 52 |
),
|
| 53 |
+
marker=dict(opacity=0.5) # 增加透明度
|
| 54 |
))
|
| 55 |
|
| 56 |
fig.update_layout(
|
|
|
|
| 60 |
range=[0, df[selected_columns].values.max() * 1.1]
|
| 61 |
),
|
| 62 |
angularaxis=dict(
|
| 63 |
+
tickfont=dict(size=16, color='black') # 放大字體,顏色為黑色
|
| 64 |
)
|
| 65 |
),
|
| 66 |
showlegend=True,
|
| 67 |
legend=dict(
|
| 68 |
+
font=dict(size=14, color='black') # 圖例文字加大並改為黑色
|
| 69 |
),
|
| 70 |
title='學生成績雷達圖',
|
| 71 |
+
plot_bgcolor='white', # 背景為白色
|
| 72 |
+
paper_bgcolor='white', # 紙張背景為白色
|
| 73 |
+
font=dict(family="Microsoft JhengHei, Noto Sans CJK, Arial") # 字型設定
|
| 74 |
)
|
| 75 |
|
| 76 |
return fig
|
| 77 |
|
| 78 |
+
|
| 79 |
def save_all_radar_charts(df, selected_columns, comparison_rows):
|
| 80 |
"""批次生成並儲存每個學生與指定比較對象的雷達圖"""
|
| 81 |
output_dir = "radar_charts"
|
|
|
|
| 86 |
continue
|
| 87 |
|
| 88 |
fig = create_radar_chart(df, [student] + comparison_rows, selected_columns)
|
| 89 |
+
|
| 90 |
+
# 在這裡更新字型
|
| 91 |
fig.update_layout(
|
| 92 |
+
font=dict(
|
| 93 |
+
family="Microsoft JhengHei, Noto Sans CJK, Arial"
|
| 94 |
+
)
|
| 95 |
)
|
| 96 |
+
|
| 97 |
file_path = os.path.join(output_dir, f"{student}_comparison.png")
|
| 98 |
+
fig.write_image(file_path, engine="kaleido")
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
# 壓縮所有圖表
|
| 101 |
zip_path = "radar_charts.zip"
|
| 102 |
with ZipFile(zip_path, 'w') as zipf:
|
| 103 |
for file_name in os.listdir(output_dir):
|
|
|
|
| 105 |
|
| 106 |
return zip_path
|
| 107 |
|
| 108 |
+
|
| 109 |
def main():
|
| 110 |
st.title('學生成績雷達圖產生器')
|
| 111 |
|
|
|
|
| 146 |
mime="application/zip"
|
| 147 |
)
|
| 148 |
|
| 149 |
+
|
| 150 |
if __name__ == "__main__":
|
| 151 |
main()
|
| 152 |
+
|