from flask import Flask, render_template, redirect, url_for, request, flash from flask_sqlalchemy import SQLAlchemy from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user from werkzeug.security import generate_password_hash, check_password_hash app = Flask(__name__) app.config['SECRET_KEY'] = 'your_secret_key' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db' db = SQLAlchemy(app) login_manager = LoginManager() login_manager.init_app(app) login_manager.login_view = 'login' class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True, nullable=False) password = db.Column(db.String(120), nullable=False) def __repr__(self): return '' % self.username # Create the database tables with app.app_context(): db.drop_all() db.create_all() @login_manager.user_loader def load_user(user_id): return User.query.get(int(user_id)) @app.route('/', methods=['GET', 'POST']) def signup(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] hashed_password = generate_password_hash(password, method='pbkdf2:sha256') new_user = User(username=username, password=hashed_password) db.session.add(new_user) db.session.commit() flash('Signup successful!', 'success') return redirect(url_for('login')) return render_template('signup.html') @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] user = User.query.filter_by(username=username).first() if user and check_password_hash(user.password, password): login_user(user) return redirect(url_for('dashboard')) flash('Invalid username or password', 'danger') return render_template('login.html') @app.route('/run_generation') @login_required def run_generation_route(): # Call the run_generation function here result = run_generation() return render_template('generation.html', result=result) @app.route('/dashboard') @login_required def dashboard(): return render_template('dashboard.html', name=current_user.username) @app.route('/logout') @login_required def logout(): logout_user() return redirect(url_for('login')) if __name__ == '__main__': app.run(debug=True)