Created
August 14, 2020 23:52
-
-
Save paulouskunda/69bb6ceeba4bf800db7e84f74cd6b026 to your computer and use it in GitHub Desktop.
Running a flask app locally with MySQL install in Xampp (Lampp - Linux Users), this method works fine.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template, redirect, session, request, url_for | |
import pymysql | |
import secret | |
# Initialize the flask library | |
app = Flask(__name__) | |
# create secret key to use for sessions | |
app.secret_key = "JustMakeThisEndInWeirdLines123@#!IThinkThisIsFine" | |
# create a connection to the mysql (phpMyAdmin) server | |
# you can create a secret.py file or just replace | |
connection = pymysql.connect(host=secret.dbHost, | |
user=secret.dbUser, | |
passwd=secret.dbPass, | |
database=secret.dbName) | |
# cursor = connection.cursor() | |
# Login route | |
@app.route("/", methods=['GET', 'POST']) | |
def login(): | |
msg = '' | |
# Check if "username" and "password" POST requests exist(user submitted form) | |
if request.method == 'POST' and 'username' in request.form and 'password' in request.form: | |
# Get the passed values | |
username = request.form['username'] | |
password = request.form['password'] | |
with connection.cursor() as cursor: | |
# check if the logged details do exist in the database | |
check_user_existence = "SELECT * FROM users WHERE username = '" + username + "' AND password = '" + password + "'; " | |
cursor.execute(check_user_existence) | |
connection.commit() | |
# fetch the one record from the db | |
user = cursor.fetchone() | |
# check if the sql returns true or false | |
if user: | |
# create session variables | |
session['isLoggedIn'] = True | |
# pymysql accepts int, so have to know the column number | |
session['id'] = user[0] | |
session['username'] = user[2] | |
# redirect to home page | |
return redirect(url_for('home')) | |
else: | |
# User does not exist or wrong username/password provided | |
msg = "Wrong username/Password" | |
return render_template("login.html", msg=msg) | |
# Logout script | |
@app.route("/logout") | |
def logout(): | |
# kill/terminate the current session, this will log the user out of the application | |
# session.pop() takes two arguments, one is for the session name and the other one is to elimate | |
session.pop('isLoggedIn', None) | |
session.pop('id', None) | |
session.pop('username', None) | |
return redirect(url_for('login')) | |
# Home/default route | |
@app.route("/home") | |
def home(): | |
# check if the session is active | |
if 'isLoggedIn' in session: | |
# query for totals cases | |
with connection.cursor() as cursor: | |
select_totals = "SELECT * FROM cumulative_totals;" | |
cursor.execute(select_totals) | |
# connection is not autocommit by default. So you must commit to save | |
# your changes. | |
connection.commit() | |
# fetch all the totals in the database | |
# Bug Query doesn't pick new values unless the application is restarted, not sure if its the | |
cumulative_totals = cursor.fetchone() | |
print(cumulative_totals) | |
# username is logged in, show the home page, | |
# cumulative_totals passes the result from the query | |
return render_template("index.html", username=session['username'], cumulative_totals=cumulative_totals) | |
return redirect(url_for('login')) | |
@app.route("/addNew") | |
def add_new(): | |
if 'isLoggedIn' in session: | |
return render_template("AddNew.html") | |
return render_template("login.html") | |
# entry point for the application | |
if __name__ == "__main__": | |
# Set the port for the application, this is only for development | |
# turn debug on in development | |
app.run(port=5004, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment