Profile Application using Python Flask and MySQL


Flask is one of the web frameworks which provide libraries to build light weight web applications in python. It is a micro framework, developed by Armin Ronacher who runs an international group of python enthusiasts (POCCO). Flask works on WSGI toolkit and jinja2 template engines.

For creating the profile application using python flask and MySQL we have to follow the below mentioned steps one by one.

Step − 1

Install the virtual environment by executing the below mentioned command in the command prompt.

pip install virtualenv

Once the virtual environment is created we can create the new virtual environment into a folder.

mkdir Flask
cd Flask
virtualenv venv

After creating the virtual environment, we have to activate the corresponding virtual environment by using the below commands based on the operating system that we use.

In Linux operating system to activate the virtual environment the below is executed in the shell.

venv/bin/activate

In windows operating system to activate the virtual environment the following command need to be executed in the command prompt.

venv\scripts\activate 

Step − 2

In this step we have to install the flask web framework into our python environment by executing the below command.

pip install flask

Next we have to install the MySQL workbench and mysqldb by using the following link and command.

https://dev.mysql.com/downloads/workbench/
pip install flask-mysqldb

Step − 3

Now create the database in the MySql with the name tutorialspointprofile and add the specifications to the profile elements.

create database tutorialspointprofile;
SELECT * FROM accounts; 
create table if not exists accounts( id int(11) not null auto_increment, 
username varchar(50) not null, 
password varchar(255) not null, 
email varchar(100) not null, 
organisation varchar(100) not null, 
address varchar(100) not null, 
city varchar(100) not null, 
state varchar(100) not null, 
country varchar(100) not null, 
postalcode varchar(100) not null, 
primary key (id) ) engine=InnoDB auto_increment=1 default char set=utf8;

Step − 4

Now we have to create the app.py file in the folder flask and enter the below mentioned code to work along with the mysql database and specifications.

from flask import Flask, render_template, request, redirect, url_for, session
from flask_mysqldb import MySQL
import MySQLdb.cursors
import re
app = Flask(__name__)
app.secret_key = '\xec\x11\xf1\xab\xa9\xf2\x01-F\xd6\xb2wR(\xe4'
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = '$Naavya01?'
app.config['MYSQL_DB'] = 'tutorialspointprofile'
mysql = MySQL(app)
@app.route('/register', methods =['GET', 'POST'])
def register():
   msg = ''
   if request.method == 'POST' and 'email' in request.form and 'address' in request.form and 'city' in request.form and 'country' in request.form and 'postalcode' in request.form and 'organisation' in request.form:
      email = request.form['email']
      organisation = request.form['organisation']
      address = request.form['address']
	  city = request.form['city']
      state = request.form['state']
      country = request.form['country']	
      postalcode = request.form['postalcode']
      cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
      elif not re.match(r'[^@]+@[^@]+\.[^@]+', email):
         msg = 'Invalid email address !'
      elif not re.match(r'[A-Za-z0-9]+', username):
         msg = 'name must contain only characters and numbers !'
      else:
         cursor.execute('INSERT INTO accounts VALUES (NULL, % s, % s, % s, % s, % s, % s, % s, % s, % s)', (email, organisation, address, city, state, country, postalcode, ))
         mysql.connection.commit()
         msg = 'Successfully registered !'
   elif request.method == 'POST':
      msg = 'Please fill out the form !'
   return render_template('register.html', msg = msg)
if __name__ == "__main__":
   app.run(host ="localhost", port = int("5000"))

Step − 5

Now we have created templates folder and in that folder create the html form to make the user to get register the details in the database of MySQL.

<html>
   <head>
      <meta charset="UTF-8">
      <title> register </title>
   </head>
   <body>
      <div class="registercontent" align="center">
         <div class="registertop">
            <h1>Register</h1>
         </div>
         </br>
         </br>
         <div class="registerbottom">
            <form action="{{ url_for('register')}}" method="post" autocomplete="off">
               <div class="msg">{{ msg }}</div>
               <input type="email" name="email" placeholder="Enter Your Email ID" class="textbox" id="email" required>
               <input type="text" name="organisation" placeholder="Enter Your Organisation" class="textbox" id="organisation" required>
               <input type="text" name="address" placeholder="Enter Your Address" class="textbox" id="address" required>
               <input type="text" name="city" placeholder="Enter Your City" class="textbox" id="city" required>
               <input type="text" name="state" placeholder="Enter Your State" class="textbox" id="state" required>
               <input type="text" name="country" placeholder="Enter Your Country" class="textbox" id="country" required>
			   <input type="text" name="postalcode" placeholder="Enter Your Postal Code" class="textbox" id="postalcode" required>
			   <input type="submit" class="btn" value="Register">
            </form>
         </div>
      </div>
   </body>
</html>

Step − 6

Finally, we have to run the project server and open the localhost webpage to view the registration form and whatever the data we entered using the registration form they will be updated in the MYSQL tutorialspointprofile database

Updated on: 19-Oct-2023

87 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements