Skip to content

ruanbekker/flask-mysql-guestbook

Repository files navigation

Python Flask with MySQL

Basic Flask app that lets you store records to MySQL, search from MySQL and also display the number of records on the MySQL Table.

Say Thanks!

Setup Dependencies:

This requires python and pip, or docker if you are using docker, or a kubernetes cluster, if you want to deploy to kubernetes

Download, Install Requirements:

git clone https://github.com/ruanbekker/flask-mysql-guestbook
cd flask-mysql-guestbook
virtualenv venv
source venv/bin/activate
pip install -r requirements.txt

MySQL Configuration:

For non docker or kubernetes deployments, the MySQL URi is defined with the environment variables:

MYSQL_USER
MYSQL_PASSWORD
MYSQL_HOST
MYSQL_DATABASE

Populate MySQL Table:

For non containerized deployments, you can populate the tables like this:

python
>>> from application import db
>>> db.create_all()

Run Application:

For non containerized deployments, you can run the application like this:

$ python application.py

Docker

Build and run:

$ docker-compose up --build -d

For database migrations:

$ docker-compose exec app /bin/sh -c 'FLASK_APP=application flask db init'
$ docker-compose exec app /bin/sh -c 'FLASK_APP=application flask db migrate -m "Initial"'
$ docker-compose exec app /bin/sh -c 'FLASK_APP=application flask db upgrade'

Kubernetes

To deploy to kubernetes:

$ kubectl apply -f ./kubernetes/

Accessing the Application

For running the application with python directly and with docker-compose:

For deploying with kubernetes, the endpoint is:

Traefik Hub

Using this application in Traefik Hub:

Resources

Thanks to Anthony from PrettyPrinted for all your Flask tutorials, it helped me tremendously over the last couple of years.