from flask import Flask, render_template, request, send_file, redirect, url_for, flash import os from werkzeug.utils import secure_filename import corelate app = Flask(__name__) app.secret_key = os.urandom(24) UPLOAD_FOLDER = os.path.dirname(os.path.abspath(__file__)) ALLOWED_EXTENSIONS = {'txt','csv'} app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 def allowed_file(filename): return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS @app.route('/') def index(): report1_exists = os.path.exists(corelate.outputfile1) report2_exists = os.path.exists(corelate.outputfile2) errors_exist = os.path.exists(corelate.errorfile) report1_data = [] report2_data = [] error_data = [] if report1_exists: with open(corelate.outputfile1, 'r') as f: for line in f: parts = line.strip().split('\t') if len(parts) >= 3: report1_data.append({ 'zipcode': parts[0], 'customers': parts[1], 'distances': parts[2] }) if report2_exists: with open(corelate.outputfile2, 'r') as f: for line in f: parts = line.strip().split('\t') if len(parts) >= 2: report2_data.append({ 'distance': parts[0], 'customers': parts[1] }) if errors_exist: with open(corelate.errorfile, 'r') as f: error_data = f.readlines() return render_template('index.html', report1_data=report1_data, report2_data=report2_data, error_data=error_data, report1_exists=report1_exists, report2_exists=report2_exists, errors_exist=errors_exist) @app.route('/run-script', methods=['GET']) def run_script(): try: corelate.process_data() flash('Script executed successfully!', 'success') except Exception as e: flash(f'Error running script: {str(e)}', 'error') return redirect(url_for('index')) @app.route('/download/') def download_file(report_type): if report_type == 'report1': return send_file(corelate.outputfile1, as_attachment=True) elif report_type == 'report2': return send_file(corelate.outputfile2, as_attachment=True) elif report_type == 'errors': return send_file(corelate.errorfile, as_attachment=True) else: flash('Invalid report type requested', 'error') return redirect(url_for('index')) @app.route('/upload', methods=['POST']) def upload_file(): if 'file' not in request.files: flash('No file part', 'error') return redirect(url_for('index')) file = request.files['file'] file_type = request.form.get('file_type') if file.filename == '': flash('No file selected', 'error') return redirect(url_for('index')) if file and allowed_file(file.filename): filename = secure_filename(file.filename) if file_type == 'zipcodedata': target_file = corelate.statedatafile elif file_type == 'ziplist': target_file = corelate.ziplist elif file_type == 'servicecenters': target_file = corelate.servicecenters else: flash('Invalid file type', 'error') return redirect(url_for('index')) file.save(os.path.join(app.config['UPLOAD_FOLDER'], target_file)) flash(f'File successfully uploaded and replaced {target_file}', 'success') else: flash('File type not allowed. Please upload .txt files only.', 'error') return redirect(url_for('index')) if __name__ == "__main__": import os port = int(os.environ.get("PORT", "7860")) app.run(host="0.0.0.0", port=port, debug=False)