美文网首页
Python Flask Project Setup

Python Flask Project Setup

作者: KevinWu12 | 来源:发表于2018-10-18 08:28 被阅读0次

    All the environment setup are operated under windows system
    The overall project file structure will be this:

    strye.PNG
    dfure.PNG
    1. 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
    
    1. 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
    
    1. The Flask configuration
      For the flask environment variable setup, as we are using windows and virtual environment, simply use set 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,
    }
    
    1. 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:

    相关文章

      网友评论

          本文标题:Python Flask Project Setup

          本文链接:https://www.haomeiwen.com/subject/vgtteftx.html