pywebview
装包
pip install pywebview
官文
https://pywebview.flowrl.com/guide/interdomain.html#invoke-python-from-javascript
案例
https://pywebview.flowrl.com/examples/fullscreen.html
实操
import webview
from flask import Flask, render_template, jsonify, request
import json
from functools import wraps
app = Flask(__name__, template_folder='./static', static_folder='./static', static_url_path='')
def verify_token(function):
@wraps(function)
def wrapper(*args, **kwargs):
data = json.loads(request.data)
token = data.get('token')
if token == webview.token:
return function(*args, **kwargs)
else:
raise Exception('Authentication error')
return wrapper
@app.route('/')
def index():
return render_template('index.html')
@app.route('/backstage/api/login/', methods=['POST'])
def login():
print(request.data)
data = json.loads(request.data)
user = data.get('username')
pwd = data.get('password')
if user != 'test' or pwd != 'test':
print({'code': '4013', 'msg': '用户名或密码错误'}, jsonify({'code': '4013', 'msg': '用户名或密码错误'}))
return jsonify({'code': '4013', 'msg': '用户名或密码错误'})
groups = {"首页": [], "业务菜单": ["3D模型", "画图展示", "业务3"], "系统设置": ["用户管理", "系统日志"]}
roles = {"首页": ["读"], "3D模型": ["读", "写"], "业务2": ["读", "写"], "业务3": ["读", "写"],
"用户管理": ["读", "写"], "系统日志": ["读", "写"]}
return jsonify({'code': '0', 'data': {'groups': groups, 'roles': roles}, 'msg': 'ok'})
@app.route('/get_usr_info', methods=['GET'])
@verify_token
def get_usr_info():
return jsonify({'code': '0', 'data': []})
if __name__ == '__main__':
chinese = {
'global.quitConfirmation': u'确定关闭?',
}
window = webview.create_window(
title='云收单',
url=app,
width=900,
height=620,
# frameless=True,
# easy_drag=True,
# hidden=True,
transparent=True,
)
webview.start(localization=chinese, debug=True, http_server=True)
flaskwebgui
装包: pip install flaskwebgui github:
https://github.com/ClimenteA/flaskwebgui/tree/master/examples/
实例:
from flask import Flask
from flask import render_template
from flaskwebgui import FlaskUI
app = Flask(__name__, static_folder='./static', template_folder='./static', static_url_path='')
ui = FlaskUI(app, width=500, height=500)
@app.route("/")
def hello():
return render_template('index.html')
@app.route("/home", methods=['GET'])
def home():
return render_template('index.html')
if __name__ == "__main__":
ui.run()
结构:
|