I have found it useful to prepare boilerplate code for spinning up things like a new application; in this article, I’ll share some code for prototyping an application that can create, retrieve, update, and delete records from a database using Flask. This walkthrough will assume familiarity with the command line, and that a Python 3 interpreter is available.
For developers with experience with Django, Flask is familiar but with a smaller footprint, and leaves more up to the application architect when it comes to implementation details like form validation and data persistence. As fascinating as design patterns for specialized applications can be, the four “simple” operations listed above - Create, Retrieve, Update, and Delete, commonly abbreviated as CRUD - form the functional core of more than a handful of widely-used applications.
I am able to post this because it is no longer used as a reference implementation by a past and future client, who loved what we were able to build with it. I hope it is of some use to someone starting up or curious about web services in Python. If you need technology strategy, solution design, or project services, contact me.
Prepare Your Environment
To start your prototype, you start by preparing an environment in which the application will run. Using a virtualenv is recommended to allow independent projects isolated dependencies; you can do this using a nice tool like Poetry or as we do here, manually manage the environment. I always encourage reviewing the manual and documentation; with open-source tools, we have the implementation available to answer concrete questions as well!
First, let’s check that pip is available:
python3 -m pip install --upgrade pip
Next, install virtualenv from PyPi - this is optional, but using this version carries some benefits over using the venv
module from the Python Standard Library:
pip3 install virtualenv
The virtual environment lives in a directory you create - it can be more than convenient to keep your code close to important configuration like the necessary environment.
To open a shell in your virtual environment, we first create it and then source
the activation script:
virtualenv crudapp
cd crudapp
source bin/activate
After this, your shell should show the name of the environment you are working in alongside your regular prompt. Package management and execution commands will work pretty much like normal, except using your isolated environment. We will need Flask for this application, so let’s add that:
pip3 install flask
Lastly, whenever you want to do something else, you’ll leave the virtualenv by executing
deactivate
Build Out Your Application
The code included below defines the endpoints, and omits logic to interact with a data store, since it is often specific to the application and resources.
Supposing we copy this code into app.py
, we can test our application by running it in our virtualenv with
python app.py
What topics need more discussion? Send in your feedback!
References and Further Reading
Tutorials on Flask abound, and the active community is a plus.
In a Flask application (as opposed to Django), we are free to work with data however we please. SQLAlchemy is a popular choice, and a oft-used connector is available to create common patterns.
Plenty has been written about virtual environments as well.