Skip to content

Repository files navigation

Python Flask Web Application

This is a Flask web application for testing and demonstration purposes.

Structure

PYTHON/
├── app.py                  # Main Flask application entry point
├── requirements.txt        # Python dependencies
├── Dockerfile              # Docker image definition
├── docker-compose.yml      # Docker Compose configuration
├── entrypoint.sh           # Container startup script
├── .dockerignore           # Docker build exclusions
├── .env.example            # Environment variables template
├── controllers/            # Controller classes
│   ├── base.py            # Base controller with common functionality
│   ├── indexController.py
│   ├── form1Controller.py - form6Controller.py
│   ├── responseccController.py
│   ├── useraccountController.py
│   ├── resultsController.py
│   ├── employeeController.py
│   ├── salesController.py
│   └── ... (all other controllers)
├── templates/             # Jinja2 HTML templates
│   ├── base.html         # Base layout template
│   ├── index.html
│   ├── form-1.html - form-6-celsius-fahrenheit.html
│   ├── inc/              # Include files (navigation, header, footer)
│   └── admin/            # Admin templates
├── api/                   # REST API endpoints
│   └── routes.py         # API Blueprint with /allusers and /user/<id>
├── data/                  # SQLite database
│   └── Main.db           # User database
├── scripts/               # Database utilities and scripts
└── static/               # Static files (CSS, JS, images)

Installation

  1. Create a virtual environment (recommended):

    python -m venv .venv
    .\.venv\Scripts\Activate.ps1
  2. Install dependencies:

    pip install -r requirements.txt

Running the Application

Development Server

Quick Start (using startup scripts):

# PowerShell
.\start-server.ps1

# Command Prompt
start-server.bat

Manual Start:

python app.py

The application will be available at: http://localhost:5000/

Configuration

You can configure the host and port using environment variables:

Environment Variables:

  • FLASK_HOST - Host to bind to (default: localhost - only accessible from local machine)
  • FLASK_PORT - Port to listen on (default: 5000)

Examples:

# Run on localhost only (default - more secure, not accessible from network)
$env:FLASK_HOST = "localhost"
python app.py

# Run on 127.0.0.1 (same as localhost)
$env:FLASK_HOST = "127.0.0.1"
python app.py

# Run on all interfaces (accessible from network)
$env:FLASK_HOST = "0.0.0.0"
python app.py

# Custom port
$env:FLASK_PORT = "8080"
python app.py

Batch (CMD):

set FLASK_HOST=localhost
set FLASK_PORT=8080
python app.py

You can also modify the commented configuration section in start-server.ps1 or start-server.bat to set your preferred defaults.

Running with Docker

The application can be run as a Docker container for isolated, reproducible deployments.

Prerequisites

  • Docker Desktop installed and running
  • Docker Compose included with Docker Desktop

Quick Start with Docker Compose

  1. Build and start the container:

    docker-compose up

    This will:

    • Build the Docker image
    • Create and start the container
    • Automatically initialize database tables (CREDENTIALS and EMPLOYEES)
    • Start the Flask application on port 5000
  2. Access the application:

    • Open browser to http://localhost:5000/
  3. Stop the container:

    # Stop with Ctrl+C, then remove containers
    docker-compose down

Docker Commands Reference

Build the Docker image:

docker build -t flask-test-app .

Run container manually (without docker-compose):

docker run -p 5000:5000 -v ${PWD}/data:/app/data flask-test-app

Run in background (detached mode):

docker-compose up -d

View logs:

docker-compose logs -f

Stop and remove containers:

docker-compose down

Rebuild after code changes:

docker-compose up --build

Environment Configuration

Create a .env file from the example to customize settings:

Copy-Item .env.example .env

Edit .env to configure:

  • FLASK_PORT - Port to expose (default: 5000)
  • FLASK_DEBUG - Enable debug mode (default: True)
  • SECRET_KEY - Session secret key (change for production!)

Example .env file:

FLASK_PORT=8080
FLASK_DEBUG=False
SECRET_KEY=your-super-secret-key-here

Data Persistence

The SQLite database is stored in the data/ directory, which is mounted as a Docker volume:

  • Database file persists across container restarts
  • To reset the database, stop the container and delete data/Main.db
  • Database tables are automatically created on first run via entrypoint.sh

