python - Flask 没有渲染所有的动态 html 表
问题描述
我将 Flask 用于我的 Web 应用程序,并使用机器学习模型进行模式预测。用户可以输入多个图像,并且基于这些图像将有一个动态表格,该表格将作为结果分析提供给用户。
问题是假设用户输入 3 个图像,只有 1 个表格可见,而不是 3 个不同的表格用于 3 个不同的图像。
我是 Flask 的新手,我不确定我做错了什么。控制台中没有错误消息。
app.py(POST 请求)
@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 个表。
推荐阅读
- javascript - 如何修复“加载资源失败:操作无法完成。iOS 12 视频中的“协议错误”?
- mysql - MySQL Query 跨越 40,000 行 v/s 一次加载和 for 循环
- karate - 如何在空手道中上传多个包含多部分的文件
- python - 如何编写 Django 查询并在 WHERE 子句中包含 PostGres 函数 (current_time)?
- ios - 适用于 Unity 的 iOS 插件,带有导航以添加新联系人
- swift - 我的代码无法在 iPhone 上运行“退出代码 1 失败”错误消息
- r - 如何根据另一个单选按钮输入将单选按钮插入闪亮的 UI?
- git - 为什么 PowerShell 似乎不支持 $env:path 变量?
- java - 如何使 Vertx MongoClient 操作同步但不阻塞 Java 中的事件循环?
- vue.js - 如何在 Vue 中将 v-model 与功能模板组件一起使用?