DeCoster commited on
Commit
fd75173
ยท
verified ยท
1 Parent(s): b22fc8a

create a python project that prompts for calendar entries and creates a .ics file as output

Browse files
Files changed (5) hide show
  1. README.md +8 -5
  2. app.py +78 -0
  3. requirements.txt +20 -0
  4. static/style.css +31 -0
  5. templates/index.html +201 -0
README.md CHANGED
@@ -1,10 +1,13 @@
1
  ---
2
- title: Pycalpal Python Calendar Pal
3
- emoji: โšก
4
- colorFrom: gray
5
- colorTo: pink
6
  sdk: static
7
  pinned: false
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
1
  ---
2
+ title: PyCalPal - Python Calendar Pal ๐Ÿ“…
3
+ colorFrom: pink
4
+ colorTo: yellow
5
+ emoji: ๐Ÿณ
6
  sdk: static
7
  pinned: false
8
+ tags:
9
+ - deepsite-v3
10
  ---
11
 
12
+ # Welcome to your new DeepSite project!
13
+ This project was created with [DeepSite](https://huggingface.co/deepsite).
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ python
2
+ from flask import Flask, render_template, request, jsonify, send_file
3
+ from ics import Calendar, Event
4
+ from datetime import datetime
5
+ import io
6
+
7
+ app = Flask(__name__)
8
+
9
+ @app.route('/')
10
+ def index():
11
+ return render_template('index.html')
12
+
13
+ @app.route('/generate', methods=['POST'])
14
+ def generate_ics():
15
+ try:
16
+ data = request.get_json()
17
+ events_data = data.get('events', [])
18
+
19
+ cal = Calendar()
20
+
21
+ for event_data in events_data:
22
+ event = Event()
23
+ event.name = event_data.get('title', 'Untitled Event')
24
+ event.location = event_data.get('location', '')
25
+ event.description = event_data.get('description', '')
26
+
27
+ # Parse dates based on all-day flag
28
+ is_all_day = event_data.get('allDay', False)
29
+
30
+ if is_all_day:
31
+ start_str = event_data.get('start')
32
+ end_str = event_data.get('end')
33
+
34
+ # Parse date strings (format depends on browser locale)
35
+ start_date = datetime.strptime(start_str.split('T')[0], '%Y-%m-%d').date()
36
+ end_date = datetime.strptime(end_str.split('T')[0], '%Y-%m-%d').date()
37
+
38
+ event.begin = start_date
39
+ event.end = end_date
40
+ event.all_day = True
41
+ else:
42
+ # Parse datetime strings
43
+ start_str = event_data.get('start')
44
+ end_str = event_data.get('end')
45
+
46
+ # Handle different datetime formats from different browsers
47
+ for fmt in ('%Y-%m-%dT%H:%M', '%m/%d/%Y, %I:%M:%S %p', '%Y-%m-%d %H:%M:%S'):
48
+ try:
49
+ start_dt = datetime.strptime(start_str, fmt)
50
+ end_dt = datetime.strptime(end_str, fmt)
51
+ event.begin = start_dt
52
+ event.end = end_dt
53
+ break
54
+ except ValueError:
55
+ continue
56
+
57
+ cal.events.add(event)
58
+
59
+ # Create in-memory file
60
+ ics_file = io.BytesIO()
61
+ ics_file.write(str(cal).encode('utf-8'))
62
+ ics_file.seek(0)
63
+
64
+ return send_file(
65
+ ics_file,
66
+ as_attachment=True,
67
+ download_name='calendar_events.ics',
68
+ mimetype='text/calendar'
69
+ )
70
+
71
+ except Exception as e:
72
+ print(f"Error generating ICS: {str(e)}")
73
+ return jsonify({'error': str(e)}), 500
74
+
75
+ if __name__ == '__main__':
76
+ app.run(debug=True)
77
+
78
+ </html>
requirements.txt ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ flask
3
+ ics.py
4
+ python-dotenv
5
+
6
+
7
+ This project creates a web-based Python application that:
8
+ 1. Provides a clean, responsive UI for entering calendar events
9
+ 2. Allows adding multiple events before download
10
+ 3. Supports both all-day and timed events
11
+ 4. Generates a proper .ics file that can be imported into any calendar application
12
+ 5. Uses Flask for the backend and Tailwind CSS for styling
13
+
14
+ To run the application:
15
+ 1. Install the requirements: `pip install -r requirements.txt`
16
+ 2. Run the Flask app: `python app.py`
17
+ 3. Visit http://localhost:5000 in your browser
18
+
19
+ The app will let you create one or more calendar events and download them as an .ics file that can be imported into Google Calendar, Outlook, Apple Calendar, etc.
20
+ </html>
static/style.css ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Custom styles to complement Tailwind */
2
+ body {
3
+ font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
4
+ }
5
+
6
+ /* Animation for the download button */
7
+ button[type="submit"] {
8
+ transition: all 0.2s ease-in-out;
9
+ }
10
+
11
+ button[type="submit"]:hover {
12
+ transform: translateY(-1px);
13
+ }
14
+
15
+ /* Style for event cards in the list */
16
+ #eventsContainer div {
17
+ transition: all 0.2s ease;
18
+ }
19
+
20
+ #eventsContainer div:hover {
21
+ transform: translateX(2px);
22
+ box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
23
+ }
24
+
25
+ /* Responsive adjustments */
26
+ @media (max-width: 640px) {
27
+ .container {
28
+ padding-left: 1rem;
29
+ padding-right: 1rem;
30
+ }
31
+ }
templates/index.html ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>PyCalPal - Create Calendar Events</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
9
+ <script src="https://unpkg.com/feather-icons"></script>
10
+ <link rel="stylesheet" href="../static/style.css">
11
+ </head>
12
+ <body class="bg-gray-50 min-h-screen">
13
+ <div class="container mx-auto px-4 py-12">
14
+ <header class="text-center mb-12">
15
+ <h1 class="text-4xl font-bold text-indigo-700 mb-2">PyCalPal</h1>
16
+ <p class="text-lg text-gray-600">Create calendar events and export to .ics format</p>
17
+ </header>
18
+
19
+ <main class="max-w-3xl mx-auto bg-white rounded-xl shadow-md overflow-hidden p-8">
20
+ <form id="eventForm" class="space-y-6">
21
+ <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
22
+ <div>
23
+ <label for="eventTitle" class="block text-sm font-medium text-gray-700">Event Title</label>
24
+ <input type="text" id="eventTitle" name="eventTitle" required
25
+ class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 p-2 border">
26
+ </div>
27
+
28
+ <div>
29
+ <label for="eventLocation" class="block text-sm font-medium text-gray-700">Location (optional)</label>
30
+ <input type="text" id="eventLocation" name="eventLocation"
31
+ class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 p-2 border">
32
+ </div>
33
+ </div>
34
+
35
+ <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
36
+ <div>
37
+ <label for="startDate" class="block text-sm font-medium text-gray-700">Start Date & Time</label>
38
+ <input type="datetime-local" id="startDate" name="startDate" required
39
+ class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 p-2 border">
40
+ </div>
41
+
42
+ <div>
43
+ <label for="endDate" class="block text-sm font-medium text-gray-700">End Date & Time</label>
44
+ <input type="datetime-local" id="endDate" name="endDate" required
45
+ class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 p-2 border">
46
+ </div>
47
+ </div>
48
+
49
+ <div>
50
+ <label for="eventDescription" class="block text-sm font-medium text-gray-700">Description (optional)</label>
51
+ <textarea id="eventDescription" name="eventDescription" rows="3"
52
+ class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 p-2 border"></textarea>
53
+ </div>
54
+
55
+ <div class="flex items-center">
56
+ <input id="allDay" name="allDay" type="checkbox" class="h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500">
57
+ <label for="allDay" class="ml-2 block text-sm text-gray-700">All-day event</label>
58
+ </div>
59
+
60
+ <div class="flex justify-end space-x-4">
61
+ <button type="button" id="addAnother" class="px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
62
+ <i data-feather="plus" class="inline mr-1"></i> Add Another
63
+ </button>
64
+ <button type="submit" class="px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
65
+ <i data-feather="download" class="inline mr-1"></i> Generate .ics
66
+ </button>
67
+ </div>
68
+ </form>
69
+
70
+ <div id="eventsList" class="mt-8 space-y-4 hidden">
71
+ <h3 class="text-lg font-medium text-gray-900">Your Events</h3>
72
+ <div id="eventsContainer" class="space-y-2"></div>
73
+ </div>
74
+ </main>
75
+ </div>
76
+
77
+ <script>
78
+ feather.replace();
79
+
80
+ document.getElementById('allDay').addEventListener('change', function() {
81
+ const startDate = document.getElementById('startDate');
82
+ const endDate = document.getElementById('endDate');
83
+
84
+ if (this.checked) {
85
+ startDate.type = 'date';
86
+ endDate.type = 'date';
87
+ } else {
88
+ startDate.type = 'datetime-local';
89
+ endDate.type = 'datetime-local';
90
+ }
91
+ });
92
+
93
+ document.getElementById('addAnother').addEventListener('click', function() {
94
+ const form = document.getElementById('eventForm');
95
+ const formData = new FormData(form);
96
+ const event = {
97
+ title: formData.get('eventTitle'),
98
+ location: formData.get('eventLocation'),
99
+ start: formData.get('startDate'),
100
+ end: formData.get('endDate'),
101
+ description: formData.get('eventDescription'),
102
+ allDay: document.getElementById('allDay').checked
103
+ };
104
+
105
+ addEventToList(event);
106
+ form.reset();
107
+ });
108
+
109
+ function addEventToList(event) {
110
+ const eventsList = document.getElementById('eventsList');
111
+ const eventsContainer = document.getElementById('eventsContainer');
112
+
113
+ eventsList.classList.remove('hidden');
114
+
115
+ const eventElement = document.createElement('div');
116
+ eventElement.className = 'bg-gray-50 p-4 rounded-lg';
117
+ eventElement.innerHTML = `
118
+ <div class="flex justify-between">
119
+ <h4 class="font-medium">${event.title}</h4>
120
+ <button class="text-red-500 hover:text-red-700" onclick="this.parentElement.parentElement.remove()">
121
+ <i data-feather="trash-2" class="w-4 h-4"></i>
122
+ </button>
123
+ </div>
124
+ <p class="text-sm text-gray-600">${formatDateTime(event.start, event.allDay)} - ${formatDateTime(event.end, event.allDay)}</p>
125
+ ${event.location ? `<p class="text-sm text-gray-600"><i data-feather="map-pin" class="w-3 h-3 inline mr-1"></i>${event.location}</p>` : ''}
126
+ ${event.description ? `<p class="text-sm text-gray-600 mt-1">${event.description}</p>` : ''}
127
+ `;
128
+
129
+ eventsContainer.appendChild(eventElement);
130
+ feather.replace();
131
+ }
132
+
133
+ function formatDateTime(datetime, isAllDay) {
134
+ if (!datetime) return '';
135
+ if (isAllDay) return new Date(datetime).toLocaleDateString();
136
+
137
+ const date = new Date(datetime);
138
+ return date.toLocaleString();
139
+ }
140
+
141
+ document.getElementById('eventForm').addEventListener('submit', async function(e) {
142
+ e.preventDefault();
143
+
144
+ const formData = new FormData(this);
145
+ const events = [];
146
+
147
+ // Get all events from the list
148
+ const eventElements = document.getElementById('eventsContainer').children;
149
+ if (eventElements.length > 0) {
150
+ Array.from(eventElements).forEach(el => {
151
+ events.push({
152
+ title: el.querySelector('h4').textContent,
153
+ location: el.querySelector('p:nth-of-type(2)')?.textContent.replace('๐Ÿ“', '').trim(),
154
+ start: el.querySelector('p:nth-of-type(1)').textContent.split(' - ')[0],
155
+ end: el.querySelector('p:nth-of-type(1)').textContent.split(' - ')[1],
156
+ description: el.querySelector('p:nth-of-type(3)')?.textContent,
157
+ allDay: !el.querySelector('p:nth-of-type(1)').textContent.includes(':')
158
+ });
159
+ });
160
+ } else {
161
+ // Add the current form data if no events in list
162
+ events.push({
163
+ title: formData.get('eventTitle'),
164
+ location: formData.get('eventLocation'),
165
+ start: formData.get('startDate'),
166
+ end: formData.get('endDate'),
167
+ description: formData.get('eventDescription'),
168
+ allDay: document.getElementById('allDay').checked
169
+ });
170
+ }
171
+
172
+ try {
173
+ const response = await fetch('/generate', {
174
+ method: 'POST',
175
+ headers: {
176
+ 'Content-Type': 'application/json',
177
+ },
178
+ body: JSON.stringify({ events })
179
+ });
180
+
181
+ if (response.ok) {
182
+ const blob = await response.blob();
183
+ const url = window.URL.createObjectURL(blob);
184
+ const a = document.createElement('a');
185
+ a.href = url;
186
+ a.download = 'calendar_events.ics';
187
+ document.body.appendChild(a);
188
+ a.click();
189
+ window.URL.revokeObjectURL(url);
190
+ a.remove();
191
+ } else {
192
+ alert('Error generating calendar file');
193
+ }
194
+ } catch (error) {
195
+ console.error('Error:', error);
196
+ alert('An error occurred while generating the calendar file');
197
+ }
198
+ });
199
+ </script>
200
+ </body>
201
+ </html>