Contents

Simple Flask App with Mysql

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

Make sure you have installed Python 3 on your device

Project structure

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
* flask-mysql/
  |--- app/
  |    |--- config/
  |    |    |--- __init__.py
  |    |    |--- Database.py
  |    |--- controller/
  |    |    |--- __init__.py
  |    |    |--- CollegerController.py
  |    |    |--- CoursesController.py
  |    |    |--- Main.py
  |    |    |--- TakeCourseController.py
  |    |--- model/
  |    |    |--- CollegerModel.py
  |    |    |--- CoursesModel.py
  |    |    |--- TakeCourseModel.py
  |    |--- templates/
  |    |--- __init__.py
  |--- run.py

Step to create this project

A step by step series of examples that tell you how to get a development env running

  1. Install virtual environment if you dont have virtual environment
1
pip install virtualenv
  1. Create virtual environment and activate inside your flask-rest-api directory according the above structure
1
2
3
virtualenv venv
> On windows -> venv\Scripts\activate
> On linux -> . env/bin/activate
  1. Install some third party libraries on your virtual environment with pip
1
pip install flask flask-sqlalchemy flask-migrate mysql-connector-python
  1. Install MySQL database if you don’t have, but if you have MySQL you can skip this step
  2. Create user and grant privilege for user was created
1
2
3
mysql> CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
mysql> FLUSH PRIVILEGES;
  1. Create database on MySQL
1
mysql> CREATE DATABASE YOUR_DATABASE_NAME
  1. Create project_name/run.py directory inside flask-project according the above structure
1
2
3
4
5
from app import app


if __name__ == "__main__":
    app.run(host="localhost", port=5000, debug=True)
  1. Create project_name/app/config/Database.py to create configuration for database
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class DbConfig:
    def __init__(self):
        self.DB_USERNAME = "<YOUR_USERNAME>"
        self.DB_PASSWORD = "<YOUR_PASSWORD>"
        self.DB_HOST = "localhost"
        self.DB_PORT = 3306
        self.DB_NAME = "<YOUR_DATABASE_NAME>"

    def getUri(self):
        return "mysql+mysqlconnector://{}:{}@{}:{}/{}".format(self.DB_USERNAME, self.DB_PASSWORD, self.DB_HOST,
                                                              self.DB_PORT, self.DB_NAME)
  1. Create project_name/app/__init__.py inside app directory according the above structure project_name/app/. This step will setup for SQLAlchemy config.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from app.config.Database import DbConfig

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = DbConfig().getUri()
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)

migrate = Migrate(app, db)
  1. Define colleger model to application and create database migration, create python file on app/model/ you can see defined model on here
  2. Update app/__init__.py should like as follows
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from app.config.Database import DbConfig

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = DbConfig().getUri()
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)

from app.model import CoursesModel, CollegerModel, TakeCourseModel

migrate = Migrate(app, db)
  1. Run migration with flask-migrate, type in terminal as below
1
2
3
flask db init
flask db migrate
flask db upgrade
  1. The structure of database should like as follows https://raw.githubusercontent.com/piinalpin/flask-mysql/master/docs/dbdiagram.png

  2. Create controller to application, create python file on app/controller/ you can see defined controller here

  3. Update app/__init__.py add this import into end line of file, this step to import the controller

1
2
3
4
from app.controller.Main import *
from app.controller.CollegerController import *
from app.controller.CoursesController import *
from app.controller.TakeCourseController import *
  1. Create templates to application on app/templates/ you can see defined templates here
  2. Then, you can run this application by terminal
1
python run.py
  1. Homepage

https://raw.githubusercontent.com/piinalpin/flask-mysql/master/docs/homepage.png

  1. Colleger Page https://raw.githubusercontent.com/piinalpin/flask-mysql/master/docs/colleger.png

  2. Courses Page https://raw.githubusercontent.com/piinalpin/flask-mysql/master/docs/courses.png

  3. Take Course Page https://raw.githubusercontent.com/piinalpin/flask-mysql/master/docs/take_courses.png

Built With

Clone or Download

You can clone or download this project

1
> Clone : git clone https://github.com/piinalpin/flask-mysql.git

Authors