Skip to content

Instantly share code, notes, and snippets.

@mkeneqa
Created April 20, 2021 04:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mkeneqa/3abac79204e903c71d1483674aed0a3f to your computer and use it in GitHub Desktop.
Save mkeneqa/3abac79204e903c71d1483674aed0a3f to your computer and use it in GitHub Desktop.
sample starter code for using pywebio in pywebview with flask server on Windows
import webview
import pywebio
from pywebio.platform.flask import webio_view
from flask import Flask, escape, request, send_from_directory, current_app, url_for
from pywebio import STATIC_PATH
from pywebio.input import *
from pywebio.output import *
from pywebio.session import *
import logging
from datetime import date, timedelta
from functools import partial
import time
from pywebio.session import info as session_info
import threading
app = Flask(__name__)
@app.route('/')
def hello():
name = request.args.get("name", "World")
msg = f'Hello, {escape(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')]
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__':
# t = threading.Thread(target=start_flask_server)
# t.daemon = True
# t.start()
# webview.create_window('CEF Example', 'http://localhost:8888')
# webview.start(gui='cef')
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='cef')
@wanghaisheng
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment