diff --git a/test_apps/test_app4.py b/test_apps/test_app4.py new file mode 100644 index 0000000..61de004 --- /dev/null +++ b/test_apps/test_app4.py @@ -0,0 +1,92 @@ +import pandas as pd +from flask import Flask, request, render_template_string +import io + +app = Flask(__name__) + +@app.route('/') +def index(): + return ''' +

Upload File

+
+ +

+ +

+ +
+ ''' + +@app.route('/upload', methods=['POST']) +def upload_file(): + if 'file' not in request.files or 'tracking_no' not in request.form: + return "No file or tracking number provided.", 400 + + file = request.files['file'] + tracking_no = request.form['tracking_no'] + filename = file.filename + + try: + # Determine file type and read accordingly + if filename.endswith('.csv'): + df = pd.read_csv(file, header=None, dtype=str) + elif filename.endswith(('.xls', '.xlsx')): + df = pd.read_excel(file, header=None, dtype=str) + else: + return "Unsupported file type. Please upload a CSV or Excel file.", 400 + + # Find the "Mail Bag No:" label and retrieve its value from column C (index 2) + mail_bag_no = None + for i, row in df.iterrows(): + if row[0] == "Mail Bag No:": + mail_bag_no = row[2] + break + + if mail_bag_no is None: + mail_bag_no = "N/A" + + # Locate header row by looking for "Tracking No" in any row + header_row_index = None + for i, row in df.iterrows(): + if row.str.contains("Tracking No", case=False, na=False).any(): + header_row_index = i + break + + if header_row_index is None: + return "'Tracking No' column not found in the file." + + # Reload the file with the identified header row + file.seek(0) # Reset file pointer to start of file for re-read + if filename.endswith('.csv'): + df = pd.read_csv(file, skiprows=header_row_index) + else: + df = pd.read_excel(file, skiprows=header_row_index) + + except Exception as e: + return f"Error processing file: {e}", 500 + + # Confirm the 'Tracking No' column exists in this adjusted DataFrame + if 'Tracking No' not in df.columns: + return f"'Tracking No' column not found. Available columns after adjustment: {', '.join(df.columns)}" + + # Filter the data based on the tracking number + filtered_data = df[df['Tracking No'] == tracking_no] + + if filtered_data.empty: + return f"No data found for Tracking No: {tracking_no}" + + # Collect data for each matching entry + result_html = "

Tracking Information

" + for _, row in filtered_data.iterrows(): + hash_column = row.get('#', "N/A") + result_html += f""" +

Tracking No: {row['Tracking No']}

+

Mail Bag No: {mail_bag_no}

+

#: {hash_column}

+
+ """ + + return render_template_string(result_html) + +if __name__ == '__main__': + app.run(debug=True, port=5000, host='0.0.0.0')