DeCoster's picture
create a python project that prompts for calendar entries and creates a .ics file as output
fd75173 verified
Raw History Blame Contribute Delete
10.1 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PyCalPal - Create Calendar Events</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
<script src="https://unpkg.com/feather-icons"></script>
<link rel="stylesheet" href="../static/style.css">
</head>
<body class="bg-gray-50 min-h-screen">
<div class="container mx-auto px-4 py-12">
<header class="text-center mb-12">
<h1 class="text-4xl font-bold text-indigo-700 mb-2">PyCalPal</h1>
<p class="text-lg text-gray-600">Create calendar events and export to .ics format</p>
</header>
<main class="max-w-3xl mx-auto bg-white rounded-xl shadow-md overflow-hidden p-8">
<form id="eventForm" class="space-y-6">
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<div>
<label for="eventTitle" class="block text-sm font-medium text-gray-700">Event Title</label>
<input type="text" id="eventTitle" name="eventTitle" required
class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 p-2 border">
</div>
<div>
<label for="eventLocation" class="block text-sm font-medium text-gray-700">Location (optional)</label>
<input type="text" id="eventLocation" name="eventLocation"
class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 p-2 border">
</div>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<div>
<label for="startDate" class="block text-sm font-medium text-gray-700">Start Date & Time</label>
<input type="datetime-local" id="startDate" name="startDate" required
class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 p-2 border">
</div>
<div>
<label for="endDate" class="block text-sm font-medium text-gray-700">End Date & Time</label>
<input type="datetime-local" id="endDate" name="endDate" required
class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 p-2 border">
</div>
</div>
<div>
<label for="eventDescription" class="block text-sm font-medium text-gray-700">Description (optional)</label>
<textarea id="eventDescription" name="eventDescription" rows="3"
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>
</div>
<div class="flex items-center">
<input id="allDay" name="allDay" type="checkbox" class="h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500">
<label for="allDay" class="ml-2 block text-sm text-gray-700">All-day event</label>
</div>
<div class="flex justify-end space-x-4">
<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">
<i data-feather="plus" class="inline mr-1"></i> Add Another
</button>
<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">
<i data-feather="download" class="inline mr-1"></i> Generate .ics
</button>
</div>
</form>
<div id="eventsList" class="mt-8 space-y-4 hidden">
<h3 class="text-lg font-medium text-gray-900">Your Events</h3>
<div id="eventsContainer" class="space-y-2"></div>
</div>
</main>
</div>
<script>
feather.replace();
document.getElementById('allDay').addEventListener('change', function() {
const startDate = document.getElementById('startDate');
const endDate = document.getElementById('endDate');
if (this.checked) {
startDate.type = 'date';
endDate.type = 'date';
} else {
startDate.type = 'datetime-local';
endDate.type = 'datetime-local';
}
});
document.getElementById('addAnother').addEventListener('click', function() {
const form = document.getElementById('eventForm');
const formData = new FormData(form);
const event = {
title: formData.get('eventTitle'),
location: formData.get('eventLocation'),
start: formData.get('startDate'),
end: formData.get('endDate'),
description: formData.get('eventDescription'),
allDay: document.getElementById('allDay').checked
};
addEventToList(event);
form.reset();
});
function addEventToList(event) {
const eventsList = document.getElementById('eventsList');
const eventsContainer = document.getElementById('eventsContainer');
eventsList.classList.remove('hidden');
const eventElement = document.createElement('div');
eventElement.className = 'bg-gray-50 p-4 rounded-lg';
eventElement.innerHTML = `
<div class="flex justify-between">
<h4 class="font-medium">${event.title}</h4>
<button class="text-red-500 hover:text-red-700" onclick="this.parentElement.parentElement.remove()">
<i data-feather="trash-2" class="w-4 h-4"></i>
</button>
</div>
<p class="text-sm text-gray-600">${formatDateTime(event.start, event.allDay)} - ${formatDateTime(event.end, event.allDay)}</p>
${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>` : ''}
${event.description ? `<p class="text-sm text-gray-600 mt-1">${event.description}</p>` : ''}
`;
eventsContainer.appendChild(eventElement);
feather.replace();
}
function formatDateTime(datetime, isAllDay) {
if (!datetime) return '';
if (isAllDay) return new Date(datetime).toLocaleDateString();
const date = new Date(datetime);
return date.toLocaleString();
}
document.getElementById('eventForm').addEventListener('submit', async function(e) {
e.preventDefault();
const formData = new FormData(this);
const events = [];
// Get all events from the list
const eventElements = document.getElementById('eventsContainer').children;
if (eventElements.length > 0) {
Array.from(eventElements).forEach(el => {
events.push({
title: el.querySelector('h4').textContent,
location: el.querySelector('p:nth-of-type(2)')?.textContent.replace('๐Ÿ“', '').trim(),
start: el.querySelector('p:nth-of-type(1)').textContent.split(' - ')[0],
end: el.querySelector('p:nth-of-type(1)').textContent.split(' - ')[1],
description: el.querySelector('p:nth-of-type(3)')?.textContent,
allDay: !el.querySelector('p:nth-of-type(1)').textContent.includes(':')
});
});
} else {
// Add the current form data if no events in list
events.push({
title: formData.get('eventTitle'),
location: formData.get('eventLocation'),
start: formData.get('startDate'),
end: formData.get('endDate'),
description: formData.get('eventDescription'),
allDay: document.getElementById('allDay').checked
});
}
try {
const response = await fetch('/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ events })
});
if (response.ok) {
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'calendar_events.ics';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
a.remove();
} else {
alert('Error generating calendar file');
}
} catch (error) {
console.error('Error:', error);
alert('An error occurred while generating the calendar file');
}
});
</script>
</body>
</html>