我有一个网页,用户在其中上传图片,我的 Python 代码将对它进行一些调整并进行一些分析。我希望新生成的图像在生成后立即在网页上显示给用户,然后继续进行需要进行的分析,然后使用该信息更新网页。但是,我不确定如何在功能中途(一旦生成新图像)从flask到网页通信网站可以显示新生成的图像,因为使用render_template只能在功能结束时完成。
我的python(烧瓶)代码如下:
@app.route('/uploaded', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
filename = secure_filename(file.filename)
f = request.files['file']
f.save(secure_filename(f.filename))
image = cv2.imread(filename)
im = Image.fromarray(image)
# make some adjustments to the image (not shown here) and then save it...
im.save(os.path.join(app.config['UPLOAD_FOLDER'], 'your_file.jpg'))
# after the new image is generated, display it on the website at {{ place_for_generated_image }}
# do some more analysis, then finally:
return render_template('index.html', analysis = analysis)
HTML很简单:
<form action = "http://localhost/uploaded" method = "POST"
enctype = "multipart/form-data">
<input type = "file" name = "file" class="form-control-file">
<input type = "submit" class="btn btn-info" value="Upload Image" button id="uploadfile" onclick="uploadfile()">
</form>
{{ place_for_generated_image }}
{{ analysis }}
倚天杖
相关分类