DEV Community

artydev
artydev

Posted on

Using PyWebio with Flask an Pywebview

Here is a nice gist :

import webview
from pywebio.platform.flask import webio_view
from flask import Flask, request
from pywebio.input import *
from pywebio.output import *
from pywebio.session import *

app = Flask(__name__)

@app.route('/')
def hello():
    name = request.args.get("name", "World")
    msg = f'Hello, {(name)}!'
    msg = msg + f'<br/><a href="/tool">View Tool</a>'
    return msg

def bmi():
    height = input("Input your height(cm):", type=FLOAT)
    weight = input("Input your weight(kg):", type=FLOAT)

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

    top_status = [(16, 'Severely underweight'), (18.5, 'Underweight'),
                  (25, 'Normal'), (30, 'Overweight'),
                  (35, 'Moderately obese'), (float('inf'), 'Severely obese')]

    put_button("Reload Page", onclick=lambda: run_js('window.location.reload()'))
    for top, status in top_status:

        if BMI <= top:
            put_text('Your BMI: %.1f. Category: %s' % (BMI, status))
            break

def start_flask_server():
    app.add_url_rule('/tool', 'webio_view', webio_view(bmi), methods=['GET', 'POST', 'OPTIONS'])
    app.run(host='0.0.0.0', port=8888)

if __name__ == '__main__':
    app.add_url_rule('/tool', 'webio_view', webio_view(bmi), methods=['GET', 'POST', 'OPTIONS'])
    webview.create_window('Flask example', app)
    webview.start(gui='edge')
Enter fullscreen mode Exit fullscreen mode

Top comments (0)