This is a sample Python Flask app with the following features:
- Folder structure that can accomodate medium-sized projects
- MySQL database connection
- Sample routes that serve Jinja templates
- Sample “utility” routes (robots.txt and error pages)
- Base Jinja template that provides a consistent page structure
- Sample CSS styles, including light/dark theming
This template has grown out of boilerplate code from several personal projects, including SingPraises.net and Scripture Tools. Although it’s been tailored to meet my own needs, I hope it will be useful to others.
These instructions are for macOS (other systems may vary).
-
Open Terminal.
-
Navigate to the folder where you want to download the project:
cd /path/to/folder
- Clone the repository:
git clone https://github.com/samuelbradshaw/flask-template.git flask-template
If you prefer a GUI for git instead of Terminal, I recommend Sourcetree.
Instructions for installing Python: Installing Python 2 and 3 on macOS
Instructions for installing MySQL: Installing MySQL on macOS
- Open Terminal and navigate to the project folder:
cd /path/to/project/folder
- Set up and activate a virtual environment:
python3 -m venv .venv
source .venv/bin/activate
- Install Flask and other Python dependencies in the virtual environment:
pip3 install -r requirements.txt
-
Log on to your MySQL instance. This can be done in Terminal, but I’d recommend using Sequel Ace as a GUI.
- Host:
127.0.0.1
- Username:
[your-mysql-username]
- Password:
[your-mysql-username]
- Port:
3306
- Host:
-
Create a new database for the app to use (for example,
sample_database
).
-
Create a new Python file called config.py inside the
main
folder in the repository (this file isn’t tracked by git, because it contains configuration/credentials that are unique to the user). -
Add the following lines to the config file, replacing the placeholders with your MySQL username, password, and database name:
SITE_NAME = 'Sample Website'
class DBInfo:
SERVER = 'localhost'
USERNAME = '[your-mysql-username]'
PASSWORD = '[your-mysql-password]'
DATABASE = '[your-database-name]'
PORT = 3306
- Navigate to the project folder and start the virtual environment (if it's not already running):
cd /path/to/project/folder
source .venv/bin/activate
- Run the Python script that loads the site:
python3 run.py
- Open the app in your browser:
http://127.0.0.1:8080