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
- Install virtual environment if you dont have virtual environment
- 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
|
- Install some third party libraries on your virtual environment with pip
1
| pip install flask flask-sqlalchemy flask-migrate mysql-connector-python
|
- Install MySQL database if you don’t have, but if you have MySQL you can skip this step
- 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;
|
- Create database on MySQL
1
| mysql> CREATE DATABASE YOUR_DATABASE_NAME
|
- 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)
|
- 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)
|
- 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)
|
- Define colleger model to application and create database migration, create python file on
app/model/
you can see defined model on here - 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)
|
- Run migration with flask-migrate, type in terminal as below
1
2
3
| flask db init
flask db migrate
flask db upgrade
|
The structure of database should like as follows
Create controller to application, create python file on app/controller/
you can see defined controller here
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 *
|
- Create templates to application on
app/templates/
you can see defined templates here - Then, you can run this application by terminal
- Homepage
Colleger Page
Courses Page
Take Course Page
Built With
Clone or Download
You can clone or download this project
1
| > Clone : git clone https://github.com/piinalpin/flask-mysql.git
|
Authors