Views
No views yet
# 載入字體
if os.path.exists(font_filename):
return fm.FontProperties(fname=font_filename)
return None# 計算 BMR
bmr = (10 * weight + 6.25 * height - 5 * age + 5) if gender == "男" else (10 * weight + 6.25 * height - 5 * age - 161)
# 計算 TDEE
factors = {
"久坐 (幾乎不運動)": 1.2, "輕度 (每週運動 1-3 天)": 1.375,
"中度 (每週運動 3-5 天)": 1.55, "高度 (每週運動 6-7 天)": 1.725,
"極高度 (高強度訓練/勞力工作)": 1.9
}
act_factor = factors.get(activity_level, 1.2)
tdee_base = bmr * act_factor
daily_activity_cal = tdee_base - bmr
# 計算運動消耗
cal_walk = 3.5 * weight * (t_walk / 60)
cal_weight = 6.0 * weight * (t_weight / 60)
cal_cardio = 7.0 * weight * (t_cardio / 60)
cal_hiit = 12.0 * weight * (t_hiit / 60)
total_exercise_cal = cal_walk + cal_weight + cal_cardio + cal_hiit
total = tdee_base + total_exercise_cal
# --- 繪圖 ---
labels = ['基礎代謝 (BMR)', '日常活動消耗', '今日運動消耗']
sizes = [bmr, daily_activity_cal, total_exercise_cal]
colors = ['#a8dadc', '#457b9d', '#e63946']
explode = (0, 0, 0.1)
fig, ax = plt.subplots(figsize=(6, 4))
valid_data = [(l, s, c, e) for l, s, c, e in zip(labels, sizes, colors, explode) if s > 0]
if valid_data:
v_labels, v_sizes, v_colors, v_explode = zip(*valid_data)
wedges, texts, autotexts = ax.pie(v_sizes, explode=v_explode, labels=v_labels, colors=v_colors,
autopct='%1.1f%%', shadow=True, startangle=140)
if my_font:
for text in texts + autotexts:
text.set_fontproperties(my_font)
ax.set_title(f"今日熱量消耗 (總計 {total:.0f} kcal)", fontproperties=my_font, fontsize=15)
else:
ax.set_title(f"Total: {total:.0f} kcal", fontsize=15)
# --- HTML 報告 ---
ex_details = ""
if total_exercise_cal > 0:
ex_details += "<ul style='margin-top:5px; padding-left:20px; color:#555;'>"
if t_walk > 0: ex_details += f"<li>散步: {cal_walk:.0f} kcal</li>"
if t_weight > 0: ex_details += f"<li>重訓: {cal_weight:.0f} kcal</li>"
if t_cardio > 0: ex_details += f"<li>有氧: {cal_cardio:.0f} kcal</li>"
if t_hiit > 0: ex_details += f"<li>高強度: {cal_hiit:.0f} kcal</li>"
ex_details += "</ul>"
else:
ex_details = "<span style='color:#888; font-size:0.9em;'>(無額外運動)</span>"
output_html = f"""
<div style='background-color: #ffffff; padding: 20px; border-radius: 15px; border: 1px solid #eee;'>
<h3 style='color: #1d3557; margin: 0 0 10px 0;'>📋 數據明細</h3>
<p>🔥 基礎代謝: <b>{bmr:.0f}</b></p>
<p>🏠 日常消耗: <b>{daily_activity_cal:.0f}</b> (不含運動)</p>
<p>💪 運動消耗: <b style='color:#e63946;'>{total_exercise_cal:.0f}</b></p>
{ex_details}
<hr>
<div style='text-align:center; color:#e63946; font-weight:bold; font-size:1.5em;'>
總計: {total:.0f} kcal
</div>
</div>
"""
return output_html, figbtn.click(nutrition_calculator, [w_input, h_input, a_input, g_input, act_input, t_walk, t_weight, t_cardio, t_hiit], [out_html, out_plot])