Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,42 +23,46 @@ if not os.path.exists("/usr/local/bin/orca"):
|
|
| 23 |
if not os.path.exists("orca.tar.gz"):
|
| 24 |
try:
|
| 25 |
st.write("Downloading orca...")
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
response.raise_for_status() # 檢查下載是否成功
|
| 28 |
with open("orca.tar.gz", "wb") as f:
|
| 29 |
-
|
| 30 |
-
|
| 31 |
st.write("Orca download completed.")
|
| 32 |
except requests.exceptions.RequestException as e:
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
try:
|
| 37 |
-
|
| 38 |
st.write("Orca file does not exist, redownloading")
|
| 39 |
-
response = requests.get(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
response.raise_for_status()
|
| 41 |
with open("orca.tar.gz", "wb") as f:
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
if file_size < 1000000: # 任意設定一個最小檔案大小,防止網路錯誤檔案下載過小
|
| 50 |
-
st.error("Orca file size is too small, try to redownload")
|
| 51 |
-
os.remove("orca.tar.gz") #刪除壞掉的檔案
|
| 52 |
-
response = requests.get("https://github.com/plotly/orca/releases/download/v1.3.1/orca-1.3.1-x86_64.tar.gz", stream=True)
|
| 53 |
-
response.raise_for_status()
|
| 54 |
-
with open("orca.tar.gz", "wb") as f:
|
| 55 |
-
for chunk in response.iter_content(chunk_size=8192):
|
| 56 |
-
f.write(chunk)
|
| 57 |
-
|
| 58 |
-
st.write("Extracting orca...")
|
| 59 |
-
with tarfile.open("orca.tar.gz", "r:gz") as tar:
|
| 60 |
-
tar.extractall()
|
| 61 |
-
st.write("Orca extraction completed.")
|
| 62 |
except Exception as e:
|
| 63 |
st.error(f"Orca extraction failed: {e}")
|
| 64 |
st.stop()
|
|
@@ -94,7 +98,6 @@ if not os.path.exists("/usr/local/bin/orca"):
|
|
| 94 |
st.stop()
|
| 95 |
st.write("Orca installation completed.")
|
| 96 |
|
| 97 |
-
|
| 98 |
def load_data(uploaded_file):
|
| 99 |
"""載入並處理CSV檔案"""
|
| 100 |
try:
|
|
@@ -164,13 +167,12 @@ def save_all_radar_charts(df, selected_columns, comparison_rows):
|
|
| 164 |
output_dir = "radar_charts"
|
| 165 |
os.makedirs(output_dir, exist_ok=True)
|
| 166 |
|
| 167 |
-
|
| 168 |
for student in df['姓名']:
|
| 169 |
if student in comparison_rows:
|
| 170 |
continue
|
| 171 |
|
| 172 |
fig = create_radar_chart(df, [student] + comparison_rows, selected_columns)
|
| 173 |
-
img_bytes = pio.to_image(fig, format="png", engine="orca")
|
| 174 |
img = Image.open(BytesIO(img_bytes)).convert("RGBA")
|
| 175 |
background = Image.new('RGBA', img.size, (255, 255, 255, 255))
|
| 176 |
composite = Image.alpha_composite(background, img)
|
|
@@ -181,7 +183,7 @@ def save_all_radar_charts(df, selected_columns, comparison_rows):
|
|
| 181 |
if isinstance(trace, go.Scatterpolar):
|
| 182 |
for i, theta in enumerate(trace.theta):
|
| 183 |
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))
|
| 184 |
-
draw.text((img.width / 2 + x, img.height / 2 + y - 25), str(trace.name), fill=(0, 0, 0))
|
| 185 |
file_path = os.path.join(output_dir, f"{student}_comparison.png")
|
| 186 |
composite.save(file_path)
|
| 187 |
|
|
|
|
| 23 |
if not os.path.exists("orca.tar.gz"):
|
| 24 |
try:
|
| 25 |
st.write("Downloading orca...")
|
| 26 |
+
|
| 27 |
+
# 使用最新的 orca 下載連結
|
| 28 |
+
orca_url = "https://github.com/plotly/orca/releases/download/v1.3.1/orca-1.3.1-x86_64.tar.gz"
|
| 29 |
+
|
| 30 |
+
response = requests.get(orca_url, stream=True)
|
| 31 |
response.raise_for_status() # 檢查下載是否成功
|
| 32 |
with open("orca.tar.gz", "wb") as f:
|
| 33 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 34 |
+
f.write(chunk)
|
| 35 |
st.write("Orca download completed.")
|
| 36 |
except requests.exceptions.RequestException as e:
|
| 37 |
+
st.error(f"Orca download failed: {e}")
|
| 38 |
+
st.stop()
|
| 39 |
|
| 40 |
try:
|
| 41 |
+
if not os.path.exists("orca.tar.gz"):
|
| 42 |
st.write("Orca file does not exist, redownloading")
|
| 43 |
+
response = requests.get(orca_url, stream=True)
|
| 44 |
+
response.raise_for_status()
|
| 45 |
+
with open("orca.tar.gz", "wb") as f:
|
| 46 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 47 |
+
f.write(chunk)
|
| 48 |
+
|
| 49 |
+
st.write("Checking orca.tar.gz file size...")
|
| 50 |
+
file_size = os.path.getsize("orca.tar.gz")
|
| 51 |
+
st.write(f"Orca file size is {file_size} bytes")
|
| 52 |
+
|
| 53 |
+
if file_size < 1000000: # 任意設定一個最小檔案大小,防止網路錯誤檔案下載過小
|
| 54 |
+
st.error("Orca file size is too small, try to redownload")
|
| 55 |
+
os.remove("orca.tar.gz") #刪除壞掉的檔案
|
| 56 |
+
response = requests.get(orca_url, stream=True)
|
| 57 |
response.raise_for_status()
|
| 58 |
with open("orca.tar.gz", "wb") as f:
|
| 59 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 60 |
+
f.write(chunk)
|
| 61 |
+
|
| 62 |
+
st.write("Extracting orca...")
|
| 63 |
+
with tarfile.open("orca.tar.gz", "r:gz") as tar:
|
| 64 |
+
tar.extractall()
|
| 65 |
+
st.write("Orca extraction completed.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
except Exception as e:
|
| 67 |
st.error(f"Orca extraction failed: {e}")
|
| 68 |
st.stop()
|
|
|
|
| 98 |
st.stop()
|
| 99 |
st.write("Orca installation completed.")
|
| 100 |
|
|
|
|
| 101 |
def load_data(uploaded_file):
|
| 102 |
"""載入並處理CSV檔案"""
|
| 103 |
try:
|
|
|
|
| 167 |
output_dir = "radar_charts"
|
| 168 |
os.makedirs(output_dir, exist_ok=True)
|
| 169 |
|
|
|
|
| 170 |
for student in df['姓名']:
|
| 171 |
if student in comparison_rows:
|
| 172 |
continue
|
| 173 |
|
| 174 |
fig = create_radar_chart(df, [student] + comparison_rows, selected_columns)
|
| 175 |
+
img_bytes = pio.to_image(fig, format="png", engine="orca") # 使用 orca
|
| 176 |
img = Image.open(BytesIO(img_bytes)).convert("RGBA")
|
| 177 |
background = Image.new('RGBA', img.size, (255, 255, 255, 255))
|
| 178 |
composite = Image.alpha_composite(background, img)
|
|
|
|
| 183 |
if isinstance(trace, go.Scatterpolar):
|
| 184 |
for i, theta in enumerate(trace.theta):
|
| 185 |
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))
|
| 186 |
+
draw.text((img.width / 2 + x, img.height / 2 + y - 25), str(trace.name), fill=(0, 0, 0)) # 直接使用PIL預設字型
|
| 187 |
file_path = os.path.join(output_dir, f"{student}_comparison.png")
|
| 188 |
composite.save(file_path)
|
| 189 |
|