All the environment setup are operated under windows system
The overall project file structure will be this:
- Create the python virtual environment:
create the virtual environment call env, this will create a folder call env in your current directory
python -m venv env
- Install the dependent packages we need for the flask restful api project:
Flask==1.0.2 # the main flask package
Flask-API==1.0 #additional package for api style Flask-SQLAlchemy==2.3.2 # the package that used to provide CRUD operations between flask and database
SQLAlchemy==1.2.8 #the underlying Python SQL toolki that required by flask-sqlalchemy
sqlacodegen==2.0.0 # the package we use to reverse the database to python classes
mysql-connector-python==8.0.11 # the driver for python to connect mysql database
flasgger==0.9.0 # the flask swagger api docs
requests==2.18.4 # the package that python use to send http request to graph db
geopy==1.14.0 # the package used for geocoding
(all the other extra packages are automatically install as dependency)
First time I create this project, all this packages are installed manually. For the later development, we can export all the installed packages to txt file,
pip freeze > requirements.txt
next time other teammates want to setup the environment, could just run this command to read this txt file to install all the packages:
pip install -r requirements.txt
- The Flask configuration
For the flask environment variable setup, as we are using windows and virtual environment, simply useset variable = values
will not work as this variable will only live in this command line session, and will not be found if we close and start the project again, thus we will create a configuration file for the flask project to load the configuration
The configuration variables files (sysenv.ini). and the file under the instance folder
[DEFAULT]
APP_SETTINGS = development
DATABASE_URL = mysql+mysqlconnector://[username]:[password]@localhost/snapmytalent
SECRET = kevinwkds
After creating the configuration variable files, then create the configuration loading python file:
import os
import configparser
config = configparser.ConfigParser()
config.read('C:\\Users\\kevin\\Documents\\smtbackend(flask)\\instance\\sysenv.ini')
secret = config['DEFAULT']['SECRET']
database_url = config['DEFAULT']['DATABASE_URL']
class Config(object):
"""Parent configuration class."""
DEBUG = False
CSRF_ENABLED = True
SECRET = secret
SQLALCHEMY_DATABASE_URI = database_url
class DevelopmentConfig(Config):
"""Configurations for Development."""
DEBUG = True
class TestingConfig(Config):
"""Configurations for Testing, with a separate test database."""
TESTING = True
SQLALCHEMY_DATABASE_URI = database_url
DEBUG = True
class StagingConfig(Config):
"""Configurations for Staging."""
DEBUG = True
class ProductionConfig(Config):
"""Configurations for Production."""
DEBUG = False
TESTING = False
app_config = {
'development': DevelopmentConfig,
'testing': TestingConfig,
'staging': StagingConfig,
'production': ProductionConfig,
}
- Create the flask app
Once we finish the flask configuration setup, then we could use these configuration to start creating the flask app entry point: