Spaces:
Paused
Paused
Download app.py from Ragulselvakumar/NotesHub: direct link, hf CLI and curl.
- Browser
- Download file 7.43 kB
-
https://huggingface.co/spaces/Ragulselvakumar/NotesHub/resolve/main/app.py
- Command line
-
hf download hf://spaces/Ragulselvakumar/NotesHub/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Ragulselvakumar/NotesHub/resolve/main/app.py
7.43 kB
| from flask import Flask, render_template, request, redirect, url_for, session, send_from_directory, flash | |
| import sqlite3 | |
| import os | |
| from werkzeug.security import generate_password_hash, check_password_hash | |
| from werkzeug.utils import secure_filename | |
| app = Flask(__name__) | |
| app.secret_key = "noteshub_secret_key_2026" | |
| DATABASE = "noteshub.db" | |
| UPLOAD_FOLDER = "uploads" | |
| app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER | |
| os.makedirs(UPLOAD_FOLDER, exist_ok=True) | |
| # DATABASE | |
| def get_db(): | |
| conn = sqlite3.connect(DATABASE) | |
| conn.row_factory = sqlite3.Row | |
| return conn | |
| def init_db(): | |
| conn = get_db() | |
| conn.execute(""" | |
| CREATE TABLE IF NOT EXISTS students ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| name TEXT NOT NULL, | |
| email TEXT UNIQUE NOT NULL, | |
| password TEXT NOT NULL, | |
| department TEXT NOT NULL, | |
| semester TEXT NOT NULL | |
| ) | |
| """) | |
| conn.execute(""" | |
| CREATE TABLE IF NOT EXISTS notes ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| title TEXT NOT NULL, | |
| department TEXT NOT NULL, | |
| semester TEXT NOT NULL, | |
| subject TEXT NOT NULL, | |
| unit TEXT NOT NULL, | |
| filename TEXT NOT NULL, | |
| uploaded_by INTEGER | |
| ) | |
| """) | |
| conn.commit() | |
| conn.close() | |
| # HOME | |
| def home(): | |
| conn = get_db() | |
| notes = conn.execute(""" | |
| SELECT notes.*, students.name AS uploader | |
| FROM notes | |
| LEFT JOIN students | |
| ON notes.uploaded_by = students.id | |
| ORDER BY notes.id DESC | |
| """).fetchall() | |
| conn.close() | |
| return render_template("index.html", notes=notes) | |
| # REGISTER | |
| def register(): | |
| if request.method == "POST": | |
| name = request.form["name"] | |
| email = request.form["email"] | |
| password = request.form["password"] | |
| department = request.form["department"] | |
| semester = request.form["semester"] | |
| hashed_password = generate_password_hash(password) | |
| try: | |
| conn = get_db() | |
| conn.execute(""" | |
| INSERT INTO students | |
| (name, email, password, department, semester) | |
| VALUES (?, ?, ?, ?, ?) | |
| """, ( | |
| name, | |
| email, | |
| hashed_password, | |
| department, | |
| semester | |
| )) | |
| conn.commit() | |
| conn.close() | |
| flash("Registration successful! Please login.") | |
| return redirect(url_for("login")) | |
| except sqlite3.IntegrityError: | |
| flash("Email already registered.") | |
| return redirect(url_for("register")) | |
| return render_template("register.html") | |
| # LOGIN | |
| def login(): | |
| if request.method == "POST": | |
| email = request.form["email"] | |
| password = request.form["password"] | |
| conn = get_db() | |
| student = conn.execute(""" | |
| SELECT * | |
| FROM students | |
| WHERE email = ? | |
| """, (email,)).fetchone() | |
| conn.close() | |
| if student and check_password_hash( | |
| student["password"], | |
| password | |
| ): | |
| session["student_id"] = student["id"] | |
| session["student_name"] = student["name"] | |
| return redirect(url_for("dashboard")) | |
| flash("Invalid email or password.") | |
| return render_template("login.html") | |
| # LOGOUT | |
| def logout(): | |
| session.clear() | |
| return redirect(url_for("home")) | |
| # DASHBOARD | |
| def dashboard(): | |
| if "student_id" not in session: | |
| return redirect(url_for("login")) | |
| conn = get_db() | |
| notes = conn.execute(""" | |
| SELECT * | |
| FROM notes | |
| WHERE uploaded_by = ? | |
| ORDER BY id DESC | |
| """, (session["student_id"],)).fetchall() | |
| conn.close() | |
| return render_template( | |
| "dashboard.html", | |
| notes=notes | |
| ) | |
| # UPLOAD NOTES | |
| def upload(): | |
| if "student_id" not in session: | |
| return redirect(url_for("login")) | |
| if request.method == "POST": | |
| title = request.form["title"] | |
| department = request.form["department"] | |
| semester = request.form["semester"] | |
| subject = request.form["subject"] | |
| unit = request.form["unit"] | |
| file = request.files["file"] | |
| if file and file.filename: | |
| filename = secure_filename(file.filename) | |
| if not filename.lower().endswith(".pdf"): | |
| flash("Only PDF files are allowed.") | |
| return redirect(url_for("upload")) | |
| file.save( | |
| os.path.join( | |
| app.config["UPLOAD_FOLDER"], | |
| filename | |
| ) | |
| ) | |
| conn = get_db() | |
| conn.execute(""" | |
| INSERT INTO notes | |
| (title, department, semester, subject, | |
| unit, filename, uploaded_by) | |
| VALUES (?, ?, ?, ?, ?, ?, ?) | |
| """, ( | |
| title, | |
| department, | |
| semester, | |
| subject, | |
| unit, | |
| filename, | |
| session["student_id"] | |
| )) | |
| conn.commit() | |
| conn.close() | |
| flash("Notes uploaded successfully!") | |
| return redirect(url_for("dashboard")) | |
| return render_template("upload.html") | |
| # SEARCH NOTES | |
| def search(): | |
| subject = request.args.get("subject", "") | |
| unit = request.args.get("unit", "") | |
| conn = get_db() | |
| query = """ | |
| SELECT notes.*, students.name AS uploader | |
| FROM notes | |
| LEFT JOIN students | |
| ON notes.uploaded_by = students.id | |
| WHERE 1=1 | |
| """ | |
| values = [] | |
| if subject: | |
| query += " AND subject LIKE ?" | |
| values.append("%" + subject + "%") | |
| if unit: | |
| query += " AND unit = ?" | |
| values.append(unit) | |
| query += " ORDER BY notes.id DESC" | |
| notes = conn.execute( | |
| query, | |
| values | |
| ).fetchall() | |
| conn.close() | |
| return render_template( | |
| "search.html", | |
| notes=notes, | |
| subject=subject, | |
| unit=unit | |
| ) | |
| # DOWNLOAD | |
| def download(filename): | |
| return send_from_directory( | |
| app.config["UPLOAD_FOLDER"], | |
| filename, | |
| as_attachment=True | |
| ) | |
| # DELETE NOTE | |
| def delete_note(note_id): | |
| if "student_id" not in session: | |
| return redirect(url_for("login")) | |
| conn = get_db() | |
| note = conn.execute(""" | |
| SELECT * | |
| FROM notes | |
| WHERE id = ? | |
| AND uploaded_by = ? | |
| """, ( | |
| note_id, | |
| session["student_id"] | |
| )).fetchone() | |
| if note: | |
| file_path = os.path.join( | |
| UPLOAD_FOLDER, | |
| note["filename"] | |
| ) | |
| if os.path.exists(file_path): | |
| os.remove(file_path) | |
| conn.execute(""" | |
| DELETE FROM notes | |
| WHERE id = ? | |
| """, (note_id,)) | |
| conn.commit() | |
| conn.close() | |
| return redirect(url_for("dashboard")) | |
| # START APPLICATION | |
| init_db() | |
| if __name__ == "__main__": | |
| app.run( | |
| host="0.0.0.0", | |
| port=7860, | |
| debug=False | |
| ) |