Migrating from Flask-Script to Flask CLI on Flask 1.0

Jorge Escobar
2 min readMay 23, 2018

--

With Flask 1.0 there was a big change on how we interact with the application management, with the integration of the Click library in Flask, which replaces in many aspects the Flask-Script library. Now all interactions with Flask happens through a Flask CLI that works with the flask command on the terminal.

Flask 1.0 also introduced tight integration with python-dotenv which allows you to handle environment variables and settings easier.

So I decided to update my MySQL Boilerplate application with these new enhancements so that we’re future proof with the new direction Flask 1.0 is taking.

If you haven’t checked it out, you can see the original version of the repository here.

First we’ll update our libraries in requirements.txt, mainly to update to Flask 1.0.2. You can install just the root packages, or to be extra-safe, use all the versioned libraries.

### Root Packages
# Flask==1.0.2
# python-dotenv==0.8.2
# PyMySQL==0.8.1
# Flask-SQLAlchemy
# Flask-Migrate==2.1.1
alembic==0.9.9
click==6.7
Flask==1.0.2
Flask-Migrate==2.1.1
Flask-SQLAlchemy==2.3.2
itsdangerous==0.24
Jinja2==2.10
Mako==1.0.7
MarkupSafe==1.0
PyMySQL==0.8.1
python-dateutil==2.7.3
python-dotenv==0.8.2
python-editor==1.0.3
six==1.11.0
SQLAlchemy==1.2.7
Werkzeug==0.14.1

Now create the database and its user, per the README.md instructions, as well as the virtualenv and install the packages. Notice that with python 3.6 we no longer have to install virtualenv. It’s built-in in Python, so to create the virtualenv you can just do python3 -m venv venv.

We’re now going to change manage.py greatly, since Flask-Migrate supports the new Flask CLI and we want to keep our usage of a factory pattern. So all we need is:

from application import create_app
app = create_app()

Now we’re going to create the .flaskenv settings file. I don’t need the envvars file so I’ll delete it now.

The .flaskenv file has the new FLASK_APP variable that tells the Flask CLI which application to run as well as the FLASK_ENV variable which tells Flask to run this app with development settings turned on which includes DEBUG mode turned on.

Now we need to initialize the database. First we run flask db init which creates the migrations folder. Next do flask db migrate to create the first version and flask db upgrade to actually write the tables on the database.

Finally to run the application do flask run and check on your http://localhost:5000.

If you want access to the shell, you can do: flask shell. Then we can interact with the models as before:

>>> from counter.models import Counter
>>> Counter.query.all()
[<Count 3>]

We’re almost there. Now we need to add dotenv to our tests. So add the new pathlib library and then this snippet:

from dotenv import load_dotenv
env_dir = pathlib.Path(__file__).parents[1]
load_dotenv(os.path.join(env_dir, '.flaskenv'))

This will load the environment variables in our .flaskenv file and make them available to our tests.py.

Last but not least, we’ll update the Dockerfile to now use the flask run command with the specific host of 0.0.0.0 so that we can reach it from the host computer:

flask run --host 0.0.0.0

The final version of the boilerplate is here.

--

--

Jorge Escobar

Technologist with both startup and enterprise leadership experience. Course maker @fromzeroedu.