Update app.py
Browse files
app.py
CHANGED
|
@@ -17,16 +17,28 @@ import os
|
|
| 17 |
|
| 18 |
def set_chinese_font():
|
| 19 |
"""
|
| 20 |
-
設定全域 Matplotlib
|
| 21 |
"""
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
# 頁面設定
|