Flask Boilerplate

screenshot of Flask Boilerplate
flask

Flask + SqlAlchemy + Docker + PyTest + PyLint + Tox Boilerplate

Overview

This product is a boilerplate repository that aims to facilitate API developers by integrating reputable libraries and following good engineering practices. It is built using Flask, SqlAlchemy, Docker, PyTest, PyLint, and Tox. The repository includes features such as CRUD implementation on a model, environment-specific configuration, logging of web requests and exceptions, and a layered architecture.

Features

  • Flask: a micro web framework for Python
  • SqlAlchemy: an SQL toolkit and Object-Relational Mapping (ORM) library for Python
  • Flask Migrate: a Flask extension for handling database migrations, built on top of Alembic
  • Flask Manager: a Flask extension that adds command line options to the Flask cli
  • Gunicorn: a Python WSGI HTTP server for Unix
  • Psycopg2: a PostgreSQL adapter for Python, used to interact with PostgreSQL instances
  • PyTest: a testing framework for Python, integrated for testing API functionality
  • PyTest-cov: a plugin for PyTest that generates coverage reports
  • PyLint: a source code analyzer for Python, integrated to test code quality
  • Tox: a tool for testing different Python environments, integrated to test the app on different environments
  • Docker: a platform for automating the deployment, scaling, and operation of applications in containers, integrated to dockerize the Flask app

Assumptions:

  • Postgresql instance is up and running
  • Python 3.7+ is installed

Step 1 - Clone repository

git clone https://github.com/uinvent/flask_boilerplate.git

Step 2 - Setup environment configurations

Create a copy of .env.sample and name it as .env. Fill it with your machine configurations. Make sure to configure the APP_DB_* configurations as they lead to your database.

Step 3 - Install project dependencies

Install the libraries used in the project.

Step 4 - Run database migrations

python manage.py init

This will create the database on the server configured in the .env file. Then run:

python manage.py db upgrade

This will run two migrations:

  • The first one will create two sample tables, "person" and "address", where each person can have multiple addresses.
  • The second one will insert records in these tables.

Step 5 - Start server

To start the app server for debugging:

python manage.py runserver -d

This will start the app server and the APIs are ready to be consumed. To start a production-ready server using Gunicorn:

python manage.py runserver

Now the APIs are ready to be consumed on the default URL, http://0.0.0.0:8088

Step 6 - Run Code Tests

To run the tests:

pytest

This command will find all test cases and run them. The tests are available in the /tests/ folder. To see the test coverage report:

pytest --cov=./src/ tests/

To run pylint on the src folder and check for errors only:

pylint src --errors-only

Note that this command uses the .pylintrc file for linting.

Summary

This product is a boilerplate repository that aims to make API development easier by integrating various reputable libraries such as Flask, SqlAlchemy, Docker, PyTest, PyLint, and Tox. It includes features such as CRUD implementation on a model, environment-specific configurations, and a layered architecture. The installation guide provides step-by-step instructions for setting up and running the boilerplate, including cloning the repository, configuring the environment, installing dependencies, running database migrations, starting the server, and running code tests.

flask
Flask

Flask is a lightweight and popular web framework for Python, known for its simplicity and flexibility. It is widely used to build web applications, providing a minimalistic approach to web development with features like routing, templates, and support for extensions.