from flask import Flask, render_template, redirect, url_for
import subprocess
import os

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/run', methods=['GET', 'POST'])
def run_classification():
    # Run the iris_classification.py script
    subprocess.run(['python3', 'iris_classification.py'], check=True)
   
      # List of images to move to static folder
    images = ['knn_vs_kmeans.png', 'confusion_matrix.png', 'pairplot.png']
    
    for image in images:
        if os.path.exists(image):
            import shutil
            shutil.move(image, f'static/{image}')     

    return redirect(url_for('results'))

@app.route('/results')
def results():
    # Get list of available images
    static_dir = 'static'
    available_images = []
    
    image_files = ['knn_vs_kmeans.png', 'confusion_matrix.png', 'pairplot.png']
    for image in image_files:
        if os.path.exists(os.path.join(static_dir, image)):
            available_images.append(image)
    
    return render_template('results.html', images=available_images)

if __name__ == '__main__':
    app.run(debug=True)