What is Jinja2?

Jinja2 is a popular template engine for Python. A web template system combines a template with a specific data source to render a dynamic web page. This allows you to pass Python variables into HTML. It’s the frontend for the backend + has cards and other things that can be used and modified + shortcut to show stuff

  • Templating Engine: Jinja2 is a Python templating engine widely used in frameworks like Flask and Django. Supports modular code through template inheritance.
  • Flexible Syntax: Offers a flexible syntax for embedding Python-like expressions, fostering dynamic content generation.
  • Built-in Filters: Comes with built-in filters for easy data manipulation and formatting directly within templates.
  • Control Structures: Supports control structures like loops and conditionals.
  • Security Measures: Automatically escapes HTML characters by default, enhancing web application security.

Migrating the database

The first piece of code you must follow is creating and migrating a database. Anytime a developer changes the schema, this code needs to be run in order to update.

Run in terminal rm instance/volumes/** Then Run “./migrate.sh”

The code below shows what is happening when you run ./migrate.sh

Link to table (better explanation): https://docs.google.com/document/d/1oI6EoxTlOUjNMKxqLVud0AA–YRGDAWfyHU3Dz7VEhM/edit?usp=sharing

Bash export FLASK_APP=main export PYTHONPATH=.:$PYTHONPATH

The first section is for setting up whether you have the proper tools enabled export similar to the from statements used in python, gets an exported library to be used in the current code

if ! command -v sqlite3 &> /dev/null; then echo "Error: sqlite3 is not installed. Please install it before running this script." exit 1 fi

In these if statements, it first gets a small description of the sqlite3 using the command command -v and then using &> redirects it into /dev/null if this is not possible with the ! it passes through the if statement

if ! command -v python3 &> /dev/null; then echo "Error: python3 is not installed. Please install it before running this script." exit 1

The echo command then prints the error message The exit command ends the script from running fi

if ! python3 -m flask --version &> /dev/null; then echo "Error: Flask is not installed. Please install it before running this script." exit 1 fi

The -m gets flask from python and then asks for the version it’s running, other than that it still remains the same below
This next part of the code checks for if you have the proper files enabled

if [ ! -d "migrations" ]; then echo "Initializing migration for the first time..." python3 -m flask db init fi

The -d checks to see if “migrations” is part of a directory and if due to the !, if not runs the code underneath it The db init causes for Flask to create a repository The -e checks if the directory exists and is a file for “instances/volumes/sqlite.db” and “instances/volumes/sqlite-backup.db” with the code not finding that putting out an error message

if [ ! -e "instance/volumes/sqlite.db" ] && [ -e "instance/volumes/sqlite-backup.db" ]; then echo "No sqlite.db found, using sqlite-backup.db to generate the file."

cp "instance/volumes/sqlite-backup.db" "instance/volumes/sqlite.db" </code> <br> <br> The <code>cp</code> command then copies files which it currently does for the files "instance/volumes/sqlite-backup.db" and "instance/volumes/sqlite.db" 

The line backup_version=$(sqlite3… sets up a variable from the data of alembic_version and selects it from version_num


backup_version=$(sqlite3 instance/volumes/sqlite.db "SELECT version_num FROM alembic_version;") echo "Version ${backup_version} detected"

python3 -m flask db stamp "${backup_version}" </code> <br> Check if sqlite.db exists .backup before migration <br> <br> <code> elif [ -e "instance/volumes/sqlite.db" ]; then

timestamp=$(date "+%Y%m%d%H%M%S")
backup_file="instance/volumes/sqlite-backup-${timestamp}.db"

sqlite3 instance/volumes/sqlite.db ".backup instance/volumes/sqlite-backup.db"
sqlite3 instance/volumes/sqlite.db ".backup ${backup_file}" fi

python3 -m flask db migrate python3 -m flask db upgrade python3 -m flask custom generate_data </code>

Extra Comments: If the database file exists, it proceeds to create a timestamp to be used in the backup file’s name. Sets variable timestamp using: %Y: Year with century as a decimal number. %m: Month as a zero-padded decimal number. %d: Day of the month as a zero-padded decimal number. %H: Hour (00 to 23). %M: Minute (00 to 59). %S: Second (00 to 59).

It constructs a backup file name with the format “instance/volumes/sqlite-backup-<timestamp>.db”. Then, it uses sqlite3 to perform a backup of the SQLite database.

After the backup (if needed), the script get’s python to run the final 3 commands, python3 -m flask db migrate for migrating across databases python3 -m flask db upgrade to upgrade the database and python3 -m flask custom generate_data for generating or populating data in the database.

Summary:

Environment Setup: Configures necessary variables for the Flask application.

Dependency Checks: Verifies essential dependencies (sqlite3, python3, Flask) are installed.

Migration Initialization: Sets up Flask database migrations.

Backup and Restore: Handles database restoration from backup if needed.

Database Migration: Applies Flask’s migration commands for schema changes.

Data Generation: Executes a custom command (generate_data) for additional data.