Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,17 +12,11 @@ except ImportError:
|
|
| 12 |
def load_data(uploaded_file):
|
| 13 |
"""載入並處理CSV檔案"""
|
| 14 |
try:
|
| 15 |
-
# 直接載入檔案
|
| 16 |
df = pd.read_csv(uploaded_file, encoding='utf-8')
|
| 17 |
-
|
| 18 |
-
# 移除空白列
|
| 19 |
df = df.dropna(how='all')
|
| 20 |
-
|
| 21 |
-
# 將數值欄位轉換為數字類型
|
| 22 |
numeric_columns = ['平均', '總分', '國文', '英文', '數學', '自科', '社會', '地理', '歷史', '公民']
|
| 23 |
for col in numeric_columns:
|
| 24 |
df[col] = pd.to_numeric(df[col], errors='coerce')
|
| 25 |
-
|
| 26 |
return df
|
| 27 |
except Exception as e:
|
| 28 |
st.error(f"載入檔案時發生錯誤:{e}")
|
|
@@ -48,7 +42,7 @@ def create_radar_chart(df, selected_rows, selected_columns):
|
|
| 48 |
dash=line_styles[i % len(line_styles)],
|
| 49 |
width=2
|
| 50 |
),
|
| 51 |
-
marker=dict(opacity=0.5)
|
| 52 |
))
|
| 53 |
|
| 54 |
fig.update_layout(
|
|
@@ -58,10 +52,13 @@ def create_radar_chart(df, selected_rows, selected_columns):
|
|
| 58 |
range=[0, df[selected_columns].values.max() * 1.1]
|
| 59 |
),
|
| 60 |
angularaxis=dict(
|
| 61 |
-
tickfont=dict(size=16, color='black')
|
| 62 |
)
|
| 63 |
),
|
| 64 |
showlegend=True,
|
|
|
|
|
|
|
|
|
|
| 65 |
title='學生成績雷達圖',
|
| 66 |
plot_bgcolor='white', # 背景為白色
|
| 67 |
paper_bgcolor='white' # 紙張背景為白色
|
|
@@ -79,11 +76,15 @@ def save_all_radar_charts(df, selected_columns, comparison_rows):
|
|
| 79 |
continue
|
| 80 |
|
| 81 |
fig = create_radar_chart(df, [student] + comparison_rows, selected_columns)
|
| 82 |
-
fig.update_layout(
|
|
|
|
|
|
|
| 83 |
file_path = os.path.join(output_dir, f"{student}_comparison.png")
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
# 壓縮所有圖表
|
| 87 |
zip_path = "radar_charts.zip"
|
| 88 |
with ZipFile(zip_path, 'w') as zipf:
|
| 89 |
for file_name in os.listdir(output_dir):
|
|
@@ -132,5 +133,4 @@ def main():
|
|
| 132 |
)
|
| 133 |
|
| 134 |
if __name__ == "__main__":
|
| 135 |
-
main()
|
| 136 |
-
|
|
|
|
| 12 |
def load_data(uploaded_file):
|
| 13 |
"""載入並處理CSV檔案"""
|
| 14 |
try:
|
|
|
|
| 15 |
df = pd.read_csv(uploaded_file, encoding='utf-8')
|
|
|
|
|
|
|
| 16 |
df = df.dropna(how='all')
|
|
|
|
|
|
|
| 17 |
numeric_columns = ['平均', '總分', '國文', '英文', '數學', '自科', '社會', '地理', '歷史', '公民']
|
| 18 |
for col in numeric_columns:
|
| 19 |
df[col] = pd.to_numeric(df[col], errors='coerce')
|
|
|
|
| 20 |
return df
|
| 21 |
except Exception as e:
|
| 22 |
st.error(f"載入檔案時發生錯誤:{e}")
|
|
|
|
| 42 |
dash=line_styles[i % len(line_styles)],
|
| 43 |
width=2
|
| 44 |
),
|
| 45 |
+
marker=dict(opacity=0.5)
|
| 46 |
))
|
| 47 |
|
| 48 |
fig.update_layout(
|
|
|
|
| 52 |
range=[0, df[selected_columns].values.max() * 1.1]
|
| 53 |
),
|
| 54 |
angularaxis=dict(
|
| 55 |
+
tickfont=dict(size=16, color='black')
|
| 56 |
)
|
| 57 |
),
|
| 58 |
showlegend=True,
|
| 59 |
+
legend=dict(
|
| 60 |
+
font=dict(size=14, color='black') # 圖例文字放大並改為黑色
|
| 61 |
+
),
|
| 62 |
title='學生成績雷達圖',
|
| 63 |
plot_bgcolor='white', # 背景為白色
|
| 64 |
paper_bgcolor='white' # 紙張背景為白色
|
|
|
|
| 76 |
continue
|
| 77 |
|
| 78 |
fig = create_radar_chart(df, [student] + comparison_rows, selected_columns)
|
| 79 |
+
fig.update_layout(
|
| 80 |
+
font=dict(family="Microsoft JhengHei, Arial", size=12) # 嵌入中文字型
|
| 81 |
+
)
|
| 82 |
file_path = os.path.join(output_dir, f"{student}_comparison.png")
|
| 83 |
+
try:
|
| 84 |
+
fig.write_image(file_path, engine="kaleido")
|
| 85 |
+
except Exception as e:
|
| 86 |
+
st.error(f"保存圖片時發生錯誤:{e}")
|
| 87 |
|
|
|
|
| 88 |
zip_path = "radar_charts.zip"
|
| 89 |
with ZipFile(zip_path, 'w') as zipf:
|
| 90 |
for file_name in os.listdir(output_dir):
|
|
|
|
| 133 |
)
|
| 134 |
|
| 135 |
if __name__ == "__main__":
|
| 136 |
+
main()
|
|
|