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 for display categorization = comparator_instance.categorize_mismatches() matched_items = list(categorization['matched_items']) # Filter matched items by sheet if specified if sheet_filter: matched_items = [item for item in matched_items if item.source_sheet == sheet_filter] # Format matched items for JSON (limit to first 500 for performance) matched_data = [] for item in matched_items[:500]: matched_data.append({ 'title': item.title, 'episode': item.episode, 'sheet': item.source_sheet, 'row': item.row_index + 1, 'reason': 'Perfect match' }) # Add matched data to results comparison_results['matched_data'] = matched_data comparison_results['matched_items_count'] = len(matched_items) # Update count for filtered 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 = '''