Lashtw commited on
Commit
aebff32
·
verified ·
1 Parent(s): dbc904d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -23
app.py CHANGED
@@ -103,11 +103,11 @@ def apply_font_to_all_text(fig):
103
 
104
  return fig
105
 
106
-
107
  def save_radar_chart_image(fig):
108
- """使用 kaleido 輸出 png 的記憶體檔案"""
109
- img_bytes = pio.to_image(fig, format="png", engine="kaleido")
110
- return img_bytes
 
111
  def main():
112
  st.title('學生成績雷達圖產生器')
113
 
@@ -140,13 +140,18 @@ def main():
140
 
141
  # 初始化選取狀態
142
  if 'selected_images' not in st.session_state:
143
- st.session_state.selected_images = {student: False for student in df['姓名'] if student not in comparison_rows}
144
-
 
 
 
 
 
145
  # 建立圖片核取方塊
146
  image_bytes = {}
147
  for student in df['姓名']:
148
  if student in comparison_rows:
149
- continue
150
 
151
  fig = create_radar_chart(df, [student] + comparison_rows, selected_columns)
152
  fig = apply_font_to_all_text(fig)
@@ -156,27 +161,33 @@ def main():
156
  cols = st.columns(3) # 排成三列
157
  for i, student in enumerate(image_bytes.keys()):
158
  with cols[i % 3]:
159
- st.image(image_bytes[student], use_container_width=True)
160
-
161
- checkbox_id = f"checkbox-{student}"
162
- st.markdown(
 
 
 
 
 
163
  f"""
164
  <div style="display: flex; align-items: center;">
165
- <input type="checkbox" id="{checkbox_id}" {"checked" if st.session_state.selected_images[student] else ""}>
166
  <label for="{checkbox_id}" style="font-family: 'Microsoft JhengHei', 'Noto Sans CJK', Arial; font-size: 16px; margin-left: 5px;">下載 {student}</label>
167
  </div>
168
- """,unsafe_allow_html=True
169
  )
170
 
171
- # 更新選取狀態
172
- if st.session_state.get(f"checkbox-{student}_clicked"):
173
- st.session_state.selected_images[student] = not st.session_state.selected_images[student]
174
- st.session_state[f"checkbox-{student}_clicked"] = False
 
175
 
176
- if st.checkbox("",key=f"checkbox-{student}_clicked",label_visibility="hidden"):
177
- pass
178
 
179
-
 
 
180
  #全部選取和全部不選
181
  if st.button("全部選取"):
182
  for student in st.session_state.selected_images:
@@ -190,14 +201,14 @@ def main():
190
  if st.button("下載選取圖片"):
191
  selected_image_bytes = []
192
  for student, selected in st.session_state.selected_images.items():
193
- if selected:
194
- selected_image_bytes.append((student,image_bytes[student]))
195
 
196
  if selected_image_bytes:
197
  zip_path = "selected_radar_charts.zip"
198
  with ZipFile(zip_path, 'w') as zipf:
199
  for student, img_bytes in selected_image_bytes:
200
- zipf.writestr(f"{student}_comparison.png", img_bytes)
201
  with open(zip_path, "rb") as f:
202
  st.download_button(
203
  label="下載選取圖片",
 
103
 
104
  return fig
105
 
 
106
  def save_radar_chart_image(fig):
107
+ """使用 kaleido 輸出 png 的記憶體檔案"""
108
+ img_bytes = pio.to_image(fig, format="png", engine="kaleido")
109
+ return img_bytes
110
+
111
  def main():
112
  st.title('學生成績雷達圖產生器')
113
 
 
140
 
141
  # 初始化選取狀態
142
  if 'selected_images' not in st.session_state:
143
+ st.session_state.selected_images = {}
144
+ for student in df['姓名']:
145
+ if student not in comparison_rows:
146
+ if student not in st.session_state.selected_images:
147
+ st.session_state.selected_images[student] = False
148
+
149
+
150
  # 建立圖片核取方塊
151
  image_bytes = {}
152
  for student in df['姓名']:
153
  if student in comparison_rows:
154
+ continue
155
 
156
  fig = create_radar_chart(df, [student] + comparison_rows, selected_columns)
157
  fig = apply_font_to_all_text(fig)
 
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
+
166
+ checkbox_id = f"checkbox-{student}"
167
+
168
+ # 更新選取狀態
169
+ def update_selection(student):
170
+ st.session_state.selected_images[student] = not st.session_state.selected_images[student]
171
+
172
+ st.markdown(
173
  f"""
174
  <div style="display: flex; align-items: center;">
175
+ <input type="checkbox" id="{checkbox_id}" {"checked" if st.session_state.selected_images[student] else ""} onclick="this.dispatchEvent(new CustomEvent('checkbox-clicked',{{"detail": '{student}'}}))">
176
  <label for="{checkbox_id}" style="font-family: 'Microsoft JhengHei', 'Noto Sans CJK', Arial; font-size: 16px; margin-left: 5px;">下載 {student}</label>
177
  </div>
178
+ """, unsafe_allow_html=True
179
  )
180
 
181
+ st.markdown(f"""<script>
182
+ document.getElementById('{checkbox_id}').addEventListener('checkbox-clicked', function(event) {{
183
+ window.dispatchEvent(new CustomEvent('checkbox-clicked-streamlit', {{detail: event.detail}}));
184
+ }});
185
+ </script>""", unsafe_allow_html=True)
186
 
 
 
187
 
188
+ if st.session_state.get('checkbox-clicked-streamlit', {}).get('detail') == student:
189
+ update_selection(student)
190
+
191
  #全部選取和全部不選
192
  if st.button("全部選取"):
193
  for student in st.session_state.selected_images:
 
201
  if st.button("下載選取圖片"):
202
  selected_image_bytes = []
203
  for student, selected in st.session_state.selected_images.items():
204
+ if selected:
205
+ selected_image_bytes.append((student,image_bytes[student]))
206
 
207
  if selected_image_bytes:
208
  zip_path = "selected_radar_charts.zip"
209
  with ZipFile(zip_path, 'w') as zipf:
210
  for student, img_bytes in selected_image_bytes:
211
+ zipf.writestr(f"{student}_comparison.png", img_bytes)
212
  with open(zip_path, "rb") as f:
213
  st.download_button(
214
  label="下載選取圖片",