Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,52 +36,9 @@ st.markdown(
|
|
| 36 |
unsafe_allow_html=True,
|
| 37 |
)
|
| 38 |
|
| 39 |
-
# Directory to save uploaded files
|
| 40 |
-
UPLOAD_DIR = "uploaded_files"
|
| 41 |
-
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 42 |
-
|
| 43 |
-
# Function to load data
|
| 44 |
-
@st.cache_data
|
| 45 |
-
def load_data(file_path):
|
| 46 |
-
df = pd.read_excel(file_path)
|
| 47 |
-
df.fillna("Not Available", inplace=True)
|
| 48 |
-
return df
|
| 49 |
-
|
| 50 |
-
# Function to convert dataframe to CSV
|
| 51 |
-
def convert_df_to_csv(df):
|
| 52 |
-
return df.to_csv(index=False).encode('utf-8')
|
| 53 |
-
|
| 54 |
-
# Function to format date
|
| 55 |
-
def format_date_column(df, column):
|
| 56 |
-
df[column] = pd.to_datetime(df[column], errors='coerce').dt.date
|
| 57 |
-
return df
|
| 58 |
-
|
| 59 |
-
# Function to verify required columns exist
|
| 60 |
-
def verify_columns(df, required_columns):
|
| 61 |
-
missing_columns = [col for col in required_columns if col not in df.columns]
|
| 62 |
-
return missing_columns
|
| 63 |
-
|
| 64 |
-
# Function to display data in tiles
|
| 65 |
-
def display_tiles(df, cols):
|
| 66 |
-
for i, (_, row) in enumerate(df.iterrows()):
|
| 67 |
-
col = cols[i % len(cols)]
|
| 68 |
-
with col:
|
| 69 |
-
st.markdown(f"**Name:** {row['Name']}")
|
| 70 |
-
st.markdown(f"**Cuisine:** {row['Cuisine']}")
|
| 71 |
-
st.markdown(f"**Location:** {row['Location']}")
|
| 72 |
-
st.markdown(f"**Restaurant Type:** {row['Restaurant Type']}")
|
| 73 |
-
st.markdown(f"**Expiry Date:** {row['Expiry Date']}")
|
| 74 |
-
st.markdown(f"**Website:** {row['Website']}")
|
| 75 |
-
st.markdown(f"**Directions:** {row['Directions']}")
|
| 76 |
-
st.markdown("---")
|
| 77 |
-
|
| 78 |
# Initialize session state
|
| 79 |
-
if 'df' not in st.session_state:
|
| 80 |
-
st.session_state.df = None
|
| 81 |
if 'authenticated' not in st.session_state:
|
| 82 |
st.session_state.authenticated = False
|
| 83 |
-
if 'just_logged_in' not in st.session_state:
|
| 84 |
-
st.session_state.just_logged_in = False
|
| 85 |
|
| 86 |
# List of valid usernames and passwords
|
| 87 |
user_credentials = {
|
|
@@ -107,135 +64,11 @@ if not st.session_state.authenticated:
|
|
| 107 |
if st.button("Login"):
|
| 108 |
if authenticate(username, password):
|
| 109 |
st.session_state.authenticated = True
|
| 110 |
-
st.session_state.just_logged_in = True
|
| 111 |
trigger_rerun()
|
| 112 |
else:
|
| 113 |
st.error("Invalid username or password")
|
| 114 |
-
elif st.session_state.just_logged_in:
|
| 115 |
-
st.session_state.just_logged_in = False
|
| 116 |
-
trigger_rerun()
|
| 117 |
else:
|
| 118 |
-
|
| 119 |
-
with st.sidebar:
|
| 120 |
-
st.image(SIDEBAR_LOGO_URL, use_column_width=True)
|
| 121 |
-
|
| 122 |
-
st.title("File Management")
|
| 123 |
-
|
| 124 |
-
# File uploader
|
| 125 |
-
uploaded_file = st.file_uploader("Choose an Excel file", type="xlsx")
|
| 126 |
-
|
| 127 |
-
if uploaded_file:
|
| 128 |
-
try:
|
| 129 |
-
# Save the uploaded file
|
| 130 |
-
file_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
|
| 131 |
-
with open(file_path, "wb") as f:
|
| 132 |
-
f.write(uploaded_file.getbuffer())
|
| 133 |
-
|
| 134 |
-
st.success(f"File '{uploaded_file.name}' uploaded successfully.")
|
| 135 |
-
|
| 136 |
-
df = load_data(file_path)
|
| 137 |
-
st.session_state.df = df
|
| 138 |
-
|
| 139 |
-
except Exception as e:
|
| 140 |
-
st.error(f"An error occurred: {e}")
|
| 141 |
-
|
| 142 |
-
# Manage existing files
|
| 143 |
-
st.subheader("Manage Existing Files")
|
| 144 |
-
existing_files = [f for f in os.listdir(UPLOAD_DIR) if f.endswith(".xlsx")]
|
| 145 |
-
if existing_files:
|
| 146 |
-
selected_file = st.selectbox("Select a file to view or delete", existing_files)
|
| 147 |
-
if st.button("Delete Selected File"):
|
| 148 |
-
os.remove(os.path.join(UPLOAD_DIR, selected_file))
|
| 149 |
-
st.success(f"File '{selected_file}' deleted successfully.")
|
| 150 |
-
trigger_rerun()
|
| 151 |
-
|
| 152 |
-
if st.button("Load Selected File") or st.session_state.df is None:
|
| 153 |
-
try:
|
| 154 |
-
file_path = os.path.join(UPLOAD_DIR, selected_file)
|
| 155 |
-
df = load_data(file_path)
|
| 156 |
-
st.session_state.df = df
|
| 157 |
-
except Exception as e:
|
| 158 |
-
st.error(f"An error occurred: {e}")
|
| 159 |
-
else:
|
| 160 |
-
st.info("No files available.")
|
| 161 |
-
|
| 162 |
-
# Display the main logo
|
| 163 |
st.image(MAIN_LOGO_URL, use_column_width=True)
|
| 164 |
-
|
| 165 |
st.title("Restaurant Data Viewer")
|
| 166 |
-
|
| 167 |
-
# Display and filter the loaded dataframe if available
|
| 168 |
-
if st.session_state.df is not None:
|
| 169 |
-
df = st.session_state.df
|
| 170 |
-
|
| 171 |
-
# Define required columns
|
| 172 |
-
required_columns = ['Name', 'Cuisine', 'Location', 'Restaurant Type', 'Expiry Date', 'Website', 'Directions']
|
| 173 |
-
|
| 174 |
-
# Verify required columns
|
| 175 |
-
missing_columns = verify_columns(df, required_columns)
|
| 176 |
-
if missing_columns:
|
| 177 |
-
st.error(f"The following required columns are missing from the uploaded file: {', '.join(missing_columns)}")
|
| 178 |
-
else:
|
| 179 |
-
# Format the expiry date column to remove time
|
| 180 |
-
df = format_date_column(df, 'Expiry Date')
|
| 181 |
-
|
| 182 |
-
# Display the dataframe
|
| 183 |
-
st.subheader("Loaded Data")
|
| 184 |
-
st.dataframe(df)
|
| 185 |
-
|
| 186 |
-
# Filter functionality
|
| 187 |
-
st.subheader("Filters")
|
| 188 |
-
# Use columns for a more responsive layout
|
| 189 |
-
col1, col2, col3, col4, col5 = st.columns(5)
|
| 190 |
-
|
| 191 |
-
with col1:
|
| 192 |
-
# Filter by Name
|
| 193 |
-
name_filter = st.text_input("Name contains")
|
| 194 |
-
with col2:
|
| 195 |
-
# Filter by Cuisine
|
| 196 |
-
cuisine_filter = st.multiselect("Cuisine", sorted(df['Cuisine'].unique()))
|
| 197 |
-
with col3:
|
| 198 |
-
# Filter by Location
|
| 199 |
-
location_filter = st.multiselect("Location", df['Location'].unique())
|
| 200 |
-
with col4:
|
| 201 |
-
# Filter by Restaurant Type
|
| 202 |
-
restaurant_type_filter = st.multiselect("Restaurant Type", df['Restaurant Type'].unique())
|
| 203 |
-
with col5:
|
| 204 |
-
# Filter by Expiry Date
|
| 205 |
-
expiry_date_filter = st.date_input("Expiry Date", [])
|
| 206 |
-
|
| 207 |
-
# Apply filters
|
| 208 |
-
filtered_df = df.copy()
|
| 209 |
-
|
| 210 |
-
if name_filter:
|
| 211 |
-
filtered_df = filtered_df[filtered_df['Name'].str.contains(name_filter, case=False, na=False)]
|
| 212 |
-
if cuisine_filter:
|
| 213 |
-
filtered_df = filtered_df[filtered_df['Cuisine'].isin(cuisine_filter)]
|
| 214 |
-
if location_filter:
|
| 215 |
-
filtered_df = filtered_df[filtered_df['Location'].isin(location_filter)]
|
| 216 |
-
if restaurant_type_filter:
|
| 217 |
-
filtered_df = filtered_df[filtered_df['Restaurant Type'].isin(restaurant_type_filter)]
|
| 218 |
-
if expiry_date_filter:
|
| 219 |
-
if len(expiry_date_filter) == 1:
|
| 220 |
-
filtered_df = filtered_df[filtered_df['Expiry Date'] == expiry_date_filter[0]]
|
| 221 |
-
elif len(expiry_date_filter) == 2:
|
| 222 |
-
start_date, end_date = expiry_date_filter
|
| 223 |
-
filtered_df = filtered_df[(filtered_df['Expiry Date'] >= start_date) & (filtered_df['Expiry Date'] <= end_date)]
|
| 224 |
-
|
| 225 |
-
# Display the filtered dataframe in a tile format
|
| 226 |
-
st.subheader("Filtered Data")
|
| 227 |
-
if not filtered_df.empty:
|
| 228 |
-
num_cols = 3 # Number of columns for the tile layout
|
| 229 |
-
cols = st.columns(num_cols)
|
| 230 |
-
display_tiles(filtered_df, cols)
|
| 231 |
-
|
| 232 |
-
# Download button for filtered data
|
| 233 |
-
csv = convert_df_to_csv(filtered_df)
|
| 234 |
-
st.download_button(
|
| 235 |
-
label="Download filtered data as CSV",
|
| 236 |
-
data=csv,
|
| 237 |
-
file_name='filtered_data.csv',
|
| 238 |
-
mime='text/csv',
|
| 239 |
-
)
|
| 240 |
-
else:
|
| 241 |
-
st.info("No data matches the filter criteria.")
|
|
|
|
| 36 |
unsafe_allow_html=True,
|
| 37 |
)
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
# Initialize session state
|
|
|
|
|
|
|
| 40 |
if 'authenticated' not in st.session_state:
|
| 41 |
st.session_state.authenticated = False
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# List of valid usernames and passwords
|
| 44 |
user_credentials = {
|
|
|
|
| 64 |
if st.button("Login"):
|
| 65 |
if authenticate(username, password):
|
| 66 |
st.session_state.authenticated = True
|
|
|
|
| 67 |
trigger_rerun()
|
| 68 |
else:
|
| 69 |
st.error("Invalid username or password")
|
|
|
|
|
|
|
|
|
|
| 70 |
else:
|
| 71 |
+
st.write("User authenticated successfully")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
st.image(MAIN_LOGO_URL, use_column_width=True)
|
|
|
|
| 73 |
st.title("Restaurant Data Viewer")
|
| 74 |
+
st.write("Main content should be here.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|