1. 目录介绍
app------>后台管理界面
config------>flask配置文件
model------>连接数据库表,相当于将mysql中表里的字段一一对应的写入,不需要迁移
service------>创建属性,是否可增删改
util------>加载数据库配置及用sqlalchemy创建连接对象
2. 代码
app—>app.py
from flask_admin import Admin
from flask import Flask
from util import db_util
from service.save_blank import BView
app = Flask(__name__)
admin = Admin(app)
admin.add_view(BView(db_util.get_test_session(),name = u'学生表'))
if __name__ == '__main__':
app.run()
config—>dev—>env.ini
[db_test]
mysql.host = 127.0.0.1
mysql.port = 3306
mysql.dbname = db_test
mysql.user = root
mysql.password = 123456
mysql.pool.size = 10
mysql.pool.timeout = 36000
mysql.pool.recycle = 60
model—>db_test.py
from sqlalchemy import Column, Date, DateTime, JSON, String, TIMESTAMP, Table, text
from sqlalchemy.dialects.mysql import BIGINT, INTEGER, MEDIUMTEXT, TINYINT, TINYTEXT
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class Student(Base):
__tablename__ = 'student'
sid = Column(INTEGER(11),primary_key=True)
sname = Column(String(255))
score = Column(INTEGER(11))
joindate = Column(Date)
service—>save_blank.py
from model.db_test import Student
from flask_admin.contrib.sqla import ModelView
class BView(ModelView):
# 常用属性
can_create = True # 是否创建
can_edit = True # 是否编辑
can_delete = True # 是否删
column_labels = {"sname": "姓名"}
def __init__(self, session, **kwargs):
super(BView, self).__init__(Student, session, **kwargs)
util—>config_util.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
import configparser
parser = configparser.ConfigParser()
package = os.environ.get('package', 'dev')
config_file = os.path.join(BASE_DIR, 'config' + os.path.sep + package + os.path.sep + 'env.ini')
parser.read(config_file)
# db_test, 读取ini配置文件中的数据库配置
def get_db_test_mysql_host():
return parser.get('db_test', 'mysql.host').strip()
def get_db_test_mysql_port():
port = parser.get('db_test', 'mysql.port').strip()
return int(port)
def get_db_test_mysql_dbname():
return parser.get('db_test', 'mysql.dbname').strip()
def get_db_test_mysql_user():
return parser.get('db_test', 'mysql.user').strip()
def get_db_test_mysql_password():
return parser.get('db_test', 'mysql.password').strip()
def get_db_test_mysql_pool_size():
pool_size = parser.get('db_test', 'mysql.pool.size').strip()
return int(pool_size)
def get_db_test_mysql_pool_timeout():
pool_timeout = parser.get('db_test', 'mysql.pool.timeout').strip()
return int(pool_timeout)
def get_db_test_mysql_pool_recycle():
pool_recycle = parser.get('db_test', 'mysql.pool.recycle').strip()
return int(pool_recycle)
util—>db_util.py
import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from util import config_util
from sqlalchemy import create_engine # 用sqlalchemy创建连接对象
from sqlalchemy.orm import sessionmaker # 用sqlalchemy创建连接对象
# 实例化
db_test_db_host = config_util.get_db_test_mysql_host()
db_test_db_port = config_util.get_db_test_mysql_port()
db_test_db_name = config_util.get_db_test_mysql_dbname()
db_test_username = config_util.get_db_test_mysql_user()
db_test_password = config_util.get_db_test_mysql_password()
db_test_pool_size = config_util.get_db_test_mysql_pool_size()
db_test_pool_timeout = config_util.get_db_test_mysql_pool_timeout()
db_test_pool_recycle = config_util.get_db_test_mysql_pool_recycle()
# 连接数据库,创建session对象
def get_test_session():
engine = create_engine(
"mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8mb4".format(db_test_username,
db_test_password,
db_test_db_host,
db_test_db_port,
db_test_db_name),
encoding="utf-8",
pool_size=db_test_pool_size,
pool_timeout=db_test_pool_timeout,
pool_recycle=db_test_pool_recycle,
echo=False)
Session = sessionmaker(bind=engine)
session = Session()
return session
3. 运行代码
运行app.py里的app.run()
效果如下:
版权声明:本文为lxp_mocheng原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。