article

Friday, September 16, 2022

Python Eel Registration Form with sqlite3 and Password Hash

Python Eel Registration Form with sqlite3 and Password Hash

Eel is a little Python library for making simple Electron-like offline HTML/JS GUI apps, with full access to Python capabilities and libraries.
https://github.com/ChrisKnott/Eel

Install
pip install eel
C:\python_dev\login>pip install eel

Create Database sqlite3 using python
createdb.py
#createdb.py
import sqlite3
from sqlite3 import Error 
 
def create_connection(db_file):
    """ create a database connection to a SQLite database """
    conn = None
    try:
        conn = sqlite3.connect(db_file)
        print(sqlite3.version)
    except Error as e:
        print(e)
    finally:
        if conn:
            conn.close()
 
 
if __name__ == '__main__':
    create_connection(r"C:\python_dev\registration\views\database\storage.db")
run
C:\python_dev\registration>createdb.py

Install PyAutoGUI

PyAutoGUI is a cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.

pip install PyAutoGUI
C:\python_dev\registration>pip install PyAutoGUI

Install werkzeug

pip install werkzeug
C:\python_dev\registration>pip install werkzeug

import from werkzeug.security import generate_password_hash
main.py
#main.py
from __future__ import print_function
import eel
from views.models.signup import save_register
import pyautogui #https://pypi.org/project/PyAutoGUI/

eel.init('views')

@eel.expose 
def btn_save(name, email, passw):
    msg = save_register(name, email, passw)
    eel.save_return(str(msg))
    
eel.start(
    'index.html',
    size=pyautogui.size()
)
Create Table
CREATE TABLE tblusers (
id INTEGER PRIMARY KEY AUTOINCREMENT
UNIQUE,
name VARCHAR (150),
email VARCHAR (150),
password VARCHAR (150)
);

registration/views/models/signup.py
#registration/views/models/signup.py
import sqlite3
from werkzeug.security import generate_password_hash #pip install werkzeug

def save_register(name, email, passw):
    try:
        connect = sqlite3.connect("views/database/storage.db")
        cursor = connect.cursor()
        _hashed_password = generate_password_hash(passw)
        print(_hashed_password)
        if name != "" and passw != "" and email != "":
            cursor.execute("INSERT INTO tblusers(name, email, password) VALUES(?,?,?)",(name, email, _hashed_password))
            connect.commit()
            connect.close()
            msg = "success"
            return msg
        else:
            msg = "failure"
            return msg
    except Exception as Error:
        print(Error)
        msg = "failure"
        return msg
Make your Front-end HTML CSS and Javascript
registration/views/index.html
//registration/views/index.html
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="/eel.js"></script>
    <link rel="stylesheet" href="css/index.css">
    <link rel="icon" href="img/password.png">
    <title>Python Registration</title>
</head>
<body class="form-v2">
	<div class="page-content">
		<div class="form-v2-content">
			<div class="form-left">
				<img src="img/form-v2.jpg" alt="form">
			</div>
			<form class="form-detail" action="#" method="post" id="myform">
				<h2>Registration Form</h2>
				<div class="form-row">
					<label for="full-name">Full Name:</label> 
					<input type="text" name="txtfullname" id="txtfullname" class="input-text" required placeholder="ex: Cairocoders Ednalan">
				</div>
				<div class="form-row">
					<label for="your_email">Your Email:</label>
					<input type="text" name="txtemail" id="txtemail" class="input-text" required pattern="[^@]+@[^@]+.[a-zA-Z]{2,6}">
				</div>
				<div class="form-row">
					<label for="password">Password:</label>
					<input type="password" name="password" id="password" class="input-text" required>
				</div>
				<div class="form-row">
					<label for="comfirm-password">Confirm Password:</label>
					<input type="password" name="confirm_password" id="confirm_password" class="input-text" required>
				</div>
				<div class="form-row-last">
					<input type="submit" name="register" class="register" value="Register" onclick="save_register_js();">
				</div>
				
				<div class="box"><label class="label_input" id="return_register" style="width: 90%;color:green;font-weight:bold;"></label></div>
			</form>
		</div>
	</div>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
