Skip to content

albertyw/base-flask

Repository files navigation

Base Flask Setup

Build Status Code Climate Test Coverage Varsnap Status

This is a base Flask/Gunicorn/nginx setup, useful as a skeleton for building simple web apps from. This is meant to be used for websites, as opposed to pure JSON sites which would be better developed using a different language or framework (e.g. sanic).

Features

  • Developed specifically for Python 3
  • Minimal but up-to-date python dependencies
  • 100% test coverage
  • No issues from Code Climate
  • PEP8 compliant
  • Documented setup and usage procedures
  • Includes setting up secure and fast nginx/gunicorn in production
  • Docker support

Users

These repositories are based on base-flask:

Setup

Modify and run baseflask/scaffold.sh to create a copy of base-flask for your project needs, then run bin/setup.sh to set up the host.

Base-flask is designed to work with a minimally sized AWS EC2 or DigitalOcean instance, running a modern version of Ubuntu (20.04 LTS recommended).

CDN

A CDN like cloudflare can also be added on to boost speed. A good cloudflare setup would be to use full SSL encryption and to install an origin certificate so that requests are encrypted end-to-end.

Development

Setup

Using virtualenvwrapper and optionally direnv

mkvirtualenv app -p python3.12  # This repository uses baseflask as the name of the virtualenv
pip install -e .[test]
ln -s .env.development .env
npm install

# Install shellcheck
# brew install shellcheck
# sudo apt-get install shellcheck

Spinning up the server:

npm run build:dev
python app/serve.py

Running tests:

ruff check .
mypy .
shellcheck --exclude=SC1091 bin/*.sh
coverage run -m unittest discover
npm test

CI/CD

This repo uses:

# Switch to python 3
pyenv local 3.12
pip install -e .[test]
ln -s .env.development .env

# Test
ruff check .
mypy .
shellcheck --exclude=SC1091 bin/*.sh
coverage run -m unittest discover
coverage report
codeclimate-test-reporter
npm test

# Deployment
ssh example.com website/bin/deploy.sh

Building and starting the docker container

docker build -t $PROJECT_NAME:test .
docker run -t -i -p 127.0.0.1:$INTERNAL_PORT:5000 $PROJECT_NAME:test

Production

Setup

Run this once on a new server to set up the web app:

bin/setup.sh

Deployment

Run this every time for a new commit to the repository:

bin/deploy.sh