commit 6c3937ae6a3e8bf075f1e0da617546166d214c1c Author: lorenz Date: Sun Jan 7 18:05:55 2024 +0100 initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..5e36584 --- /dev/null +++ b/README.md @@ -0,0 +1,72 @@ +# Calendar Generator Flask App + +This Flask application generates a web-based calendar view with images and associated texts for each day. It's designed to read images and texts from specified directories and display them in a calendar format. The web-based pages can be printed to a pdf-file. This is easy, because every browser application has the option to print a page to a pdf-file. Per DIN A4 page 4 days get displayed. + +## Setup +### Prerequisites +- Python 3.x +- Flask +- Basic understanding of Python and Flask + +### Installation + +1. **Clone or Download the Repository** + + Clone this repository if you're familiar with Git, or simply download it as a ZIP file and extract it. + +2. **Install Required Packages** + + Flask is required and can be installed via pip: + ```bash + pip install Flask + `````` + +### Folder Structure +Your project directory should be structured as follows: + +```yaml + +project_folder/ +│ app.py +│ README.md +│ +├───static/ +│ └───images/ +│ │ 2024-01-15.jpg +│ │ 2024-01-16.png +│ ... +│ +└───texts/ + │ 2024-01-15.txt + │ 2024-01-16.txt + ... +``` +- `app.py`: Main Flask application file. +- `static/images/`: Directory for storing calendar images. Images should be named `YYYY-MM-DD.extension`. +- `texts/`: Directory for storing text files corresponding to each day, named `YYYY-MM-DD.txt`. + +### Running the Application + +1. Navigate to the project folder in your command line or terminal. +2. Execute the Flask application: + ```bash + python app.py + `````` +3. Access the calendar in a web browser at `http://127.0.0.1:5000/`. + +## How It Works + +- The application serves a web page displaying a calendar. +- Each day on the calendar can have an image and a text associated with it. +- Images are read from `static/images/`, and text files are from `texts/`. +- Files are matched based on their filenames, formatted as `YYYY-MM-DD`. +- The calendar is dynamically generated based on available images and texts. +- If an image or text is not available for a specific day, it will be displayed without that content. + +## Notes + +- Text for each day should be concise. Lengthy texts may be truncated in the display to maintain the layout of the calendar. +- You can customize the appearance and behavior of the calendar by modifying the HTML, CSS, and Flask application logic. + +## Copyright and license +This code is for my very good friend Felix W.. Code copyright 2024 Lorenz B. \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..25a7b39 --- /dev/null +++ b/app.py @@ -0,0 +1,89 @@ +from flask import Flask, render_template +from datetime import datetime +import os +import re + +app = Flask(__name__) + +def get_dates_from_images(image_directory): + # Regular expression to match files with a date format YYYY-MM-DD + date_pattern = re.compile(r'(\d{4}-\d{2}-\d{2})') + + # List to store the dates + dates = [] + + # Iterate over each file in the images directory + for filename in os.listdir(image_directory): + # Search for the pattern in the filename + match = date_pattern.search(filename) + if match: + # Extract the date from the filename + try: + date = datetime.strptime(match.group(), '%Y-%m-%d') + dates.append(date) + except ValueError: + # If the date format is incorrect, ignore the file + continue + + # Return the sorted list of dates + return sorted(dates) + +def get_image_and_text_for_date(date, image_directory, text_directory): + formatted_date = date.strftime('%Y-%m-%d') + image_file = None + text_content = None + + # Check for image file + for ext in ['jpg', 'jpeg', 'png']: + # Check using full path, but store relative path for HTML + image_full_path = os.path.join(image_directory, f"{formatted_date}.{ext}") + if os.path.isfile(image_full_path): + # Store the relative path from within the 'static' directory + image_file = f"images/{formatted_date}.{ext}" + break + + # Check for text file + text_path = os.path.join(text_directory, f"{formatted_date}.txt") + if os.path.isfile(text_path): + with open(text_path, 'r') as file: + text_content = file.read() + + return image_file, text_content + + +@app.route('/') +def calendar(): + BASE_DIR = os.path.dirname(os.path.abspath(__file__)) + + image_directory = os.path.join(BASE_DIR, 'static', 'images') + text_directory = os.path.join(BASE_DIR, 'texts') + + # Get sorted dates from image files + dates = get_dates_from_images(image_directory) + if not dates: + return "No images found with the correct date format in the name.", 404 + + # Generate data for each date + pages_data = [] + days_data = [] + for date in dates: + image_file, text_content = get_image_and_text_for_date(date, image_directory, text_directory) + days_data.append({ + 'date': date, + 'image_file': image_file, + 'text_content': text_content + }) + + # Check if we have collected data for 4 days, then start a new page + if len(days_data) == 4: + pages_data.append(days_data) + days_data = [] + + # Add any remaining days to the last page + if days_data: + pages_data.append(days_data) + + return render_template('index.html', pages=pages_data) + +if __name__ == '__main__': + app.run(debug=True) diff --git a/static/images/2024-01-15.jpg b/static/images/2024-01-15.jpg new file mode 100755 index 0000000..b0e361c Binary files /dev/null and b/static/images/2024-01-15.jpg differ diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..3bc9e61 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,107 @@ + + + + + + + + + {% for page in pages %} +
+ {% for day in page %} +
+
+
+
+ {% if day.image_file %} + Bild für {{ day.date.strftime('%d. %B %Y') }} + {% endif %} +
+
+

{{ day.date.strftime('%A %d.%m.%Y') }}

+
+
+ {% if day.text_content %} +

{{ day.text_content }}

+ {% endif %} +
+
+ {% endfor %} +
+ {% endfor %} + + + + diff --git a/texts/2024-01-15.txt b/texts/2024-01-15.txt new file mode 100755 index 0000000..6266b15 --- /dev/null +++ b/texts/2024-01-15.txt @@ -0,0 +1 @@ +Beispiel Text \ No newline at end of file