bmi.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from pywebio import start_server
  2. from pywebio.input import *
  3. from pywebio.output import *
  4. from pywebio.session import info as session_info
  5. def t(eng, chinese):
  6. """return English or Chinese text according to the user's browser language"""
  7. return chinese if 'zh' in session_info.user_language else eng
  8. def main():
  9. """BMI Calculation
  10. Simple application for calculating Body Mass Index.
  11. 计算BMI指数的简单应用
  12. """
  13. put_markdown(t("""# Body Mass Index
  14. [Body mass index](https://en.wikipedia.org/wiki/Body_mass_index) (BMI) is a measure of body fat based on height and weight that applies to adult men and women.
  15. BMI Categories:
  16. | Category | BMI |
  17. | -------------------- | ------------- |
  18. | Severely underweight | BMI<14.9 |
  19. | Underweight | 14.9≤BMI<18.4 |
  20. | Normal | 18.4≤BMI<22.9 |
  21. | Overweight | 22.9≤BMI<27.5 |
  22. | Moderately obese | 27.5≤BMI<40 |
  23. | Severely obese | BMI≥40 |
  24. ## BMI calculation
  25. The source code of this application is [here](https://github.com/wang0618/PyWebIO/blob/dev/demos/bmi.py)
  26. """, """# BMI指数
  27. [`BMI指数`](https://baike.baidu.com/item/%E4%BD%93%E8%B4%A8%E6%8C%87%E6%95%B0/1455733)(Body Mass Index,BMI),是用体重千克数除以身高米数的平方得出的数字,是国际上常用的衡量人体胖瘦程度以及是否健康的一个标准。
  28. 成年人的BMI值处于以下阶段
  29. | 体形分类 | BMI值范围 |
  30. | ------ | -------- |
  31. | 极瘦 | BMI<14.9 |
  32. | 偏瘦 | 14.9≤BMI<18.4 |
  33. | 正常 | 18.4≤BMI<22.9 |
  34. | 过重 | 22.9≤BMI<27.5 |
  35. | 肥胖 | 27.5≤BMI<40 |
  36. | 非常肥胖 | BMI≥40 |
  37. ## BMI指数计算器
  38. 本程序的源代码[链接](https://github.com/wang0618/PyWebIO/blob/dev/demos/bmi.py)
  39. """))
  40. info = input_group(t('BMI calculation', '计算BMI:'), [
  41. input(t("Your Height(cm)", "请输入你的身高(cm)"), name="height", type=FLOAT),
  42. input(t("Your Weight(kg)", "请输入你的体重(kg)"), name="weight", type=FLOAT),
  43. ])
  44. BMI = info['weight'] / (info['height'] / 100) ** 2
  45. top_status = [(14.9, t('Severely underweight', '极瘦')), (18.4, t('Underweight', '偏瘦')),
  46. (22.9, t('Normal', '正常')), (27.5, t('Overweight', '过重')),
  47. (40.0, t('Moderately obese', '肥胖')), (float('inf'), t('Severely obese', '非常肥胖'))]
  48. for top, status in top_status:
  49. if BMI <= top:
  50. put_markdown(t('Your BMI: `%.1f`, Category: `%s`', '你的 BMI 值: `%.1f`,身体状态: `%s`') % (BMI, status))
  51. break
  52. if __name__ == '__main__':
  53. start_server(main, debug=True, port=8080)