Flask没有渲染所有的动态html表



我正在使用Flask作为我的web应用程序,我正在使用机器学习模型进行模式预测。用户可以输入多个图像,根据这些图像,将有一个动态表,作为结果分析提供给用户。

问题是假设用户输入3个图像,只有1个表可见,而不是3个不同的图像3个不同的表。

我是Flask的新手,我不确定我做错了什么。控制台中没有错误信息。

app.py (POST Request)

@app.route('/', methods=['POST'])
def upload_image():
if request.method == 'POST':
# checks whether or not the post request has the file part
if 'files' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['files']
if file.filename == '':
flash('No file selected for uploading')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(os.getcwd() +
UPLOAD_INPUT_IMAGES_FOLDER, file.filename))
flash('File successfully uploaded')
extracted_text = ocr_processing(file)
print(extracted_text)
match = extracted_text.lower()
dark_pattern_file = "/Users/ri/Desktop/DPL/DP.csv"
df = pd.read_csv(dark_pattern_file)
for row in df.Pattern_String:
result = ratio(row, match)
print(result)
if result >= 10:
loaded_vec = CountVectorizer(
vocabulary=pickle.load(open("model/tfidf_vector.pkl", "rb")))
loaded_tfidf = pickle.load(open("model/tfidf_transformer.pkl", "rb"))
model_pattern_type = pickle.load(
open("model/clf_svm_Pattern_Category.pkl", "rb"))
model_pattern_category = pickle.load(
open("model/clf_svm_Pattern_Type.pkl", "rb"))
match = [match]
X_new_counts = loaded_vec.transform(
match)
X_new_tfidf = loaded_tfidf.transform(X_new_counts)
predicted_pattern_type = model_pattern_type.predict(X_new_tfidf)
your_predicted_pattern_type = predicted_pattern_type[0]
predicted_pattern_category = model_pattern_category.predict(
X_new_tfidf)
your_predicted_pattern_category = predicted_pattern_category[0]
return render_template('uploads/results.html',
msg='Processed successfully!',
match=match,
your_predicted_pattern_category=your_predicted_pattern_category,
your_predicted_pattern_type=your_predicted_pattern_type,
img_src=UPLOAD_INPUT_IMAGES_FOLDER + file.filename)
else:
return render_template('uploads/results.html',
msg='Processed successfully!',
match=match,
img_src=UPLOAD_INPUT_IMAGES_FOLDER + file.filename)

else:
flash('Allowed file types are txt, pdf, png, jpg, jpeg, gif')
return redirect(request.url)

results.html(表所在位置)

<table>
<tbody>
<tr>
<th class="table-info">IMAGE PROVIDED</th>
{% if img_src %}
<td><img src="{{ img_src }}" alt="user-image" class="uploaded-image"></td>
{% endif %}
</tr>
<tr>
//Many other such rows
</tr>

</tbody>
</table>

在py代码中使用return render_template(...)会导致循环结束。
您需要做的是将想要呈现的数据累积到一个列表或字典中,并将其传递给模板引擎。
在模板中,您需要遍历条目并创建N个表。

最新更新