• flask 项目结构


    flask 项目框架:

    init 内注册 + 初始化
    
    
    

    manage :

    启动文件:
    from flask import Flask
    from flask_script import Manager
    
    from App import create_app
    from flask_migrate import  MigrateCommand
    
    
    app = create_app()
    
    manager = Manager(app=app)
    manager.add_command('db',MigrateCommand)
    
    
    @app.route('/')
    def hello_world():
        return 'Hello World!'
    
    
    if __name__ == '__main__':
        manager.run()
    
    

    App/init:

    from flask import Flask
    
    # 初始化 app
    from App.ext import init_ext
    from App.settings import envs
    from App.views import init_first_blue
    
    
    def create_app():
        app = Flask(__name__)
        # 初始化指定的配置文件
        app.config.from_object(envs.get('develop'))
    
        # 初始化蓝图
        init_first_blue(app)
    
        # 注册扩展库
        init_ext(app)
        return app
    

    App/ext:

    # 拓展模块
    from flask_sqlalchemy import SQLAlchemy
    from flask_migrate import  Migrate
    db = SQLAlchemy()
    
    '''
    数据更新 (迁移操作)
    pip install flask-migrate'''
    migrate = Migrate()
    
    
    
    def init_ext(app):
        # 扩展模块
    
        db.init_app(app)
    
        migrate.init_app(app,db=db)
    
        pass
    

    App/models:

    from App.ext import db
    
    
    class Person(db.Model):
        id = db.Column(db.Integer,primary_key=True,autoincrement=True)
        p_name = db.Column(db.String(16))
        p_age = db.Column(db.Integer,default=18)
        
        
       ---> python manage.py db init
    
    --> python manage.py migrate
    
    --> python manage.py upgrate
    
    -->python manage.py runserver -r -d
    
    (在试图曾调用--》 表)
    
    

    App/setting:

    选择环境
    def get_db_uri(dbinfo):
        ENGINE = dbinfo.get('ENGINE') or 'mysql'
        DRIVER = dbinfo.get('DRIVER') or 'pymysql'
        USER = dbinfo.get('USER') or 'root'
        PASSWORD = dbinfo.get('PASSWORD') or '123.com'
        HOST = dbinfo.get('HOST') or 'localhost'
        PORT = dbinfo.get('PORT') or '3306'
        DB = dbinfo.get('DB') or 'develop'
    
        return "{}+{}://{}:{}@{}:{}/{}".format(ENGINE,DRIVER,USER,PASSWORD,HOST,PORT,DB)
    
    
    # 环境初始化
    class Config:
        DEBUG = False
        TESTING = False
    
        SECRET_KEY = 'Rock'
    
        SQLALCHEMY_TRACK_MOOIFICATIONS = False
    
    
    
    # 每一个环境
    class DevelopConfig(Config):
    
        DEBUG = True
    
        DATABASE ={
            'ENGINE': 'mysql',
            'DRIVER': 'pymysql',
            'USER': 'root',
            'PASSWORD':'123.com',
            'HOST': 'localhost',
            'PORT': '3306',
            'DB': 'test'
        }
    
        SQLALCHEMY_DATABASE_URL = get_db_uri(DATABASE)
    
    
    # 测试环境
    class TestingConfig(Config):
        TESTING  = True
    
        DATABASE = {
            'ENGINE': 'mysql',
            'DRIVER': 'pymysql',
            'USER': 'root',
            'PASSWORD': '123.com',
            'HOST': 'localhost',
            'PORT': '3306',
            'DB': 'test'
        }
    
        SQLALCHEMY_DATABASE_URL = get_db_uri(DATABASE)
    
    
    # 每一个环境
    class StagingConfig(Config):
        DATABASE = {
            'ENGINE': 'mysql',
            'DRIVER': 'pymysql',
            'USER': 'root',
            'PASSWORD': '123.com',
            'HOST': 'localhost',
            'PORT': '3306',
            'DB': 'test'
        }
    
        SQLALCHEMY_DATABASE_URL = get_db_uri(DATABASE)
    
    
    # 每一个环境
    class ProductConfig(Config):
    
        DATABASE = {
            'ENGINE': 'mysql',
            'DRIVER': 'pymysql',
            'USER': 'root',
            'PASSWORD': '123.com',
            'HOST': 'localhost',
            'PORT': '3306',
            'DB': 'test'
        }
    
        SQLALCHEMY_DATABASE_URL = get_db_uri(DATABASE)
    
    
    envs = {
        'develop':DevelopConfig,
        'testing': TestingConfig,
        'staging': StagingConfig,
        'product': ProductConfig,
        'default': DevelopConfig
    }
    

    App/views:

    import random
    
    from flask import Blueprint
    
    from App.ext import db
    from App.models import Person
    
    blue = Blueprint('first_blue',__name__)
    
    def init_first_blue(app):
        # 注册化蓝图
        app.register_blueprint(blueprint=blue)
    
    
    @blue.route('/')
    def index():
        return 'hello world'
    
    
    @blue.route('/index')
    def add_person():
    
        person = Person()
        person.p_name = '你喜欢的%d'% random.randrange(100)
    
        person.p_age = random.randrange(100)
    
        db.session.add(person)
        db.session.commit()
    
        return 'Add success'
    

    拓展:

    py 脚本 --》 py 包 (confact )
    
    
    
  • 相关阅读:
    JMeter中的HTTPS套接字错误
    使用JMETER进行REST API测试(分步指南)
    JMeter BeanShell示例
    未能载入软件包“WebDriverAgentRunner”,因为它已损坏或丢失必要的资源。
    ERROR: Could not connect to lockdownd, error code -19 -20
    (转)Selenium-11: Execute JavaScript with JavascriptExecutor
    在服务器上使用 gradle 打包 android 源码
    poi+properties文件实现多环境测试数据一键切换
    cocos2D-X 常用功能封装
    MD5 AND JSON AND XML
  • 原文地址:https://www.cnblogs.com/shaozheng/p/12934496.html
Copyright © 2020-2023  润新知