[Work in progress...]
Simple Python Flask CRUD Application based on Flask-SQLAlchemy. In this application we are connecting a database where we add, retrieve, update and delete users. The app also handles users authentication through json web tokens.
Project is created with:
- FLASK
- Flask-SQLAalchemy: Database ORM
- Flask-Migrate: Database updates
- Flask_Bcrypt: Encrypt passwords
- Flask-Marshmallow: Helps with serializing and deserializing objects. Transform our SQLAlchemy objects into readable JSON data and helps to validate users data inputs.
To run this project clone the project and:
cd ../<project-name>
virtualenv env --python=python3.9 # Recomended
source env/bin/activate # Recomended
pip install -r requirements.txt
python run.py
Flask extension that is used to migrate sqlalchemy based database models. When adding columns, migrate will compare the current version and the new version and create a script to migrate from one to the other. Once you have data it becames very usefull extension.
python migrate.py db init # First time to create unexisting tables
python migrate.py db migrate
python migrate.py db upgrade
benefits:
-
- Stop/avoid circular import;
-
- Better and easier setup, for example, test, different environments;
-
- More organized and readable project.
There are many ways to setup your project folder structure. One is by its function. For instance:
project/
__init__.py
models/
__init__.py
base.py
users.py
posts.py
...
routes/
__init__.py
home.py
account.py
dashboard.py
...
templates/
base.html
post.html
...
services/
__init__.py
google.py
mail.py
...
All things are grouped by its function. If it hehaves as a model, put it in models folder; if it behaves as a route, put it in routes folder. Build a create_app factory in project/init.py, and init_app of everything: