Creating a Web App From Scratch Using Python Flask and MySQL

In this series, we’ll be using Python, Flask and MySQL to create a simple web application from scratch. It will be a simple bucket list application where users can register, sign in and create their bucket list.

This tutorial assumes that you have some basic knowledge of the Python programming language. We’ll be using Flask, a Python web application framework, to create our application, with MySQL as the back end.

Introduction to Python Flask

Flask is a Python framework for creating web applications. From the official site,

Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions.

When we think about Python, the de facto framework that comes to our mind is the Django framework. But from a Python beginner’s perspective, Flask is easier to get started with, when compared to Django.

1. Setting Up Flask

Setting up Flask is pretty simple and quick. With pip package manager, all we need to do is: 

Once you’re done with installing Flask, create a folder called FlaskApp. Navigate to the FlaskApp folder and create a file called app.py. Import the flask module and create an app using Flask as shown:

Now define the basic route / and its corresponding request handler:

Next, check if the executed file is the main program and run the app:

Save the changes and execute app.py:

Point your browser to http://localhost:5000/ and you should have the welcome message.

2. Creating a Home Page

First, when the application runs, we should show a home page with the latest bucket list items added by users. So, let’s add our home page to our application folder.

Flask looks for template files inside the templates folder. So, navigate to the FlaskApp folder and create a folder called templates. Inside templates, create a file called index.html. Open up index.html and add the following HTML:

Open up app.py and import render_template, which we’ll use to render the template files.

Modify the main method to return the rendered template file.

Save the changes and restart the server. Point your browser to http://localhost:5000/ and you should have the below screen:

Flask App homepageFlask App homepageFlask App homepage

3. Creating a Signup Page

Step 1

Setting Up the Database

We’ll be using MySQL as the back end. So, log into MySQL from the command line, or if you prefer a GUI like MySQL work bench, you can use that too. First, create a database called BucketList. From the command line: 

Enter the required password and when logged in, execute the following command to create the database:

Once the database has been created, create a table called tbl_user as shown:

We’ll be using Stored procedures for our Python application to interact with the MySQL database. So, once the table tbl_user has been created, create a stored procedure called sp_createUser to sign up a user.

When creating a stored procedure to create a user in the tbl_user table, first we need to check if a user with the same username already exists. If it exists, we need to throw an error to the user, otherwise we’ll create the user in the user table. Here is how the stored procedure sp_createUser would look:

Step 2

Create a Signup interface

Navigate to the FlaskApp/templates directory and create an HTML file called signup.html. Add the following HTML code to signup.html:

Also add the following CSS as signup.css to the static folder inside FlaskApp.

In app.py add another method called signup to render the signup page once a request comes to /signup:

Save the changes and restart the server. Click on the Sign Up button on the home page and you should have the signup page as shown: 

Flask App signup pageFlask App signup pageFlask App signup page

Step 3

Implement a Signup method 

Next, we need a server-side method for the UI to interact with the MySQL database. So, navigate to FlaskApp and open app.py. Create a new method called signUp and also add a route /api/signup. Here is how it looks:

We’ll be using AJAX to post our signup data to the signup method, so we need to specify the method in the route definition.

In order to read the posted values we need to import request from Flask.

Using request we can read the posted values as shown below:

Once the values are read, we’ll simply check if they are valid and for the time being let’s just return a simple message:

Also import json from Flask, since we are using it in the above code to return json data.

Step 4

Create a Signup request

We’ll be using the fetch() API to send the signup request to the Python method. So, let’s attach the signup button click event as shown:

You should put this script inside a <script> tag at the bottom of the signup document. Save all the changes and restart the server. From the Sign Up page, fill in the details and click Sign Up. Check the browser console and you should have the below message:

Step 5

Call the MySQL Stored Procedure 

Once we have the name, email address and password, we can simply call the MySQL stored procedure to create the new user.

To connect with MySQL, we’ll be using Flask-MySQL, which is a Flask extension. In order to get started with Flask-MySQL, install it using pip package manager:

