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) # Handle empty string as None if sheet_filter == '' or sheet_filter == 'undefined': 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 # Debug: Print available sheets available_sheets = list(comparator_instance.data.keys()) print(f"Available sheets: {available_sheets}") print(f"Requested sheet_filter: {repr(sheet_filter)}") # 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 # Generate visualize data visualize_data = comparator_instance.generate_visualize_data(sheet_filter) comparison_results['visualize_data'] = visualize_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) @app.route('/get_sheets', methods=['POST']) def get_sheets(): """Get available sheet names from an Excel file""" try: file_path = request.json.get('file_path', 'data/sample-data.xlsx') if not Path(file_path).exists(): return jsonify({'error': f'File not found: {file_path}'}), 400 # Create comparator and load data to get sheet names temp_comparator = KSTCoordiComparator(file_path) if not temp_comparator.load_data(): return jsonify({'error': 'Failed to load Excel data'}), 500 available_sheets = list(temp_comparator.data.keys()) return jsonify({ 'success': True, 'sheets': available_sheets, 'default_sheet': available_sheets[0] if available_sheets else None }) except Exception as e: return jsonify({'error': str(e)}), 500 def create_templates_dir(): """Create templates directory and HTML file""" templates_dir = Path('templates') templates_dir.mkdir(exist_ok=True) html_content = '''