首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用flask框架在单击html按钮时在后台运行python脚本

如何使用flask框架在单击html按钮时在后台运行python脚本
EN

Stack Overflow用户
提问于 2019-03-24 04:05:07
回答 1查看 3.8K关注 0票数 2

‘上传.html’

代码语言:javascript
运行
复制
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2 style="color:DodgerBlue;">File Uploader</h2>

<form id = "upload-form" action = "{{ url_for('upload') }}" method="POST" 
 enctype="multipart/form-data">
<input type = "file" name = "file" accept = "files/*" multiple>
<input type = "submit" value = "submit">
</form>
</body>
</html>

‘app.py’

代码语言:javascript
运行
复制
import os
from flask import Flask, request, render_template, send_from_directory
app = Flask(__name__)

APP_ROOT = os.path.dirname(os.path.abspath(__file__))

@app.route("/")
def index():
    return render_template("upload.html")

@app.route("/upload", methods=["POST"])
def upload():

    target = os.path.join(APP_ROOT, 'Athena_XML_files/')
    print(target)
    if not os.path.isdir(target):
        os.mkdir(target)

    for file in request.files.getlist("file"):
        print(file)
        filename = file.filename
        destination = "/".join([target, filename])
        print(destination)
        file.save(destination)

    return render_template("complete.html")

@app.route('/run_script')
def run_script():
    app.config.from_pyfile("main.py")
    return render_template('result.html')

if __name__ == "__main__":
    app.run(port=4555, debug=True)

‘完成.html’

代码语言:javascript
运行
复制
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>XML files Uploaded</h1>
<ul><br><br>
<h2><strong>Please click to obtain the result</strong></h2><br><br>
<ul class="nav nav-stacked">
    <li role="presentation"><a href="{{ url_for('run_script') }}">CLICK  
HERE</a></li>
</ul>
</ul>
</body>
</html>

上传XML文件并单击CLICK HERE按钮后,main.py文件应该会在后台运行,我已经在app.py文件中为此创建了一个函数来运行main.py文件。

上传工作正常,但单击按钮后,main.py文件不会在后台运行。

EN

回答 1

Stack Overflow用户

发布于 2019-03-24 10:23:05

我不确定你的主意是不是一个好主意。我发布了一些小的解决方案,但您应该阅读this的文章以获得更好的实践。

1)如果要从flask内部运行外部脚本,可以使用子进程从命令行运行脚本。

代码语言:javascript
运行
复制
@app.route('/run-script')
def run_script():
   result = subprocess.check_output("python main.py", shell=True)
   return render_template('results.html', **locals())

2)如果你想在后台运行Python代码而不返回任何内容,你可以创建一个线程。

代码语言:javascript
运行
复制
from threading import Thread

@app.route('/run-in-background')
def run_in_background():
    run_func()
    return redirect(url_for('.index'))

def run_func():
    data = { 'some': 'data', 'any': 'data' }
    thr = Thread(target=run_async_func, args=[app, data])
    thr.start()
    return thr

def run_async_func(app, data):
    with app.app_context():
    # Your working code here!
    example_module.do_somthing_with(data)

不确定这是否有帮助。这两种解决方案都会搞得一团糟。

你应该读一下烧瓶文档。app.config.from_pyfile函数计算来自Python代码的配置数据。这与你的问题非常不同。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55317880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档