首页 > 解决方案 > 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>

标签: pythonhtmlflask

解决方案


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


推荐阅读