有一需求:Web 框架用 Flask 在同一个项目内(例:Project)目录结构如下
myProject
|- app1
|- app2
|- Flask-app
app1 是一个简单爬虫, app2 是一个数据分析,都不是 Flask 应用,能不能定义一组 Model 使这三个 app 都共用这一组 model,并且使项目结构如下:
myProject
|- app1
|- app2
|- Flask-app
|- models
P.S. 已用 Flask-SQLalchemy
Yes, the simplest way is, sys.path.append('the directory where models are located'), and then you can import
If multiple frameworks are mixed, use pure sqlalchemy instead of flask-sqlalchemy.
1. Create a
initdb.py
in the project root directory, which is your myProject directory, and the user initializes SQLAlchemy, for example:2. Similarly, create a shared
application.py
file in the root directory, which is used to create apps, for example:3. Create a
__init__.py
file in each individual app to initialize the app, for example:4. Create a
app.py
file in each separate app to start the separate app, for example:5. You can create a
models
directory in the root directory to store all database models.6. When using it in the view, you only need to enter the required
import
.