Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,8 +4,9 @@ import plotly.graph_objects as go
|
|
| 4 |
import os
|
| 5 |
from zipfile import ZipFile
|
| 6 |
import plotly.io as pio
|
| 7 |
-
from PIL import Image, ImageDraw
|
| 8 |
from io import BytesIO
|
|
|
|
| 9 |
|
| 10 |
try:
|
| 11 |
import kaleido
|
|
@@ -71,7 +72,7 @@ def create_radar_chart(df, selected_rows, selected_columns):
|
|
| 71 |
title='學生成績雷達圖',
|
| 72 |
plot_bgcolor='white',
|
| 73 |
paper_bgcolor='white',
|
| 74 |
-
|
| 75 |
)
|
| 76 |
|
| 77 |
return fig
|
|
@@ -80,19 +81,14 @@ def save_all_radar_charts(df, selected_columns, comparison_rows):
|
|
| 80 |
"""批次生成並儲存每個學生與指定比較對象的雷達圖"""
|
| 81 |
output_dir = "radar_charts"
|
| 82 |
os.makedirs(output_dir, exist_ok=True)
|
| 83 |
-
|
| 84 |
-
try:
|
| 85 |
-
font = ImageFont.truetype(font_path, size=12, encoding="utf-8") # 設置中文字型
|
| 86 |
-
except FileNotFoundError:
|
| 87 |
-
st.error(f"找不到字型檔案: {font_path},請確保檔案路徑正確")
|
| 88 |
-
return None
|
| 89 |
|
| 90 |
for student in df['姓名']:
|
| 91 |
if student in comparison_rows:
|
| 92 |
continue
|
| 93 |
|
| 94 |
fig = create_radar_chart(df, [student] + comparison_rows, selected_columns)
|
| 95 |
-
img_bytes = pio.to_image(fig, format="png", engine="
|
| 96 |
img = Image.open(BytesIO(img_bytes)).convert("RGBA")
|
| 97 |
background = Image.new('RGBA', img.size, (255, 255, 255, 255))
|
| 98 |
composite = Image.alpha_composite(background, img)
|
|
@@ -103,7 +99,7 @@ def save_all_radar_charts(df, selected_columns, comparison_rows):
|
|
| 103 |
if isinstance(trace, go.Scatterpolar):
|
| 104 |
for i, theta in enumerate(trace.theta):
|
| 105 |
x, y = fig.layout.polar.radialaxis.range[1] * (0.5 - 0.5*trace.r[i] / fig.layout.polar.radialaxis.range[1]) * np.cos(np.radians(90-theta)), fig.layout.polar.radialaxis.range[1] * (0.5 - 0.5*trace.r[i] / fig.layout.polar.radialaxis.range[1]) * np.sin(np.radians(90-theta))
|
| 106 |
-
draw.text((img.width / 2 + x, img.height / 2 + y - 25), str(trace.name),
|
| 107 |
file_path = os.path.join(output_dir, f"{student}_comparison.png")
|
| 108 |
composite.save(file_path)
|
| 109 |
|
|
@@ -148,7 +144,7 @@ def main():
|
|
| 148 |
st.error("請至少選擇一個比較對象")
|
| 149 |
else:
|
| 150 |
zip_path = save_all_radar_charts(df, selected_columns, comparison_rows)
|
| 151 |
-
if zip_path:
|
| 152 |
with open(zip_path, "rb") as f:
|
| 153 |
st.download_button(
|
| 154 |
label="下載壓縮檔",
|
|
|
|
| 4 |
import os
|
| 5 |
from zipfile import ZipFile
|
| 6 |
import plotly.io as pio
|
| 7 |
+
from PIL import Image, ImageDraw
|
| 8 |
from io import BytesIO
|
| 9 |
+
import numpy as np
|
| 10 |
|
| 11 |
try:
|
| 12 |
import kaleido
|
|
|
|
| 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
|
|
|
|
| 81 |
"""批次生成並儲存每個學生與指定比較對象的雷達圖"""
|
| 82 |
output_dir = "radar_charts"
|
| 83 |
os.makedirs(output_dir, exist_ok=True)
|
| 84 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
for student in df['姓名']:
|
| 87 |
if student in comparison_rows:
|
| 88 |
continue
|
| 89 |
|
| 90 |
fig = create_radar_chart(df, [student] + comparison_rows, selected_columns)
|
| 91 |
+
img_bytes = pio.to_image(fig, format="png", engine="orca") # 使用 orca
|
| 92 |
img = Image.open(BytesIO(img_bytes)).convert("RGBA")
|
| 93 |
background = Image.new('RGBA', img.size, (255, 255, 255, 255))
|
| 94 |
composite = Image.alpha_composite(background, img)
|
|
|
|
| 99 |
if isinstance(trace, go.Scatterpolar):
|
| 100 |
for i, theta in enumerate(trace.theta):
|
| 101 |
x, y = fig.layout.polar.radialaxis.range[1] * (0.5 - 0.5*trace.r[i] / fig.layout.polar.radialaxis.range[1]) * np.cos(np.radians(90-theta)), fig.layout.polar.radialaxis.range[1] * (0.5 - 0.5*trace.r[i] / fig.layout.polar.radialaxis.range[1]) * np.sin(np.radians(90-theta))
|
| 102 |
+
draw.text((img.width / 2 + x, img.height / 2 + y - 25), str(trace.name), fill=(0, 0, 0)) # 直接使用PIL預設字型
|
| 103 |
file_path = os.path.join(output_dir, f"{student}_comparison.png")
|
| 104 |
composite.save(file_path)
|
| 105 |
|
|
|
|
| 144 |
st.error("請至少選擇一個比較對象")
|
| 145 |
else:
|
| 146 |
zip_path = save_all_radar_charts(df, selected_columns, comparison_rows)
|
| 147 |
+
if zip_path:
|
| 148 |
with open(zip_path, "rb") as f:
|
| 149 |
st.download_button(
|
| 150 |
label="下載壓縮檔",
|