from flask import Flask, render_template, request, jsonify import json import os import tempfile from pathlib import Path from werkzeug.utils import secure_filename from data_comparator import KSTCoordiComparator app = Flask(__name__) app.config['MAX_CONTENT_LENGTH'] = 50 * 1024 * 1024 # 50MB max file size app.config['UPLOAD_FOLDER'] = tempfile.gettempdir() # Global variable to store comparison results comparison_results = None comparator_instance = None @app.route('/') def index(): return render_template('index.html') @app.route('/analyze', methods=['POST']) def analyze_data(): global comparison_results, comparator_instance try: file_path = request.json.get('file_path', 'data/sample-data.xlsx') sheet_filter = request.json.get('sheet_filter', None) if not Path(file_path).exists(): return jsonify({'error': f'File not found: {file_path}'}), 400 # Create comparator and analyze comparator_instance = KSTCoordiComparator(file_path) if not comparator_instance.load_data(): return jsonify({'error': 'Failed to load Excel data'}), 500 # Get comparison results with optional sheet filtering comparison_results = comparator_instance.get_comparison_summary(sheet_filter) # Get matched items from the grouped data matched_items_data = [] for title, items in comparison_results['grouped_by_title']['matched_by_title'].items(): for item in items[:500]: # Limit for performance matched_items_data.append({ 'title': item['title'], 'episode': item['episode'], 'sheet': item['sheet'], 'row': item['row_index'] + 1 if item['row_index'] is not None else 'N/A', 'reason': 'Perfect match' }) # Add matched data to results comparison_results['matched_data'] = matched_items_data return jsonify({ 'success': True, 'results': comparison_results }) except Exception as e: return jsonify({'error': str(e)}), 500 @app.route('/upload', methods=['POST']) def upload_file(): try: if 'file' not in request.files: return jsonify({'error': 'No file provided'}), 400 file = request.files['file'] if file.filename == '': return jsonify({'error': 'No file selected'}), 400 if file and file.filename.lower().endswith(('.xlsx', '.xls')): # Save uploaded file filename = secure_filename(file.filename) file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(file_path) return jsonify({ 'success': True, 'file_path': file_path, 'filename': filename }) else: return jsonify({'error': 'Please upload an Excel file (.xlsx or .xls)'}), 400 except Exception as e: return jsonify({'error': str(e)}), 500 @app.route('/get_results') def get_results(): if comparison_results is None: return jsonify({'error': 'No analysis results available'}), 404 return jsonify(comparison_results) def create_templates_dir(): """Create templates directory and HTML file""" templates_dir = Path('templates') templates_dir.mkdir(exist_ok=True) html_content = ''' KST vs Coordi Data Comparison

KST vs Coordi Data Comparison Tool

''' html_file = templates_dir / 'index.html' html_file.write_text(html_content) def main(): # Create templates directory and HTML file create_templates_dir() print("Starting web-based GUI...") print("Open your browser and go to: http://localhost:8080") app.run(debug=True, host='0.0.0.0', port=8080) if __name__ == "__main__": main()