Lashtw commited on
Commit
ee2e423
·
verified ·
1 Parent(s): 8d8bfbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -24
app.py CHANGED
@@ -112,7 +112,6 @@ def save_radar_chart_image(fig):
112
  """使用 kaleido 輸出 png 的記憶體檔案"""
113
  img_bytes = pio.to_image(fig, format="png", engine="kaleido")
114
  return img_bytes
115
-
116
  def create_composite_image(fig, student):
117
  """使用 PIL 合成圖片,確保學生的成績在最上層"""
118
  img_bytes = pio.to_image(fig, format="png", engine="kaleido")
@@ -144,30 +143,41 @@ def main():
144
  st.plotly_chart(fig, use_container_width=True)
145
  except Exception as e:
146
  st.error(f"生成雷達圖時發生錯誤:{e}")
147
-
148
- st.write("### 批次繪製個別學生比較圖")
149
-
150
- individual_students = st.multiselect("選擇要個別比較的學生", df['姓名'].tolist(), key = "student")
151
- comparison_items = st.multiselect("選擇要比較的項目", df['姓名'].tolist(), key = "item")
152
-
153
- if individual_students and comparison_items:
154
-
155
- image_options = []
156
- image_bytes = {}
157
- for student in individual_students:
158
- fig = create_radar_chart(df, [student] + comparison_items, selected_columns)
159
- image_bytes[student] = create_composite_image(fig,student)
160
- image_options.append(f"{student} 與 {', '.join(comparison_items)} 的比較")
161
-
162
- selected_image_options = st.multiselect("選擇要顯示的圖片", options=image_options)
163
-
164
- cols = st.columns(3) # 排成三列
165
- for i, option in enumerate(selected_image_options):
166
- student = option.split(" 與 ")[0]
167
- with cols[i%3]:
168
- st.image(image_bytes[student], use_container_width=True)
169
- st.text(option)
170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  if __name__ == "__main__":
172
  import numpy as np
173
  main()
 
112
  """使用 kaleido 輸出 png 的記憶體檔案"""
113
  img_bytes = pio.to_image(fig, format="png", engine="kaleido")
114
  return img_bytes
 
115
  def create_composite_image(fig, student):
116
  """使用 PIL 合成圖片,確保學生的成績在最上層"""
117
  img_bytes = pio.to_image(fig, format="png", engine="kaleido")
 
143
  st.plotly_chart(fig, use_container_width=True)
144
  except Exception as e:
145
  st.error(f"生成雷達圖時發生錯誤:{e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
 
147
+ st.write("### 批次下載全班學生雷達圖")
148
+
149
+ comparison_item = st.selectbox("選擇要比較的項目", df['姓名'].tolist())
150
+
151
+ if comparison_item:
152
+ image_bytes = {}
153
+ for student in df['姓名']:
154
+ if student == comparison_item:
155
+ continue
156
+
157
+ fig = create_radar_chart(df, [student, comparison_item], selected_columns)
158
+ fig = apply_font_to_all_text(fig)
159
+ image_bytes[student] = create_composite_image(fig, student)
160
+
161
+ cols = st.columns(3) # 排成三列
162
+ for i, student in enumerate(image_bytes.keys()):
163
+ with cols[i%3]:
164
+ st.image(image_bytes[student], use_container_width=True)
165
+ st.text(f"{student} 與 {comparison_item} 的比較")
166
+
167
+ if st.button("下載全班雷達圖"):
168
+ zip_path = "all_radar_charts.zip"
169
+ with ZipFile(zip_path, 'w') as zipf:
170
+ for student, img in image_bytes.items():
171
+ img_bytes = BytesIO()
172
+ img.save(img_bytes, format="png")
173
+ zipf.writestr(f"{student}_vs_{comparison_item}.png", img_bytes.getvalue())
174
+ with open(zip_path, "rb") as f:
175
+ st.download_button(
176
+ label="下載全班雷達圖",
177
+ data=f,
178
+ file_name="all_radar_charts.zip",
179
+ mime="application/zip"
180
+ )
181
  if __name__ == "__main__":
182
  import numpy as np
183
  main()