Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import requests
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
+
|
| 6 |
+
# Initialize session state
|
| 7 |
+
if 'bookings' not in st.session_state:
|
| 8 |
+
st.session_state.bookings = []
|
| 9 |
+
|
| 10 |
+
# Function to get available slots
|
| 11 |
+
def get_available_slots(location_id):
|
| 12 |
+
service_id = 36561
|
| 13 |
+
# For simplicity, assume each court has slots from 9 AM to 9 PM
|
| 14 |
+
all_slots = [f"{hour}:00" for hour in range(9, 21)]
|
| 15 |
+
booked_slots = [booking['time'] for booking in st.session_state.bookings if booking['service_id'] == service_id and booking['location_id'] == location_id]
|
| 16 |
+
available_slots = [slot for slot in all_slots if slot not in booked_slots]
|
| 17 |
+
return available_slots
|
| 18 |
+
|
| 19 |
+
# Function to add a new booking
|
| 20 |
+
def add_booking(location_id, time):
|
| 21 |
+
service_id = 36561
|
| 22 |
+
st.session_state.bookings.append({
|
| 23 |
+
'id': len(st.session_state.bookings) + 1,
|
| 24 |
+
'service_id': service_id,
|
| 25 |
+
'location_id': location_id,
|
| 26 |
+
'time': time,
|
| 27 |
+
'created_at': datetime.now()
|
| 28 |
+
})
|
| 29 |
+
|
| 30 |
+
# Function to get a booking by ID
|
| 31 |
+
def get_booking_by_id(booking_id):
|
| 32 |
+
for booking in st.session_state.bookings:
|
| 33 |
+
if booking['id'] == booking_id:
|
| 34 |
+
return booking
|
| 35 |
+
return None
|
| 36 |
+
|
| 37 |
+
# Function to update a booking by ID
|
| 38 |
+
def update_booking(booking_id, location_id, time):
|
| 39 |
+
service_id = 36561
|
| 40 |
+
for booking in st.session_state.bookings:
|
| 41 |
+
if booking['id'] == booking_id:
|
| 42 |
+
booking['service_id'] = service_id
|
| 43 |
+
booking['location_id'] = location_id
|
| 44 |
+
booking['time'] = time
|
| 45 |
+
return booking
|
| 46 |
+
return None
|
| 47 |
+
|
| 48 |
+
# Function to delete a booking by ID
|
| 49 |
+
def delete_booking(booking_id):
|
| 50 |
+
st.session_state.bookings = [booking for booking in st.session_state.bookings if booking['id'] != booking_id]
|
| 51 |
+
|
| 52 |
+
# Function to get current month's bookings from API
|
| 53 |
+
def get_current_month_bookings():
|
| 54 |
+
api_key = "AmeTa7RskgvDqF3VGLBJ6PrWS9NnUtCz2w48QyXu"
|
| 55 |
+
now = datetime.now()
|
| 56 |
+
start_time = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
|
| 57 |
+
end_time = (start_time + timedelta(days=32)).replace(day=1) - timedelta(seconds=1)
|
| 58 |
+
url = f"https://app.booklux.com/api/v1/bookings?api_key={api_key}&startTime={start_time}&endTime={end_time}"
|
| 59 |
+
|
| 60 |
+
response = requests.get(url)
|
| 61 |
+
if response.status_code == 200:
|
| 62 |
+
return response.json()['data']
|
| 63 |
+
else:
|
| 64 |
+
st.error(f"Error fetching bookings: {response.status_code}")
|
| 65 |
+
return []
|
| 66 |
+
|
| 67 |
+
# Streamlit app layout
|
| 68 |
+
st.title("YOLO Management - Squash Club")
|
| 69 |
+
|
| 70 |
+
# Add new booking section
|
| 71 |
+
st.header("Add New Booking")
|
| 72 |
+
location_id = st.selectbox("Court Number", list(range(1, 8)))
|
| 73 |
+
time = st.selectbox("Time Slot", get_available_slots(location_id))
|
| 74 |
+
if st.button("Add Booking"):
|
| 75 |
+
add_booking(location_id, time)
|
| 76 |
+
st.success(f"Booking added successfully for Court {location_id} at {time}!")
|
| 77 |
+
|
| 78 |
+
# Display bookings
|
| 79 |
+
st.header("Bookings List")
|
| 80 |
+
current_month_bookings = get_current_month_bookings()
|
| 81 |
+
bookings_df = pd.DataFrame(current_month_bookings)
|
| 82 |
+
st.dataframe(bookings_df)
|
| 83 |
+
|
| 84 |
+
# Update booking section
|
| 85 |
+
st.header("Update Booking")
|
| 86 |
+
booking_id = st.number_input("Booking ID", min_value=1, step=1)
|
| 87 |
+
new_location_id = st.selectbox("New Court Number", list(range(1, 8)))
|
| 88 |
+
new_time = st.selectbox("New Time Slot", get_available_slots(new_location_id))
|
| 89 |
+
if st.button("Update Booking"):
|
| 90 |
+
updated_booking = update_booking(booking_id, new_location_id, new_time)
|
| 91 |
+
if updated_booking:
|
| 92 |
+
st.success(f"Booking {booking_id} updated successfully!")
|
| 93 |
+
else:
|
| 94 |
+
st.error(f"Booking {booking_id} not found!")
|
| 95 |
+
|
| 96 |
+
# Delete booking section
|
| 97 |
+
st.header("Delete Booking")
|
| 98 |
+
delete_booking_id = st.number_input("Booking ID to Delete", min_value=1, step=1)
|
| 99 |
+
if st.button("Delete Booking"):
|
| 100 |
+
delete_booking(delete_booking_id)
|
| 101 |
+
st.success(f"Booking {delete_booking_id} deleted successfully!")
|