Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,18 +3,27 @@ import pandas as pd
|
|
| 3 |
import plotly.graph_objects as go
|
| 4 |
import os
|
| 5 |
from zipfile import ZipFile
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
def load_data(uploaded_file):
|
| 11 |
"""載入並處理CSV檔案"""
|
| 12 |
try:
|
|
|
|
| 13 |
df = pd.read_csv(uploaded_file, encoding='utf-8')
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
numeric_columns = ['平均', '總分', '國文', '英文', '數學', '自科', '社會', '地理', '歷史', '公民']
|
| 16 |
for col in numeric_columns:
|
| 17 |
df[col] = pd.to_numeric(df[col], errors='coerce')
|
|
|
|
| 18 |
return df
|
| 19 |
except Exception as e:
|
| 20 |
st.error(f"載入檔案時發生錯誤:{e}")
|
|
@@ -40,24 +49,23 @@ def create_radar_chart(df, selected_rows, selected_columns):
|
|
| 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,
|
| 51 |
-
tickvals=[0, 20, 40, 60, 80, 100],
|
| 52 |
-
tickfont=dict(size=12, color='black') # 修改尺度標註字體顏色為黑色
|
| 53 |
),
|
| 54 |
angularaxis=dict(
|
| 55 |
-
tickfont=dict(size=
|
| 56 |
)
|
| 57 |
),
|
| 58 |
showlegend=True,
|
| 59 |
legend=dict(
|
| 60 |
-
font=dict(size=14, color='black') #
|
| 61 |
),
|
| 62 |
title='學生成績雷達圖',
|
| 63 |
plot_bgcolor='white', # 背景為白色
|
|
@@ -79,17 +87,15 @@ def save_all_radar_charts(df, selected_columns, comparison_rows):
|
|
| 79 |
|
| 80 |
fig = create_radar_chart(df, [student] + comparison_rows, selected_columns)
|
| 81 |
|
| 82 |
-
#
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
png_path = os.path.join(output_dir, f"{student}_comparison.png")
|
| 92 |
-
cairosvg.svg2png(bytestring=svg_content.encode('utf-8'), write_to=png_path)
|
| 93 |
|
| 94 |
# 壓縮所有圖表
|
| 95 |
zip_path = "radar_charts.zip"
|
|
|
|
| 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}")
|
|
|
|
| 49 |
color=colors[i % len(colors)],
|
| 50 |
dash=line_styles[i % len(line_styles)],
|
| 51 |
width=2
|
| 52 |
+
),
|
| 53 |
+
marker=dict(opacity=0.5) # 增加透明度
|
| 54 |
))
|
| 55 |
|
| 56 |
fig.update_layout(
|
| 57 |
polar=dict(
|
| 58 |
radialaxis=dict(
|
| 59 |
visible=True,
|
| 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', # 背景為白色
|
|
|
|
| 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"
|