Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,12 +7,22 @@ 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
|
| 13 |
except ImportError:
|
| 14 |
st.error("請安裝 'kaleido' 套件以啟用圖像導出功能:\n\n $ pip install kaleido")
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def load_data(uploaded_file):
|
| 17 |
"""載入並處理CSV檔案"""
|
| 18 |
try:
|
|
@@ -82,7 +92,6 @@ def save_all_radar_charts(df, selected_columns, comparison_rows):
|
|
| 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
|
|
@@ -94,12 +103,12 @@ def save_all_radar_charts(df, selected_columns, comparison_rows):
|
|
| 94 |
composite = Image.alpha_composite(background, img)
|
| 95 |
draw = ImageDraw.Draw(composite)
|
| 96 |
|
| 97 |
-
|
| 98 |
for trace in fig.data:
|
| 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))
|
| 103 |
file_path = os.path.join(output_dir, f"{student}_comparison.png")
|
| 104 |
composite.save(file_path)
|
| 105 |
|
|
@@ -145,7 +154,7 @@ def main():
|
|
| 145 |
else:
|
| 146 |
zip_path = save_all_radar_charts(df, selected_columns, comparison_rows)
|
| 147 |
if zip_path:
|
| 148 |
-
|
| 149 |
st.download_button(
|
| 150 |
label="下載壓縮檔",
|
| 151 |
data=f,
|
|
@@ -155,7 +164,7 @@ def main():
|
|
| 155 |
on_click=lambda: st.session_state.update({'download_triggered':True}),
|
| 156 |
disabled=st.session_state.get("download_triggered",False)
|
| 157 |
)
|
| 158 |
-
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|
| 161 |
if 'download_triggered' not in st.session_state:
|
|
|
|
| 7 |
from PIL import Image, ImageDraw
|
| 8 |
from io import BytesIO
|
| 9 |
import numpy as np
|
| 10 |
+
import os
|
| 11 |
+
import shutil
|
| 12 |
|
| 13 |
try:
|
| 14 |
import kaleido
|
| 15 |
except ImportError:
|
| 16 |
st.error("請安裝 'kaleido' 套件以啟用圖像導出功能:\n\n $ pip install kaleido")
|
| 17 |
|
| 18 |
+
# 自動下載 orca
|
| 19 |
+
if not os.path.exists("/usr/local/bin/orca"):
|
| 20 |
+
if not os.path.exists("orca.tar.gz"):
|
| 21 |
+
os.system("wget https://github.com/plotly/orca/releases/download/v1.3.1/orca-1.3.1-x86_64.tar.gz")
|
| 22 |
+
os.system("tar xzf orca.tar.gz")
|
| 23 |
+
shutil.move("orca-1.3.1-x86_64/orca","/usr/local/bin/orca")
|
| 24 |
+
os.chmod("/usr/local/bin/orca", 0o755)
|
| 25 |
+
|
| 26 |
def load_data(uploaded_file):
|
| 27 |
"""載入並處理CSV檔案"""
|
| 28 |
try:
|
|
|
|
| 92 |
output_dir = "radar_charts"
|
| 93 |
os.makedirs(output_dir, exist_ok=True)
|
| 94 |
|
|
|
|
| 95 |
for student in df['姓名']:
|
| 96 |
if student in comparison_rows:
|
| 97 |
continue
|
|
|
|
| 103 |
composite = Image.alpha_composite(background, img)
|
| 104 |
draw = ImageDraw.Draw(composite)
|
| 105 |
|
| 106 |
+
# 手動繪製圖表標籤文字
|
| 107 |
for trace in fig.data:
|
| 108 |
if isinstance(trace, go.Scatterpolar):
|
| 109 |
for i, theta in enumerate(trace.theta):
|
| 110 |
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))
|
| 111 |
+
draw.text((img.width / 2 + x, img.height / 2 + y - 25), str(trace.name), fill=(0, 0, 0))
|
| 112 |
file_path = os.path.join(output_dir, f"{student}_comparison.png")
|
| 113 |
composite.save(file_path)
|
| 114 |
|
|
|
|
| 154 |
else:
|
| 155 |
zip_path = save_all_radar_charts(df, selected_columns, comparison_rows)
|
| 156 |
if zip_path:
|
| 157 |
+
with open(zip_path, "rb") as f:
|
| 158 |
st.download_button(
|
| 159 |
label="下載壓縮檔",
|
| 160 |
data=f,
|
|
|
|
| 164 |
on_click=lambda: st.session_state.update({'download_triggered':True}),
|
| 165 |
disabled=st.session_state.get("download_triggered",False)
|
| 166 |
)
|
| 167 |
+
st.session_state.download_triggered = False
|
| 168 |
|
| 169 |
if __name__ == "__main__":
|
| 170 |
if 'download_triggered' not in st.session_state:
|