squash / app.py
kivilaid's picture
Update app.py
7b87998 verified
import streamlit as st
import pandas as pd
import requests
from datetime import datetime, timedelta
# Set page configuration to wide
st.set_page_config(layout="wide")
# Initialize session state
if 'bookings' not in st.session_state:
st.session_state.bookings = []
# Function to get available slots
def get_available_slots(location_id):
service_id = 36561
# For simplicity, assume each court has slots from 9 AM to 9 PM
all_slots = [f"{hour}:00" for hour in range(9, 21)]
booked_slots = [booking['time'] for booking in st.session_state.bookings if booking['service_id'] == service_id and booking['location_id'] == location_id]
available_slots = [slot for slot in all_slots if slot not in booked_slots]
return available_slots
# Function to add a new booking
def add_booking(location_id, time):
service_id = 36561
st.session_state.bookings.append({
'id': len(st.session_state.bookings) + 1,
'service_id': service_id,
'location_id': location_id,
'time': time,
'created_at': datetime.now()
})
# Function to get a booking by ID
def get_booking_by_id(booking_id):
for booking in st.session_state.bookings:
if booking['id'] == booking_id:
return booking
return None
# Function to update a booking by ID
def update_booking(booking_id, location_id, time):
service_id = 36561
for booking in st.session_state.bookings:
if booking['id'] == booking_id:
booking['service_id'] = service_id
booking['location_id'] = location_id
booking['time'] = time
return booking
return None
# Function to delete a booking by ID
def delete_booking(booking_id):
st.session_state.bookings = [booking for booking in st.session_state.bookings if booking['id'] != booking_id]
# Function to get current month's bookings from API
def get_current_month_bookings():
api_key = "AmeTa7RskgvDqF3VGLBJ6PrWS9NnUtCz2w48QyXu"
now = datetime.now()
start_time = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
end_time = (start_time + timedelta(days=32)).replace(day=1) - timedelta(seconds=1)
url = f"https://app.booklux.com/api/v1/bookings?api_key={api_key}"
#url = f"https://app.booklux.com/api/v1/bookings?api_key={api_key}&startTime={start_time}&endTime={end_time}"
response = requests.get(url)
if response.status_code == 200:
return response.json()['data']
else:
st.error(f"Error fetching bookings: {response.status_code}")
return []
# Streamlit app layout
st.title("YOLO Management - Squash Club")
# Add new booking section
st.header("Add New Booking")
location_id = st.selectbox("Court Number", list(range(1, 8)))
time = st.selectbox("Time Slot", get_available_slots(location_id))
if st.button("Add Booking"):
add_booking(location_id, time)
st.success(f"Booking added successfully for Court {location_id} at {time}!")
# Display bookings
st.header("Bookings List")
current_month_bookings = get_current_month_bookings()
bookings_df = pd.DataFrame(current_month_bookings)
st.dataframe(bookings_df)
# Update booking section
st.header("Update Booking")
booking_id = st.number_input("Booking ID", min_value=1, step=1)
new_location_id = st.selectbox("New Court Number", list(range(1, 8)))
new_time = st.selectbox("New Time Slot", get_available_slots(new_location_id))
if st.button("Update Booking"):
updated_booking = update_booking(booking_id, new_location_id, new_time)
if updated_booking:
st.success(f"Booking {booking_id} updated successfully!")
else:
st.error(f"Booking {booking_id} not found!")
# Delete booking section
st.header("Delete Booking")
delete_booking_id = st.number_input("Booking ID to Delete", min_value=1, step=1)
if st.button("Delete Booking"):
delete_booking(delete_booking_id)
st.success(f"Booking {delete_booking_id} deleted successfully!")
# Available slots section
st.header("Available Slots")
court_numbers = list(range(1, 8))
tabs = st.tabs([f"Court {i}" for i in court_numbers])
for i, tab in enumerate(tabs):
with tab:
available_slots = get_available_slots(court_numbers[i])
st.write(f"Available slots for Court {court_numbers[i]}:")
st.write(available_slots)