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

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay