0

I am using Flask for my web application and I am using Machine learning models for pattern prediction. Multiple images can be inputed by the user and based on those images there will be a dynamic table that will be provided to the user as a Result analysis.

The issue is suppose a user enters 3 images, only 1 table is visible instead of 3 different tables for 3 different images.

I am new to Flask and I am not sure what am I doing wrong. There are no Error messages in the console.

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 (where the table is)

<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>
EverydayDeveloper
  • 1,110
  • 4
  • 11
  • 34

1 Answers1

0

The fact that use return render_template(...) in the py code cause the loop to end.
What you need to do is to accumulate the data you want to render in a list or dict and pass it to template engine.
In the template you need to loop over the entries and create N tables.

balderman
  • 22,927
  • 7
  • 34
  • 52
  • How can I do that exactly? I am new to flask and I am not sure how to do this? Could you please help – EverydayDeveloper Aug 11 '21 at 12:54
  • Loop over the DF rows. For every row that you want to render a table for - add entry to list. Ex `data = [] data.append({'x':1})`. See jinja2 docs for how to loop over list of dicts. https://stackoverflow.com/questions/25373154/how-to-iterate-through-a-list-of-dictionaries-in-jinja-template – balderman Aug 11 '21 at 12:58
  • As I said before. Create a list of dicts, pass them to `render` and loop over the list INSIDE the template. Good luck. – balderman Aug 11 '21 at 13:07