Lashtw commited on
Commit
debc832
·
verified ·
1 Parent(s): 6777840

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -16
app.py CHANGED
@@ -3,18 +3,12 @@ import pandas as pd
3
  import plotly.graph_objects as go
4
  import os
5
  from zipfile import ZipFile
6
-
7
  try:
8
  import kaleido
9
- import plotly.io as pio
10
  except ImportError:
11
  st.error("請安裝 'kaleido' 套件以啟用圖像導出功能:\n\n $ pip install kaleido")
12
 
13
- # set kaleido config if installed, to try and fix the chinese font issue
14
- try:
15
- pio.kaleido.scope.default_font = "Microsoft JhengHei, Noto Sans CJK, Arial"
16
- except:
17
- st.error("字型設定錯誤,請確定是否安裝 kaleido")
18
 
19
 
20
  def load_data(uploaded_file):
@@ -92,18 +86,17 @@ def save_all_radar_charts(df, selected_columns, comparison_rows):
92
  for student in df['姓名']:
93
  if student in comparison_rows:
94
  continue
95
-
96
- fig = create_radar_chart(df, [student] + comparison_rows, selected_columns)
97
 
98
- # 在這裡更新字型,確保字型有被設定
99
- fig.update_layout(
100
- font=dict(
101
- family="Microsoft JhengHei, Noto Sans CJK, Arial"
102
- )
103
- )
104
 
 
 
 
 
 
105
  file_path = os.path.join(output_dir, f"{student}_comparison.png")
106
- fig.write_image(file_path, engine="kaleido")
 
107
 
108
  # 壓縮所有圖表
109
  zip_path = "radar_charts.zip"
 
3
  import plotly.graph_objects as go
4
  import os
5
  from zipfile import ZipFile
6
+ import plotly.io as pio
7
  try:
8
  import kaleido
 
9
  except ImportError:
10
  st.error("請安裝 'kaleido' 套件以啟用圖像導出功能:\n\n $ pip install kaleido")
11
 
 
 
 
 
 
12
 
13
 
14
  def load_data(uploaded_file):
 
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
 
92
+ # 這裡使用 plotly.io.to_image 直接生成圖片
93
+ img_bytes = pio.to_image(fig, format="png",
94
+ engine="kaleido",
95
+ font_family="Microsoft JhengHei, Noto Sans CJK, Arial"
96
+ )
97
  file_path = os.path.join(output_dir, f"{student}_comparison.png")
98
+ with open(file_path, "wb") as f:
99
+ f.write(img_bytes)
100
 
101
  # 壓縮所有圖表
102
  zip_path = "radar_charts.zip"