Import MySQL inside app.py:

Earlier we defined our app as shown:

Along with that include the following MySQL configurations:

First, let’s create the MySQL connection:

Once the connection is created, we’ll require a cursor to query our stored procedure. So, using conn connection, create a cursor.

Before calling the create user stored procedure, let’s make our password salted using a helper provided by Werkzeug. Import the module into app.py:

Use the salting module to create the hashed password.

Now, let’s call the procedure sp_createUser:

If the procedure is executed successfully, then we’ll commit the changes and return the success message. 

Save the changes and restart the server. Go to the signup page and enter the name, email address and password and click the Sign Up button. On successful user creation, you’ll be able to see a message in your browser console.

Wrapping It Up

In this tutorial, we saw how to get started with creating a web application using Python Flask, MySQL and the Flask-MySQL extension. We created and designed the database tables and stored procedure and implemented the signup functionality. In the next tutorial, we’ll take this series to the next level by implementing sign-in functionality and some other features.

Source code for this tutorial is available on GitHub.

This post has been updated with contributions from Jacob Jackson. Jacob is a web developer, technical writer, a freelancer, and an open-source contributor.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Jay

In this series, we'll be using Python, Flask and MySQL to create a simple web application from scratch. It will be a simple bucket list application where users can register, sign in and create their bucket list.

This tutorial assumes that you have some basic knowledge of the Python programming language. We'll be using Flask, a Python web application framework, to create our application, with MySQL as the back end.

Introduction to Python Flask

Flask is a Python framework for creating web applications. From the official site,

Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions.

When we think about Python, the de facto framework that comes to our mind is the Django framework. But from a Python beginner's perspective, Flask is easier to get started with, when compared to Django.

1. Setting Up Flask

Setting up Flask is pretty simple and quick. With pip package manager, all we need to do is: 

Once you're done with installing Flask, create a folder called FlaskApp. Navigate to the FlaskApp folder and create a file called app.py. Import the flask module and create an app using Flask as shown:

Now define the basic route / and its corresponding request handler:

Next, check if the executed file is the main program and run the app:

Save the changes and execute app.py:

Point your browser to http://localhost:5000/ and you should have the welcome message.

2. Creating a Home Page

First, when the application runs, we should show a home page with the latest bucket list items added by users. So, let's add our home page to our application folder.

Flask looks for template files inside the templates folder. So, navigate to the FlaskApp folder and create a folder called templates. Inside templates, create a file called index.html. Open up index.html and add the following HTML:

Open up app.py and import render_template, which we'll use to render the template files.

Modify the main method to return the rendered template file.

Save the changes and restart the server. Point your browser to http://localhost:5000/ and you should have the below screen:

Flask App homepageFlask App homepageFlask App homepage

3. Creating a Signup Page

Step 1

Setting Up the Database

We'll be using MySQL as the back end. So, log into MySQL from the command line, or if you prefer a GUI like MySQL work bench, you can use that too. First, create a database called BucketList. From the command line: 

Enter the required password and when logged in, execute the following command to create the database:

Once the database has been created, create a table called tbl_user as shown:

We'll be using Stored procedures for our Python application to interact with the MySQL database. So, once the table tbl_user has been created, create a stored procedure called sp_createUser to sign up a user.

When creating a stored procedure to create a user in the tbl_user table, first we need to check if a user with the same username already exists. If it exists, we need to throw an error to the user, otherwise we'll create the user in the user table. Here is how the stored procedure sp_createUser would look:

Step 2

Create a Signup interface

Navigate to the FlaskApp/templates directory and create an HTML file called signup.html. Add the following HTML code to signup.html:

Also add the following CSS as signup.css to the static folder inside FlaskApp.

In app.py add another method called signup to render the signup page once a request comes to /signup:

Save the changes and restart the server. Click on the Sign Up button on the home page and you should have the signup page as shown: 

Flask App signup pageFlask App signup pageFlask App signup page

Step 3

Implement a Signup method 

