This is a quick template which help you build up backend with Flask + MySQL and handle config and API functions clearly.
-
Clone the project
$ git clone https://github.com/liuyuweitarek/Flask_MySQL_QuickTemplate.git $ cd Flask_MySQL_QuickTemplate
-
Build Project Python Environment
Python Version > 3
- Set up/Start up virtual env
$ python3 -m venv env $ source env/bin/activate
- Install Dependencies
$ python -m pip install --upgrade pip $ pip install -r requirements.txt
- Set up/Start up virtual env
-
Set up MySQL Environment
-
MySQL Installation Guideline: Follow the instructions based on your OS system.
-
Create the database for this project
$ mysql -u root -p (input password) $ mysql> CREATE DATABASE {DATABASENAME}_dev ; $ mysql> CREATE DATABASE {DATABASENAME}_prod ;
If you are not root, you could ask your admin for creating databases and granting the privileges to you with adding the commands below.
(In root account) $ mysql> GRANT ALL PRIVILEGES ON {DATABASENAME}_dev.* TO '{ACCOUNT}'@'%'; $ mysql> GRANT ALL PRIVILEGES ON {DATABASENAME}_prod.* TO '{ACCOUNT}'@'%';
P.S. If the error shows that it can't find the user, using the code down below to check your account identity
$ SELECT User FROM mysql.user; # It shows like: +------------------+--------------+--------------+ | user | host | password | +------------------+--------------+--------------+ | {ACCOUNT} | localhost | 37as%#8123fs | | Other_user | % | slclk2j393 | +------------------+--------------+--------------+ 1 rows in set (0.01 sec)
Based on the columns, "user" and "host", on User table, you might need to change the commands to :
(In root account) $ mysql> GRANT ALL PRIVILEGES ON {DATABASENAME}_dev.* TO '{ACCOUNT}'@'localhost'; $ mysql> GRANT ALL PRIVILEGES ON {DATABASENAME}_prod.* TO '{ACCOUNT}'@'localhost';
-
-
Modify cfg.py's config
class ProductionConfig: ... HOST = {YOUR IP ADDRESS} <= Edit PORT = {PORT which is available on your machine} <= Edit ... # SQL SQLALCHEMY_DATABASE_URI= "mysql://{ACCOUNT}:{PASSWORD}@127.0.0.1/{DATABASENAME}_prod" <= Edit ... class DevelopmentConfig: ... HOST = {YOUR IP ADDRESS} <= Edit PORT = {PORT which is available on your machine} <= Edit ... # SQL SQLALCHEMY_DATABASE_URI= "mysql://{ACCOUNT}:{PASSWORD}@127.0.0.1/{DATABASE_NAME}_dev" <= Edit ...
- Make sure that MySQL Service active correctly
$ service mysql status
- Activate project python environment
$ cd ~/Flask_MySQL_QuickTemplate $ source env/bin/activate $ export PYTHONPATH=src
- Run
$ python run.py --config [dev, prod]
[ GET API ] : {YOUR_IP_ADDRESS}:{PORT}/GET/dev_or_prod
[ POST API ] : {YOUR_IP_ADDRESS}:{PORT}/POST/storedata