首页 > 解决方案 > 如何同时运行 python flask 应用程序和 webview

问题描述

我正在尝试同时运行烧瓶应用程序和 webview,但似乎只有烧瓶应用程序首先运行并阻止 webview 打开它。

if __name__ == '__main__':
    os.system('python app.py')
    webview.create_window('Hello world', 'http://127.0.0.1:5000/')
    webview.start()

只有在我关闭烧瓶服务器(Ctr+C)后才会启动 Webview 命令,但直到那时 webview 返回连接被拒绝。

标签: pythonflaskwebviewpython-os

解决方案


您需要在后台运行烧瓶应用程序。这是一个示例工作示例。os.system('python app.py &')

主文件

import webview
import os

if __name__ == '__main__':
    os.system('python app.py &')
    webview.create_window('Hello world', 'http://127.0.0.1:5000/')
    webview.start()

应用程序.py

# Importing flask module in the project is mandatory 
# An object of Flask class is our WSGI application. 
from flask import Flask 

# Flask constructor takes the name of 
# current module (__name__) as argument. 
app = Flask(__name__) 

# The route() function of the Flask class is a decorator, 
# which tells the application which URL should call 
# the associated function. 
@app.route('/') 
# ‘/’ URL is bound with hello_world() function. 
def hello_world(): 
    return 'Hello World'

# main driver function 
if __name__ == '__main__': 

    # run() method of Flask class runs the application 
    # on the local development server. 
    app.run()

推荐阅读