Next, we need a server-side method for the UI to interact with the MySQL database. So, navigate to FlaskApp and open app.py. Create a new method called signUp and also add a route /api/signup. Here is how it looks:

We'll be using AJAX to post our signup data to the signup method, so we need to specify the method in the route definition.

In order to read the posted values we need to import request from Flask.

Using request we can read the posted values as shown below:

Once the values are read, we'll simply check if they are valid and for the time being let's just return a simple message:

Also import json from Flask, since we are using it in the above code to return json data.

Step 4

Create a Signup request

We'll be using the fetch() API to send the signup request to the Python method. So, let's attach the signup button click event as shown:

You should put this script inside a <script> tag at the bottom of the signup document. Save all the changes and restart the server. From the Sign Up page, fill in the details and click Sign Up. Check the browser console and you should have the below message:

Step 5

Call the MySQL Stored Procedure 

Once we have the name, email address and password, we can simply call the MySQL stored procedure to create the new user.

To connect with MySQL, we'll be using Flask-MySQL, which is a Flask extension. In order to get started with Flask-MySQL, install it using pip package manager:

Import MySQL inside app.py:

Earlier we defined our app as shown:

Along with that include the following MySQL configurations:

First, let's create the MySQL connection:

Once the connection is created, we'll require a cursor to query our stored procedure. So, using conn connection, create a cursor.

Before calling the create user stored procedure, let's make our password salted using a helper provided by Werkzeug. Import the module into app.py:

Use the salting module to create the hashed password.

Now, let's call the procedure sp_createUser:

If the procedure is executed successfully, then we'll commit the changes and return the success message. 

Save the changes and restart the server. Go to the signup page and enter the name, email address and password and click the Sign Up button. On successful user creation, you'll be able to see a message in your browser console.

Wrapping It Up

In this tutorial, we saw how to get started with creating a web application using Python Flask, MySQL and the Flask-MySQL extension. We created and designed the database tables and stored procedure and implemented the signup functionality. In the next tutorial, we'll take this series to the next level by implementing sign-in functionality and some other features.

Source code for this tutorial is available on GitHub.

This post has been updated with contributions from Jacob Jackson. Jacob is a web developer, technical writer, a freelancer, and an open-source contributor.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Jay


Print Share Comment Cite Upload Translate Updates
APA

Jay | Sciencx (2015-01-03T19:05:29+00:00) Creating a Web App From Scratch Using Python Flask and MySQL. Retrieved from https://www.scien.cx/2015/01/03/creating-a-web-app-from-scratch-using-python-flask-and-mysql/

MLA
" » Creating a Web App From Scratch Using Python Flask and MySQL." Jay | Sciencx - Saturday January 3, 2015, https://www.scien.cx/2015/01/03/creating-a-web-app-from-scratch-using-python-flask-and-mysql/
HARVARD
Jay | Sciencx Saturday January 3, 2015 » Creating a Web App From Scratch Using Python Flask and MySQL., viewed ,<https://www.scien.cx/2015/01/03/creating-a-web-app-from-scratch-using-python-flask-and-mysql/>
VANCOUVER
Jay | Sciencx - » Creating a Web App From Scratch Using Python Flask and MySQL. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2015/01/03/creating-a-web-app-from-scratch-using-python-flask-and-mysql/
CHICAGO
" » Creating a Web App From Scratch Using Python Flask and MySQL." Jay | Sciencx - Accessed . https://www.scien.cx/2015/01/03/creating-a-web-app-from-scratch-using-python-flask-and-mysql/
IEEE
" » Creating a Web App From Scratch Using Python Flask and MySQL." Jay | Sciencx [Online]. Available: https://www.scien.cx/2015/01/03/creating-a-web-app-from-scratch-using-python-flask-and-mysql/. [Accessed: ]
rf:citation
» Creating a Web App From Scratch Using Python Flask and MySQL | Jay | Sciencx | https://www.scien.cx/2015/01/03/creating-a-web-app-from-scratch-using-python-flask-and-mysql/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.