bmi.py 3.1 KB

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