From bcf3f659bb475f31bae9dccfa3815eb1dc874028 Mon Sep 17 00:00:00 2001 From: ralphi3 Date: Thu, 18 Jun 2026 10:34:30 +1200 Subject: [PATCH] database signin and better styling --- app.py | 90 +++++++++++++++++++++ index.html | 18 ----- instance/accounts.db | Bin 0 -> 16384 bytes static/style.css | 59 ++++++++++++++ style.css | 27 ------- about.html => templates/about.html | 8 +- templates/base.html | 21 +++++ bivariate.html => templates/bivariate.html | 13 ++- templates/index.html | 10 +++ templates/signin.html | 13 +++ templates/signup.html | 25 ++++++ test.html => templates/test.html | 0 12 files changed, 233 insertions(+), 51 deletions(-) create mode 100644 app.py delete mode 100644 index.html create mode 100644 instance/accounts.db create mode 100644 static/style.css delete mode 100644 style.css rename about.html => templates/about.html (79%) create mode 100644 templates/base.html rename bivariate.html => templates/bivariate.html (94%) create mode 100644 templates/index.html create mode 100644 templates/signin.html create mode 100644 templates/signup.html rename test.html => templates/test.html (100%) diff --git a/app.py b/app.py new file mode 100644 index 0000000..7b31903 --- /dev/null +++ b/app.py @@ -0,0 +1,90 @@ +from flask import Flask, render_template, redirect, request, url_for +from flask_sqlalchemy import SQLAlchemy +from werkzeug.security import generate_password_hash, check_password_hash + +app = Flask(__name__) + +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///accounts.db' +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False +app.secret_key = 'super_secret_session_encryption_key' +db = SQLAlchemy(app) + +class User(db.Model): + id = db.Column(db.Integer, primary_key=True) + username = db.Column(db.String(80), unique=True, nullable=False) + email = db.Column(db.String(120), unique=True, nullable=False) + password_hash = db.Column(db.String(128), nullable=False) + +with app.app_context(): + db.create_all() + + +@app.route('/submit-form', methods=['POST']) +def handle_submission(): + form_username = request.form.get('username') + form_email = request.form.get('email') + form_password = request.form.get('password') # 1. Grab password from HTML + + # Check for duplicates + if User.query.filter_by(username=form_username).first(): + return "

Username taken!

", 400 + if User.query.filter_by(email=form_email).first(): + return "

Email already registered!

", 400 + + # 2. Hash the password into a secure scramble + hashed_password = generate_password_hash(form_password) + + # 3. Save the user with the scrambled hash + new_user = User(username=form_username, email=form_email, password_hash=hashed_password) + db.session.add(new_user) + db.session.commit() + + return redirect(url_for('success')) + +@app.route('/success') +def success(): + return "

Data successfully saved to the database!

" + +@app.route('/users') +def view_users(): + # Fetch all user records from the database + all_users = User.query.all() + + # Create a simple HTML table to display them nicely + html_output = "

Registered Accounts

" + html_output += "" + + for user in all_users: + html_output += f"" + + html_output += "
IDUsernameEmail
{user.id}{user.username}{user.email}{user.password_hash}
" + + # If the database is completely empty + if not all_users: + return "

No accounts found in the database yet.

" + + return html_output + + +@app.route('/') +def home(): + return render_template('index.html') + +@app.route('/bivariate') +def bivariate(): + return render_template('bivariate.html') + +@app.route('/about') +def about(): + return render_template('about.html') + +@app.route('/signin') +def signin(): + return render_template('signin.html') + +@app.route('/signup') +def signup(): + return render_template('signup.html') + +if __name__ == '__main__': + app.run(debug=True) diff --git a/index.html b/index.html deleted file mode 100644 index 0d9af3f..0000000 --- a/index.html +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - Math Website - - - - -

Math website

-

with math

- - \ No newline at end of file diff --git a/instance/accounts.db b/instance/accounts.db new file mode 100644 index 0000000000000000000000000000000000000000..d62d5603fd7764b61e14456249807d7aaecaa93d GIT binary patch literal 16384 zcmeI&O;6h}7zc2tMX**X-KFAcBqmyEnqud@yHHRB9bLC(?KF0&oW_AlErli>V>dQw zA7vkeFR<^zX~)GyomM2+o&Bvmd9fcm@$cp&?>envDrr2Kjzvlh@|0*A*`bsWlFMQx zi$&40le^c8w)>7b^5T1~RQ^Ts>(^xcy8N^JtmHexvKtZezFQQ&mH&#lkS6&34z{_XFB~)1~cRtM!}~ z3#$24j3s^F2=)#dLDki3O9^r;hNC~NjNfTK6G?J0nMVB+k(~UM<}Q!&HfX+X1V{AH zKcdxPR8zO^wVR!uPpgYZSgM@gwpOfd=T|pgRx~m^jpXO#e3UKO`(l<(=GT4Iwa?Xl zWjpuJM5Yq+ndlRxTFOEz?~`mpfdB*`009U<00Izz00bZa0SG|gVSy`KTP2mvubosT zsfwLr^~N9>OfS#Uy1^aWt-E!$6^IW9UuL@V>GI<6)kjBcG0*i(ZiVK+bD445Q8o*^?#4I6PT{r_CMbar|^U|{K^Vx<10SG_<0uX=z1Rwwb2tWV=5P-lV z6Ijbv3hF(@?fU - +{% extends 'base.html' %} + +{% block content %} @@ -9,4 +10,5 @@

About this website

- \ No newline at end of file + +{% endblock %} \ No newline at end of file diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..6de1853 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,21 @@ + + + + + + {% block title %}Math Website{% endblock %} + + + + +
+ {% block content %}{% endblock %} +
+ + \ No newline at end of file diff --git a/bivariate.html b/templates/bivariate.html similarity index 94% rename from bivariate.html rename to templates/bivariate.html index 1ab6742..011c10b 100644 --- a/bivariate.html +++ b/templates/bivariate.html @@ -1,5 +1,8 @@ - - +{% extends 'base.html' %} + +{% block content %} + +
@@ -32,4 +35,8 @@
- \ No newline at end of file + + + + +{% endblock %} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..a05a401 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,10 @@ +{% extends "base.html" %} +{% block content %} + +
+ +

homepage for math website wow

+ +
+ +{% endblock %} \ No newline at end of file diff --git a/templates/signin.html b/templates/signin.html new file mode 100644 index 0000000..ec9094b --- /dev/null +++ b/templates/signin.html @@ -0,0 +1,13 @@ +{% extends 'base.html' %} + +{% block content %} + + + + + + +

Sign in page with sql database

+ + +{% endblock %} \ No newline at end of file diff --git a/templates/signup.html b/templates/signup.html new file mode 100644 index 0000000..909a6c0 --- /dev/null +++ b/templates/signup.html @@ -0,0 +1,25 @@ +{% extends 'base.html' %} + +{% block content %} + + + + + + +

Sign up page with sql database

+
+ +

+ + +

+ + +

+ + +
+ + +{% endblock %} \ No newline at end of file diff --git a/test.html b/templates/test.html similarity index 100% rename from test.html rename to templates/test.html