Docker Container Features

  • Automatic Database Initialization: Creates CREDENTIALS and EMPLOYEES tables on startup
  • Sample Data: Populates employee directory with test data if empty
  • Health Checks: Container monitors application health automatically
  • Non-Root User: Runs as flaskuser for security
  • Volume Mounting: Database persists outside container
  • Environment Variables: Configurable via .env file

Troubleshooting Docker

Port conflict:

# Change port in docker-compose.yml or .env file
FLASK_PORT=8080
docker-compose up

Database permission errors:

# On Windows, ensure the data directory exists
New-Item -ItemType Directory -Force -Path data

View container logs:

docker-compose logs flask-app

Reset everything:

docker-compose down -v  # Remove volumes
Remove-Item data/Main.db  # Delete database
docker-compose up --build  # Rebuild and start fresh

Features

Web Pages

All pages are accessible via clean path-based URLs:

  • Home: http://localhost:5000/

  • Forms:

    • Form 1: /form1 - Information about yourself
    • Form 2: /form2 - Book Accommodation
    • Form 3: /form3 - Credit Card (with Luhn validation)
    • Form 4: /form4 - Login Form (credentials: admin/pw1234 or joe/doe)
    • Form 5: /form5 - Media Settings
    • Form 6: /form6 - Celsius/Fahrenheit Converter
  • Pages:

    • Orange Page: /orangePage
    • Green Page: /greenPage
    • Brown Page: /brownPage
    • Child Window: /childWindow
    • Nested Menu 1: /nestedmenu1
    • Nested Menu 2: /nestedmenu2
    • Overlay 1: /overlay1
    • Card Flip 1: /cardflip1
  • Admin:

    • Employee Finder: /employee
    • Sales Statistics: /sales

REST API Endpoints

  • Get all users: GET http://localhost:5000/api/allusers
  • Get user by ID: GET http://localhost:5000/api/user/1

Test Credentials

Form 4 - Login Form

Credentials are stored in the database (data/Main.db in the CREDENTIALS table) with hashed passwords:

  • Admin: username: admin, password: pw1234
  • Employee: username: joe, password: doe

User Profile Management:

  • Employee users have editable profile forms with data stored in the USER_PROFILES table
  • Profile data includes: title, initial, first name, middle name, last name, gender, and languages
  • Profile changes are saved to the database and persist across sessions
  • Click "Save" on the user account page to update profile information
  • Logout: Visit /logout to end your session securely

Session-Based Authentication:

  • User credentials verified against database with hashed passwords
  • Session data stored securely server-side (not in cookies)
  • Profile updates require active login session
  • Unauthenticated access to /useraccount redirects to login page
  • See SECURITY.md for detailed security documentation

Note: To reset or modify credentials, use the database utilities in utils/db_utils.py.

Form 3 - Credit Card

Test credit card numbers (valid per Luhn algorithm):

  • 4111111111111111 (Visa)
  • 5500000000000004 (Mastercard)
  • 340000000000009 (Amex)

Technology Stack

  • Flask 3.0.0 - Web framework
  • Jinja2 3.1.2 - Template engine
  • luhn 0.2.0 - Credit card validation
  • SQLite - Database (embedded)

Development

Adding a New Controller

  1. Create a new controller file in controllers/ directory:

    from controllers.base import BaseController
    
    class MycontrollerController(BaseController):
        def run(self):
            return self.render_view('my-template', self.data)
  2. Create the corresponding template in templates/ directory

  3. Access at http://localhost:5000/?action=mycontroller

Adding a New Template

Templates use Jinja2 syntax:

{% extends 'base.html' %}
{% block body %}
<div class="center">
    <h2>My Page</h2>
    <p>Content goes here</p>
</div>
{% endblock %}

Troubleshooting

Import Errors

Make sure you're in the PYTHON directory when running:

cd PYTHON
python app.py

Database Errors

Ensure the data/Main.db file exists and is readable.

Port Already in Use

Change the port in app.py:

app.run(host='0.0.0.0', port=5001, debug=True)

About

Sample site for trying automated tests based on Python/Flask

Topics

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages