This project generates a PDF calendar with tide information for a specified tide station, year, and month. It includes a web interface built with Flask, and the application can be containerized using Docker.
- Python 3.9 or later
- Docker
- Git (for version control)
-
Clone the repository:
git clone https://github.com/yourusername/tide-calendar-generator.git cd tide-calendar-generator
-
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install the required Python packages:
pip install -r requirements.txt
-
Install system dependencies:
sudo apt-get update sudo apt-get install -y pcal ghostscript
-
Set environment variables:
Create a
.env
file in the project root with the following content:FLASK_APP=run.py FLASK_ENV=development
-
Run the Flask app:
flask run --host=0.0.0.0 --port=5001
-
Access the app:
Open your web browser and go to
http://127.0.0.1:5000/
.
-
Build the Docker image:
docker build -t tide-calendar-app .
-
Run the Docker container:
docker run -p 5001:5001 tide-calendar-app
-
Access the app:
Open your web browser and go to
http://127.0.0.1:5001/
.
tide_calendar/
├── app/
│ ├── __init__.py
│ ├── routes.py
│ ├── templates/
│ │ └── index.html
│ └── static/
│ └── style.css
├── get_tides.py
├── run.py
├── requirements.txt
├── Dockerfile
└── README.md
- app/init.py: Initializes the Flask app.
- app/routes.py: Contains the route for the web interface.
- app/templates/index.html: HTML template for the web interface.
- app/static/style.css: CSS file for styling the web interface.
- get_tides.py: Script to generate the PDF calendar with tide information.
- run.py: Entry point to run the Flask app.
- requirements.txt: List of Python dependencies.
- Dockerfile: Instructions to build the Docker image.
- Enter the tide station ID, year, and month in the web form.
- Click "Generate PDF" to create and download the tide calendar.
- Ensure
pcal
andghostscript
are installed on your system if running locally. - The default tide station ID is set to
9449639
for demonstration purposes.
For deployment, it's common practice to use standard HTTP (port 80) or HTTPS (port 443) ports to make the application accessible over the web without specifying a port number in the URL. Here’s how you can set this up in your Docker deployment.
-
Modify the Docker Run Command
To use port 80 or 443, map the internal port 5000 to the desired external port.
For HTTP (port 80):
docker run -p 80:5000 tide-calendar-app
For HTTPS (port 443):
docker run -p 443:5000 tide-calendar-app
-
Using HTTPS (port 443)
If you are using HTTPS, you'll need to set up SSL/TLS certificates. This is typically done using a reverse proxy like Nginx or a cloud provider's load balancer that handles SSL termination.
-
Nginx Configuration
Create an Nginx configuration file to proxy requests to your Flask app running inside Docker.
nginx.conf:
server { listen 80; server_name yourdomain.com; location / { proxy_pass http://localhost:5000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
-
Docker Compose
You can use Docker Compose to run both your Flask app and Nginx.
docker-compose.yml:
version: '3' services: app: build: . ports: - "5000:5000" environment: FLASK_ENV: production nginx: image: nginx:latest ports: - "80:80" volumes: - ./nginx.conf:/etc/nginx/conf.d/default.conf depends_on: - app
-
Building and Running with Docker Compose
docker-compose up --build
If you are using port 443, you’ll need to set up SSL certificates. Let’s Encrypt is a popular free option. Here’s a basic setup using Certbot with Nginx.
-
Install Certbot
Install Certbot and the Nginx plugin on your server.
For Ubuntu:
sudo apt-get update sudo apt-get install certbot python3-certbot-nginx
-
Obtain and Install SSL Certificate
sudo certbot --nginx -d yourdomain.com
Follow the prompts to obtain and install the certificate.
-
Automatic Renewal
Certbot automatically installs a cron job for certificate renewal. You can test the renewal process with:
sudo certbot renew --dry-run
- For production deployment, map port 5000 to port 80 for HTTP or port 443 for HTTPS in your Docker run command.
- Use Nginx as a reverse proxy to manage incoming traffic and SSL termination.
- Obtain and configure SSL certificates if using HTTPS, using tools like Certbot and Let's Encrypt.
- If you encounter a
500 Internal Server Error
, check the Flask console output for detailed error messages. - Ensure all environment variables are set correctly, and dependencies are installed.
Contributions are welcome! Please fork the repository and submit a pull request for review.
This project is licensed under the GNU GPL v3 License.