<script>
		jQuery.validator.setDefaults({
		  	debug: true,
		  	success:  function(label){
        		label.attr('id', 'valid');
   		 	},
		});
		
        // NEW USER REGISTRATION
        async function save_register_js(){
			$( "#myform" ).validate({
				rules: {
					password: "required",
					confirm_password: {
						equalTo: "#password"
					}
				},
				messages: {
					txtfullname: {
						required: "Please provide Name"
					},
					txtemail: {
						required: "Please provide Email"
					},
					password: {
						required: "Please provide Password"
					},
					confirm_password: {
						required: "Please provide Password",
						equalTo: "Wrong Password"
					}
				},
				submitHandler: function(form) {
					eel.btn_save($('#txtfullname').val(),$('#txtemail').val(),$('#password').val())
				}
			});
        };
        eel.expose(save_return); 
        function save_return(status){
            if (status == "success"){
                $('#return_register').text('Registration completed successfully!');
                $('#txtfullname').val('');
                $('#txtemail').val('');
                $('#password').val('');
            }
            if (status == "failed"){
                $('#return_register').text('Error when registering, make sure you have no blank fields.');
            }
        };
</script>
</body>
</html>
css file registration\views\css\index.css
//registration\views\css\index.css
body {
	margin:  0;
}
.page-content {
	width: 100%;
	margin:  0 auto;
	background-image: -moz-linear-gradient( 136deg, rgb(224,195,252) 0%, rgb(142,197,252) 100%);
    background-image: -webkit-linear-gradient( 136deg, rgb(224,195,252) 0%, rgb(142,197,252) 100%);
    background-image: -ms-linear-gradient( 136deg, rgb(224,195,252) 0%, rgb(142,197,252) 100%);
	display: flex;
	display: -webkit-flex;
	justify-content: center;
	-o-justify-content: center;
	-ms-justify-content: center;
	-moz-justify-content: center;
	-webkit-justify-content: center;
	align-items: center;
	-o-align-items: center;
	-ms-align-items: center;
	-moz-align-items: center;
	-webkit-align-items: center;
}
.form-v2-content  {
	background: #fff;
	width: 851px;
	border-radius: 15px;
	-o-border-radius: 15px;
	-ms-border-radius: 15px;
	-moz-border-radius: 15px;
	-webkit-border-radius: 15px;
	margin: 100px 0;
	position: relative;
	display: flex;
	display: -webkit-flex;
}
.form-v2-content .form-left {
	margin-bottom: -4px;
	position: relative;
}
.form-v2-content .form-left img {
	border-top-left-radius: 15px;
	border-bottom-left-radius: 15px;
}
.form-v2-content .form-left .text-1,
.form-v2-content .form-left .text-2 {
	font-family: 'Roboto', sans-serif;
	font-weight: 700;
	color: #fff;
}
.form-v2-content .form-left .text-1 {
	position: absolute;
    left: 9%;
    bottom: 15%;
}
.form-v2-content .form-left .text-2 {
	position: absolute;
    right: 8%;
    bottom: 1.5%;
}
.form-v2-content .form-left .text-1 p {
	font-size: 32px;
	line-height: 1.67;
}
.form-v2-content .form-detail {
    padding: 20px 40px 20px 40px;
	position: relative;
	width: 100%;
}
.form-v2-content .form-detail h2 {
	font-family: 'Roboto', sans-serif;
	font-weight: 700;
	color: #333;
	font-size: 28px;
	position: relative;
	padding: 6px 0 0;
	margin-bottom: 25px;
}
.form-v2-content .form-row {
    width: 100%;
    position: relative;
}
.form-v2-content .form-detail label {
	font-family: 'Roboto', sans-serif;
	font-weight: 400;
	font-size: 16px;
	color: #666;
	display: block;
	margin-bottom: 11px;
}
.form-v2-content .form-detail .form-row label#valid {
    position: absolute;
    right: 20px;
    top: 50%;
    transform: translateY(-50%);
    -o-transform: translateY(-50%);
    -moz-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    -webkit-transform: translateY(-50%);
    width: 14px;
    height: 14px;
    border-radius: 50%;
    -o-border-radius: 50%;
    -ms-border-radius: 50%;
    -moz-border-radius: 50%;
    -webkit-border-radius: 50%;
    background: #53c83c;
}
.form-v2-content .form-detail .form-row label#valid::after {
	content: "";
    position: absolute;
    left: 5px;
    top: 1px;
    width: 3px;
    height: 8px;
    border: 1px solid #fff;
    border-width: 0 2px 2px 0;
    -webkit-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    -o-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    transform: rotate(45deg);
}
.form-v2-content .form-detail .form-row label.error {
	padding-left: 0;
	margin-left: 0;
    display: block;
    position: absolute;
    bottom: -10px;
    width: 100%;
    background: none;
    color: red;
}
.form-v2-content .form-detail .input-text {
	margin-bottom: 27px;
}
.form-v2-content .form-detail input {
	width: 91%;
    padding: 14.5px 15px;
    border: 1px solid #e5e5e5;
    border-radius: 5px;
    -o-border-radius: 5px;
    -ms-border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    appearance: unset;
    -moz-appearance: unset;
    -webkit-appearance: unset;
    -o-appearance: unset;
    -ms-appearance: unset;
    outline: none;
    -moz-outline: none;
    -webkit-outline: none;
    -o-outline: none;
    -ms-outline: none;
    font-family: 'Roboto', sans-serif;
    font-size: 16px;
    color: #333;
}
.form-v2-content .form-detail .form-row input:focus {
	border: 1px solid #53c83c;
}
.form-v2-content .form-detail input[type="radio"] {
	outline: none;
    -moz-outline: none;
    -webkit-outline: none;
    -o-outline: none;
    -ms-outline: none;
    -o-appearance: none;
    -ms-appearance: none;
	-webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    display: inline-block;
  	width: 20px;
  	height: 20px;
  	padding:  4px;
  	background-clip: content-box;
  	border: 1px solid #e5e5e5;
  	background-color: #fff;
  	border-radius: 50%;
  	-o-border-radius: 50%;
  	-ms-border-radius: 50%;
  	-moz-border-radius: 50%;
  	-webkit-border-radius: 50%;
  	cursor: pointer;
  	float: left;
  	margin-top: 0;
}
.form-v2-content .form-detail .register {
	background: #3b63ca;
	border-radius: 6px;
	-o-border-radius: 6px;
	-ms-border-radius: 6px;
	-moz-border-radius: 6px;
	-webkit-border-radius: 6px;
	width: 160px;
	border: none;
	margin: 6px 0 50px 0px;
	cursor: pointer;
	font-family: 'Roboto', sans-serif;
	color: #fff;
	font-weight: 500;
	font-size: 16px;
}
.form-v2-content .form-detail .register:hover {
	background: #3356b0;
}
.form-v2-content .form-detail .form-row-last input {
	padding: 15.5px;
}
input::-webkit-input-placeholder { /* Chrome/Opera/Safari */
  color: #999;
  font-size: 14px;
}
input::-moz-placeholder { /* Firefox 19+ */
  color: #999;
  font-size: 14px;
}
input:-ms-input-placeholder { /* IE 10+ */
  color: #999;
  font-size: 14px;
}
input:-moz-placeholder { /* Firefox 18- */
  color: #999;
  font-size: 14px;
}

