交互式 Web 应用-PyWebIO

参考
https://mp.weixin.qq.com/s/w4geqzmuqAXvE78257jQ2Q#

pip install pywebio

bmi.py

from pywebio import start_server
from pywebio.input import input, FLOAT
from pywebio.output import put_text

def bmi():
    height = input("请输入你的身高(cm):", type=FLOAT)
    weight = input("请输入你的体重(kg):", type=FLOAT)

    BMI = weight / (height / 100) ** 2

    top_status = [(14.9, '极瘦'), (18.4, '偏瘦'),
                  (22.9, '正常'), (27.5, '过重'),
                  (40.0, '肥胖'), (float('inf'), '非常肥胖')]

    for top, status in top_status:
        if BMI <= top:
            put_text('你的 BMI 值: %.1f,身体状态:%s' % (BMI, status))
            break

if __name__ == '__main__':
    start_server(bmi, port=80)

webio_tornado.py

import tornado.ioloop
import tornado.web
from pywebio.platform.tornado import webio_handler
import bmi

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

if __name__ == "__main__":
    application = tornado.web.Application([
        (r"/", MainHandler),
        (r"/bmi", webio_handler(bmi.bmi())),  # bmi 即为上文计算BMI指数的函数
    ])
    application.listen(port=80, address='localhost')
    tornado.ioloop.IOLoop.current().start()
上一篇:Python 炫技操作:海象运算符的三种用法


下一篇:判断身材状态