from flask import Flask, render_template, request, redirect, url_for, flash
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
from werkzeug.utils import secure_filename
import os
app = Flask(__name__)
app.config["SECRET_KEY"] = "secret_key_here"
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///studyverse.db"
app.config["UPLOAD_FOLDER"] = "uploads/"
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login"
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), unique=True, nullable=False)
password = db.Column(db.String(128), nullable=False)
class Assignment(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(128), nullable=False)
file = db.Column(db.String(128), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
user = db.relationship("User", backref=db.backref("assignments", lazy=True))
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
@app.route("/")
def index():
return """
<!DOCTYPE html>
<html>
<head>
<title>StudyVerse</title>
</head>
<body>
<h1>Welcome to StudyVerse!</h1>
<p><a href="/signup">Sign up</a> or <a href="/login">Login</a></p>
</body>
</html>
"""
@app.route("/signup", methods=["GET", "POST"])
def signup():
if request.method == "POST":
username = request.form["username"]
password = request.form["password"]
user = User(username=username, password=password)
db.session.add(user)
db.session.commit()
flash("Sign up successful!")
return redirect(url_for("login"))
return """
<!DOCTYPE html>
<html>
<head>
<title>Sign Up - StudyVerse</title>
</head>
<body>
<h1>Sign Up</h1>
<form action="/signup" method="POST">
<label for="username">Username:</label><br>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password" required><br><br>
<input type="submit" value="Sign Up">
</form>
</body>
</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 user.password == password:
login_user(user)
flash("Login successful!")
return redirect(url_for("dashboard"))
else:
flash("Invalid username or password")
return """
<!DOCTYPE html>
<html>
<head>
<title>Login - StudyVerse</title>
</head>
<body>
<h1>Login</h1>
<form action="/login" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
"""
@app.route("/logout")
@login_required
def logout():
logout_user()
flash("Logged out successfully!")
return redirect(url_for("index"))
@app.route("/dashboard")
@login_required
def dashboard():
assignments = Assignment.query.filter_by(user_id=current_user.id).all()
assignment_list = ""
for assignment in assignments:
assignment_list += f"<li>{assignment.title}</li>"
return f"""
<!DOCTYPE html>
<html>
<head>
<title>Dashboard - StudyVerse</title>
</head>
<body>
<h1>Welcome to Your Dashboard</h1>
<p>Hello, {current_user.username}!</p>
<h2>Your Assignments:</h2>
<ul>
{assignment_list}
</ul>
<p><a href="/upload">Upload Assignment</a> | <a href="/logout">Logout</a></p>
</body>
</html>
"""
@app.route("/upload", methods=["GET", "POST"])
@login_required
def upload():
if request.method == "POST":
file = request.files["file"]
filename = secure_filename(file.filename)
file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
assignment = Assignment(title=request.form["title"], file=filename, user_id=current_user.id)
db.session.add(assignment)
db.session.commit()
flash("Assignment uploaded successfully!")
return redirect(url_for("dashboard"))
return """
<!DOCTYPE html>
<html>
<head>
<title>Upload Assignment - StudyVerse</title>
</head>
<body>
<h1>Upload Assignment</h1>
<form action="/upload" method="POST" enctype="multipart/form-data">
<label for="title">Title:</label><br>
<input type="text" id="title" name="title" required><br><br>
<label for="file">File:</label><br>
<input type="file" id="file" name="file" required><br><br>
<input type="submit" value="Upload">
</form>
<p><a href="/dashboard">Back to Dashboard</a></p>
</body>
</html>
"""
@app.route("/chat")
@login_required
def chat():
return """
<!DOCTYPE html>
<html>
<head>
<title>Chat - StudyVerse</title>
</head>
<body>
<h1>Chat Room</h1>
<p>This is the chat room. You can start chatting here.</p>
<p><a href="/dashboard">Back to Dashboard</a></p>
</body>
</html>
"""
if __name__ == "__main__":
app.run(debug=True)
Write, Run & Share HTML code online using OneCompiler's HTML online Code editor for free. It's one of the robust, feature-rich online Code editor for HTML language, running on the latest version HTML5. Getting started with the OneCompiler's HTML compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as HTML. You can also specify the stylesheet information in styles.css tab and scripts information in scripts.js tab and start coding.
HTML(Hyper Text Markup language) is the standard markup language for Web pages, was created by Berners-Lee in the year 1991. Almost every web page over internet might be using HTML.
<!DOCTYPE html><html> and ends with </html><h1> to <h6> where <h1> is the highest important heading and <h6> is the least important sub-heading.<p>..</p> tag.<a> tag.
<a href="https://onecompiler.com/html">HTML online compiler</a>
<img> tag, where src attribute consists of image name.<button>..</button> tag<ul> for unordered/bullet list and <ol> for ordered/number list, and the list items are defined in <li>.<a href="https://onecompiler.com/html">HTML online compiler</a>
CSS(cascading style sheets) describes how HTML elements will look on the web page like color, font-style, font-size, background color etc.
Below is a sample style sheet which displays heading in green and in Candara font with padding space of 25px.
body{
padding: 25px;
}
.title {
color: #228B22;
font-family: Candara;
}
<table> tag.<tr> tag<th> tag<td> tag<caption> tag<script> is the tag used to write scripts in HTML<script src="script.js"></script>