Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,14 @@ import plotly.graph_objects as go
|
|
| 4 |
import os
|
| 5 |
from zipfile import ZipFile
|
| 6 |
import plotly.io as pio
|
|
|
|
|
|
|
|
|
|
| 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:
|
|
@@ -29,7 +31,6 @@ def load_data(uploaded_file):
|
|
| 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']
|
|
@@ -52,26 +53,26 @@ def create_radar_chart(df, selected_rows, selected_columns):
|
|
| 52 |
),
|
| 53 |
marker=dict(opacity=0.5)
|
| 54 |
))
|
| 55 |
-
|
| 56 |
fig.update_layout(
|
| 57 |
-
|
| 58 |
radialaxis=dict(
|
| 59 |
visible=True,
|
| 60 |
range=[0, df[selected_columns].values.max() * 1.1],
|
| 61 |
-
tickfont=dict(size=12, color='black', family="Microsoft JhengHei, Noto Sans CJK, Arial")
|
| 62 |
),
|
| 63 |
-
|
| 64 |
-
|
| 65 |
)
|
| 66 |
),
|
| 67 |
showlegend=True,
|
| 68 |
legend=dict(
|
| 69 |
-
font=dict(size=14, color='black', family="Microsoft JhengHei, Noto Sans CJK, Arial")
|
| 70 |
),
|
| 71 |
title='學生成績雷達圖',
|
| 72 |
-
plot_bgcolor='white',
|
| 73 |
-
paper_bgcolor='white',
|
| 74 |
-
font=dict(family="Microsoft JhengHei, Noto Sans CJK, Arial")
|
| 75 |
)
|
| 76 |
|
| 77 |
return fig
|
|
@@ -84,8 +85,8 @@ def apply_font_to_all_text(fig):
|
|
| 84 |
if hasattr(trace, 'marker') and hasattr(trace.marker, 'textfont'):
|
| 85 |
trace.marker.textfont.family = "Microsoft JhengHei, Noto Sans CJK, Arial"
|
| 86 |
fig.update_layout(
|
| 87 |
-
|
| 88 |
-
|
| 89 |
)
|
| 90 |
)
|
| 91 |
|
|
@@ -94,12 +95,13 @@ def apply_font_to_all_text(fig):
|
|
| 94 |
if hasattr(fig, 'layout') and hasattr(fig.layout, 'yaxis'):
|
| 95 |
fig.layout.yaxis.tickfont.family = "Microsoft JhengHei, Noto Sans CJK, Arial"
|
| 96 |
if hasattr(fig, 'layout') and hasattr(fig.layout, 'polar') and hasattr(fig.layout.polar, 'radialaxis'):
|
| 97 |
-
|
| 98 |
if hasattr(fig, 'layout') and hasattr(fig.layout, 'polar') and hasattr(fig.layout.polar, 'angularaxis'):
|
| 99 |
-
|
| 100 |
|
| 101 |
return fig
|
| 102 |
|
|
|
|
| 103 |
def save_all_radar_charts(df, selected_columns, comparison_rows):
|
| 104 |
"""批次生成並儲存每個學生與指定比較對象的雷達圖"""
|
| 105 |
output_dir = "radar_charts"
|
|
@@ -108,17 +110,26 @@ def save_all_radar_charts(df, selected_columns, comparison_rows):
|
|
| 108 |
for student in df['姓名']:
|
| 109 |
if student in comparison_rows:
|
| 110 |
continue
|
| 111 |
-
|
|
|
|
| 112 |
fig = create_radar_chart(df, [student] + comparison_rows, selected_columns)
|
| 113 |
fig = apply_font_to_all_text(fig)
|
| 114 |
|
| 115 |
-
#
|
| 116 |
-
pio.
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
fig.write_image(file_path, engine="kaleido")
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
# 壓縮所有圖表
|
| 124 |
zip_path = "radar_charts.zip"
|
|
@@ -128,7 +139,6 @@ def save_all_radar_charts(df, selected_columns, comparison_rows):
|
|
| 128 |
|
| 129 |
return zip_path
|
| 130 |
|
| 131 |
-
|
| 132 |
def main():
|
| 133 |
st.title('學生成績雷達圖產生器')
|
| 134 |
|
|
@@ -168,13 +178,12 @@ def main():
|
|
| 168 |
file_name="radar_charts.zip",
|
| 169 |
mime="application/zip",
|
| 170 |
key="download_button",
|
| 171 |
-
on_click=lambda: st.session_state.update({'download_triggered':True}),
|
| 172 |
-
disabled=st.session_state.get("download_triggered",False)
|
| 173 |
)
|
| 174 |
st.session_state.download_triggered = False
|
| 175 |
|
| 176 |
if __name__ == "__main__":
|
| 177 |
if 'download_triggered' not in st.session_state:
|
| 178 |
st.session_state.download_triggered = False
|
| 179 |
-
|
| 180 |
main()
|
|
|
|
| 4 |
import os
|
| 5 |
from zipfile import ZipFile
|
| 6 |
import plotly.io as pio
|
| 7 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
|
| 10 |
try:
|
| 11 |
import kaleido
|
| 12 |
except ImportError:
|
| 13 |
st.error("請安裝 'kaleido' 套件以啟用圖像導出功能:\n\n $ pip install kaleido")
|
| 14 |
|
|
|
|
| 15 |
def load_data(uploaded_file):
|
| 16 |
"""載入並處理CSV檔案"""
|
| 17 |
try:
|
|
|
|
| 31 |
st.error(f"載入檔案時發生錯誤:{e}")
|
| 32 |
return None
|
| 33 |
|
|
|
|
| 34 |
def create_radar_chart(df, selected_rows, selected_columns):
|
| 35 |
"""使用Plotly建立雷達圖"""
|
| 36 |
line_styles = ['solid', 'dot', 'dash', 'longdash', 'dashdot']
|
|
|
|
| 53 |
),
|
| 54 |
marker=dict(opacity=0.5)
|
| 55 |
))
|
| 56 |
+
|
| 57 |
fig.update_layout(
|
| 58 |
+
polar=dict(
|
| 59 |
radialaxis=dict(
|
| 60 |
visible=True,
|
| 61 |
range=[0, df[selected_columns].values.max() * 1.1],
|
| 62 |
+
tickfont=dict(size=12, color='black', family="Microsoft JhengHei, Noto Sans CJK, Arial")
|
| 63 |
),
|
| 64 |
+
angularaxis=dict(
|
| 65 |
+
tickfont=dict(size=16, color='black', family="Microsoft JhengHei, Noto Sans CJK, Arial")
|
| 66 |
)
|
| 67 |
),
|
| 68 |
showlegend=True,
|
| 69 |
legend=dict(
|
| 70 |
+
font=dict(size=14, color='black', family="Microsoft JhengHei, Noto Sans CJK, Arial")
|
| 71 |
),
|
| 72 |
title='學生成績雷達圖',
|
| 73 |
+
plot_bgcolor='white',
|
| 74 |
+
paper_bgcolor='white',
|
| 75 |
+
font=dict(family="Microsoft JhengHei, Noto Sans CJK, Arial")
|
| 76 |
)
|
| 77 |
|
| 78 |
return fig
|
|
|
|
| 85 |
if hasattr(trace, 'marker') and hasattr(trace.marker, 'textfont'):
|
| 86 |
trace.marker.textfont.family = "Microsoft JhengHei, Noto Sans CJK, Arial"
|
| 87 |
fig.update_layout(
|
| 88 |
+
font=dict(
|
| 89 |
+
family="Microsoft JhengHei, Noto Sans CJK, Arial"
|
| 90 |
)
|
| 91 |
)
|
| 92 |
|
|
|
|
| 95 |
if hasattr(fig, 'layout') and hasattr(fig.layout, 'yaxis'):
|
| 96 |
fig.layout.yaxis.tickfont.family = "Microsoft JhengHei, Noto Sans CJK, Arial"
|
| 97 |
if hasattr(fig, 'layout') and hasattr(fig.layout, 'polar') and hasattr(fig.layout.polar, 'radialaxis'):
|
| 98 |
+
fig.layout.polar.radialaxis.tickfont.family = "Microsoft JhengHei, Noto Sans CJK, Arial"
|
| 99 |
if hasattr(fig, 'layout') and hasattr(fig.layout, 'polar') and hasattr(fig.layout.polar, 'angularaxis'):
|
| 100 |
+
fig.layout.polar.angularaxis.tickfont.family = "Microsoft JhengHei, Noto Sans CJK, Arial"
|
| 101 |
|
| 102 |
return fig
|
| 103 |
|
| 104 |
+
|
| 105 |
def save_all_radar_charts(df, selected_columns, comparison_rows):
|
| 106 |
"""批次生成並儲存每個學生與指定比較對象的雷達圖"""
|
| 107 |
output_dir = "radar_charts"
|
|
|
|
| 110 |
for student in df['姓名']:
|
| 111 |
if student in comparison_rows:
|
| 112 |
continue
|
| 113 |
+
|
| 114 |
+
# 建立圖表
|
| 115 |
fig = create_radar_chart(df, [student] + comparison_rows, selected_columns)
|
| 116 |
fig = apply_font_to_all_text(fig)
|
| 117 |
|
| 118 |
+
# 使用 to_image 取得圖表影像
|
| 119 |
+
img_bytes = pio.to_image(fig, format="png", engine="kaleido")
|
| 120 |
|
| 121 |
+
# 使用 PIL 打開影像
|
| 122 |
+
img = Image.open(BytesIO(img_bytes)).convert("RGBA")
|
|
|
|
| 123 |
|
| 124 |
+
# 建立空白背景
|
| 125 |
+
background = Image.new('RGBA', img.size, (255, 255, 255, 255))
|
| 126 |
+
|
| 127 |
+
# 合成圖片
|
| 128 |
+
composite = Image.alpha_composite(background, img)
|
| 129 |
+
|
| 130 |
+
# 儲存圖片
|
| 131 |
+
file_path = os.path.join(output_dir, f"{student}_comparison.png")
|
| 132 |
+
composite.save(file_path)
|
| 133 |
|
| 134 |
# 壓縮所有圖表
|
| 135 |
zip_path = "radar_charts.zip"
|
|
|
|
| 139 |
|
| 140 |
return zip_path
|
| 141 |
|
|
|
|
| 142 |
def main():
|
| 143 |
st.title('學生成績雷達圖產生器')
|
| 144 |
|
|
|
|
| 178 |
file_name="radar_charts.zip",
|
| 179 |
mime="application/zip",
|
| 180 |
key="download_button",
|
| 181 |
+
on_click=lambda: st.session_state.update({'download_triggered':True}),
|
| 182 |
+
disabled=st.session_state.get("download_triggered",False)
|
| 183 |
)
|
| 184 |
st.session_state.download_triggered = False
|
| 185 |
|
| 186 |
if __name__ == "__main__":
|
| 187 |
if 'download_triggered' not in st.session_state:
|
| 188 |
st.session_state.download_triggered = False
|
|
|
|
| 189 |
main()
|