-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRed_Bus_Web_streamlit.py
More file actions
217 lines (182 loc) · 9.63 KB
/
Red_Bus_Web_streamlit.py
File metadata and controls
217 lines (182 loc) · 9.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from geopy.geocoders import Nominatim
import streamlit as st
import pandas as pd
import psycopg2
from PIL import Image
from datetime import time
# Initialize Nominatim (OpenStreetMap)
geolocator = Nominatim(user_agent="india_state_finder")
def get_state_in_india(city_name):
try:
location = geolocator.geocode(f"{city_name}, India", exactly_one=True, addressdetails=True)
if location:
address = location.raw['address']
state = address.get('state', "State not found")
return state
else:
return "Location not found in India"
except Exception as e:
return f"Error: {str(e)}"
# Set page to full width
st.set_page_config(page_title="RED BUS.IN", layout="wide")
# Load local logo
LOCAL_LOGO_PATH = r"C:\\Users\\USER\\Desktop\\RedBus_Automation_Project\\red bus logo.png"
try:
logo = Image.open(LOCAL_LOGO_PATH)
logo = logo.resize((250, 250))
except Exception as e:
st.error(f"Failed to load logo: {str(e)}")
logo = None
# Create header
col1, col2 = st.columns([1, 5])
with col1:
if logo:
st.image(logo)
with col2:
st.markdown("<h1 style='color: red;'>RedBus Booking</h1>", unsafe_allow_html=True)
# PostgreSQL connection
DB_CONFIG = {
"dbname": "red_bus",
"user": "postgres",
"password": "sample12",
"host": "localhost",
"port": "5432",
}
@st.cache_data(ttl=300)
def execute_query(query, params=None):
conn = psycopg2.connect(**DB_CONFIG)
cursor = conn.cursor()
try:
cursor.execute(query, params or ())
columns = [desc[0] for desc in cursor.description]
results = cursor.fetchall()
return pd.DataFrame(results, columns=columns)
finally:
cursor.close()
conn.close()
@st.cache_data(ttl=3600)
def get_route_pairs():
route_query = "SELECT DISTINCT route_name FROM bus_routes ORDER BY route_name"
route_df = execute_query(route_query)
route_pairs = [(r.split(' to ')[0].strip(), r.split(' to ')[1].strip()) for r in route_df['route_name'] if ' to ' in r]
return route_pairs
def get_destinations_for_origin(origin):
return sorted({dest for src, dest in get_route_pairs() if src == origin})
def get_all_origins():
return sorted({src for src, dest in get_route_pairs()})
# Initialize session state
for key in ['from_location', 'to_location', 'search_clicked', 'seats_required']:
if key not in st.session_state:
st.session_state[key] = None if key != 'seats_required' else 1
# Sidebar filters
st.sidebar.header("Filter Options")
st.sidebar.markdown("**Journey Details**")
all_origins = get_all_origins()
from_location = st.sidebar.selectbox("From", all_origins, index=None, placeholder="Select departure", key='from_location_select')
to_location = None
if st.session_state.from_location_select:
destinations = get_destinations_for_origin(st.session_state.from_location_select)
to_location = st.sidebar.selectbox("To", destinations, index=None, placeholder="Select arrival", key='to_location_select')
def filter_and_display_buses():
if st.session_state.from_location_select and st.session_state.to_location_select:
route_name = f"{st.session_state.from_location_select} to {st.session_state.to_location_select}"
base_query = """
SELECT
id, route_name, bus_name, bus_type,
departing_time::time as departing_time,
reaching_time::time as reaching_time,
duration, seats_available, price, star_rating
FROM bus_routes
WHERE route_name = %s
"""
all_buses = execute_query(base_query, [route_name])
if all_buses.empty:
st.warning(f"No buses available for route: {route_name}")
return None
filtered = all_buses.copy()
filter_messages = []
if departure_time != "All":
time_filters = {
"Evening (6PM-12AM)": (time(18, 0), time(23, 59, 59)),
"Midnight (12AM-6AM)": (time(0, 0), time(5, 59, 59)),
"Morning (6AM-12PM)": (time(6, 0), time(11, 59, 59)),
"Afternoon (12PM-6PM)": (time(12, 0), time(17, 59, 59)),
}
time_start, time_end = time_filters[departure_time]
filtered['departing_time'] = filtered['departing_time'].apply(lambda x: x if isinstance(x, time) else time(x.hour, x.minute, x.second))
filtered = filtered[filtered['departing_time'].apply(lambda x: time_start <= x <= time_end)]
filter_messages.append(f"⏰ {len(filtered)} buses available for {departure_time}")
if ac_option != "All":
ac_mask = filtered['bus_type'].str.contains('AC', case=False)
filtered = filtered[ac_mask] if ac_option == "AC" else filtered[~ac_mask]
filter_messages.append(f"❄️ {len(filtered)} {'AC' if ac_option == 'AC' else 'Non-AC'} buses available")
if seat_type != "All":
seat_mask = filtered['bus_type'].str.contains(seat_type, case=False)
filtered = filtered[seat_mask]
filter_messages.append(f"🛏️ {len(filtered)} {seat_type} buses available")
star_mask = (filtered['star_rating'] >= star_min) & (filtered['star_rating'] <= star_max)
filtered = filtered[star_mask]
filter_messages.append(f"⭐ {len(filtered)} buses with rating {star_min}-{star_max}")
price_mask = (filtered['price'] >= price_range[0]) & (filtered['price'] <= price_range[1])
filtered = filtered[price_mask]
filter_messages.append(f"💰 {len(filtered)} buses in price range ₹{price_range[0]}-₹{price_range[1]}")
seats_mask = filtered['seats_available'] >= st.session_state.seats_required
filtered = filtered[seats_mask]
filter_messages.append(f"🧑 {len(filtered)} buses with {st.session_state.seats_required}+ seats available")
with st.expander("Filter Progress", expanded=True):
for msg in filter_messages:
st.write(msg)
return filtered
return None
if st.session_state.from_location_select and st.session_state.to_location_select:
st.sidebar.markdown("**Departure Time**")
departure_time = st.sidebar.selectbox("Time of Departure", ["All", "Evening (6PM-12AM)", "Midnight (12AM-6AM)", "Morning (6AM-12PM)", "Afternoon (12PM-6PM)"], index=0)
st.sidebar.markdown("**Bus Type**")
ac_option = st.sidebar.radio("AC Type", ["All", "AC", "Non-AC"], index=0)
st.sidebar.markdown("**Seat Type**")
seat_type = st.sidebar.radio("Seating Type", ["All", "Sleeper", "Seater"], index=0)
st.sidebar.markdown("**Star Rating Range**")
star_min, star_max = st.sidebar.slider("Select star rating range", 0.0, 5.0, (0.0, 5.0), 0.5)
st.sidebar.markdown("**Price Range**")
price_range = st.sidebar.slider("Select price range", 0, 10000, (0, 10000), 100)
st.sidebar.markdown("**Passenger Details**")
seats_required = st.sidebar.number_input("Seats Required", min_value=1, max_value=20, value=1)
if st.sidebar.button("Search Buses"):
st.session_state.from_location = st.session_state.from_location_select
st.session_state.to_location = st.session_state.to_location_select
st.session_state.seats_required = seats_required
st.session_state.search_clicked = True
departure_state = get_state_in_india(st.session_state.from_location_select)
arrival_state = get_state_in_india(st.session_state.to_location_select)
st.write(f"\U0001F4CD **This state is for the 'Select departure' → `{st.session_state.from_location_select}`: `{departure_state}`**")
st.write(f"\U0001F4CD **This state is for the 'Select arrival' → `{st.session_state.to_location_select}`: `{arrival_state}`**")
if st.session_state.search_clicked:
filtered_df = filter_and_display_buses()
if filtered_df is not None and not filtered_df.empty:
ac_count = filtered_df['bus_type'].str.contains('AC', case=False).sum()
non_ac_count = len(filtered_df) - ac_count
total_available_seats = filtered_df['seats_available'].sum()
sleeper_available = filtered_df[filtered_df['bus_type'].str.contains('Sleeper', case=False)]['seats_available'].sum()
seater_available = total_available_seats - sleeper_available
st.write(f"### Available Buses from {st.session_state.from_location} to {st.session_state.to_location}")
st.write(f"**Found {len(filtered_df)} matching buses**")
st.write(f"- AC Buses: {ac_count} | Non-AC Buses: {non_ac_count}")
st.write(f"- Total Available Seats: {total_available_seats:,}")
st.write(f"- Sleeper Seats: {sleeper_available:,} | Seater Seats: {seater_available:,}")
default_columns = ['bus_name', 'bus_type', 'departing_time', 'reaching_time', 'duration', 'seats_available', 'price', 'star_rating']
selected_columns = st.multiselect("Select columns to view", filtered_df.columns.tolist(), default=default_columns)
if selected_columns:
display_df = filtered_df[selected_columns].copy()
if 'price' in display_df.columns:
display_df['price'] = display_df['price'].apply(lambda x: f"₹{x:,.2f}")
if 'seats_available' in display_df.columns:
display_df['seats_available'] = display_df['seats_available'].apply(lambda x: f"{x:,}")
st.dataframe(display_df, use_container_width=True)
else:
st.warning("Please select at least one column to display")
elif filtered_df is not None and filtered_df.empty:
st.warning("No buses available matching all your selected filters. Please adjust your filters.")
else:
st.warning("No buses available for this route")
else:
st.info("Please select your journey details and click 'Search Buses'")