/* Responsive */
@media screen and (max-width: 991px) {
	.form-v2-content {
		margin: 180px 20px;
		flex-direction:  column;
		-o-flex-direction:  column;
		-ms-flex-direction:  column;
		-moz-flex-direction:  column;
		-webkit-flex-direction:  column;
	}
	.form-v2-content .form-left {
		width: 100%;
	}
	.form-v2-content .form-left img {
		width: 100%;
		border-bottom-left-radius: 0px;
	    border-top-right-radius: 15px;
	}
	.form-v2-content .form-detail {
		padding: 30px 20px 30px 20px;
	    width: auto;
	}
	.form-v2-content .form-detail .form-row input {
		width: 95%;
	}
	.form-v2-content .form-detail .form-checkbox {
		width: 100%;
	}
}
@media screen and (max-width: 575px) {
	.form-v2-content .form-detail .form-row input {
		width: 89.5%;
	}
}

#field { margin-left: .5em; float: left; }
#field, label { float: left; font-family: Arial, Helvetica, sans-serif; font-size: small; }
br { clear: both; }
input { border: 1px solid black; margin-bottom: .5em;  }
input.error { border: 1px solid red; }
label.error {
	background: url('images/unchecked.gif') no-repeat;
	padding-left: 16px;
	margin-left: .3em;
}
label.valid {
	background: url('images/checked.gif') no-repeat;
	display: block;
	width: 16px;
	height: 16px;
}
Run : C:\python_dev\registration>python main.py

Related Post