ciyao commited on
Commit
e14f0f2
·
verified ·
1 Parent(s): e32cdb6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -9
app.py CHANGED
@@ -17,16 +17,28 @@ import os
17
 
18
  def set_chinese_font():
19
  """
20
- 設定全域 Matplotlib 中文字型為 SimHei,適用非 Windows 環境。
21
  """
22
- font_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static', 'fonts', 'SimHei.ttf')
23
- if not os.path.isfile(font_path):
24
- st.warning(f"找不到中文字型檔案: {font_path}")
25
- return
26
-
27
- font_prop = fm.FontProperties(fname=font_path)
28
- plt.rcParams['font.family'] = font_prop.get_name()
29
- plt.rcParams['axes.unicode_minus'] = False
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
 
32
  # 頁面設定
 
17
 
18
  def set_chinese_font():
19
  """
20
+ 設定全域 Matplotlib 中文字型,強制指定 SimHei,避免亂碼。
21
  """
22
+ try:
23
+ font_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static', 'fonts', 'SimHei.ttf')
24
+ if not os.path.exists(font_path):
25
+ st.warning(f"未找到字型檔案:{font_path}")
26
+ return
27
+
28
+ # 加入字型
29
+ fm.fontManager.addfont(font_path)
30
+ font_prop = fm.FontProperties(fname=font_path)
31
+ font_name = font_prop.get_name()
32
+
33
+ # 明確設定 sans-serif 和 family
34
+ plt.rcParams['font.family'] = font_name
35
+ plt.rcParams['font.sans-serif'] = [font_name] # 這行非常關鍵
36
+ plt.rcParams['axes.unicode_minus'] = False
37
+
38
+ st.success(f"已套用中文字型: {font_name}")
39
+
40
+ except Exception as e:
41
+ st.error(f"設定中文字型失敗: {e}")
42
 
43
 
44
  # 頁面設定