zayeem00 commited on
Commit
7612948
·
verified ·
1 Parent(s): 1e1fd13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +160 -1
app.py CHANGED
@@ -36,7 +36,48 @@ st.markdown(
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
 
@@ -71,4 +112,122 @@ 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.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 
 
112
  st.write("User authenticated successfully")
113
  st.image(MAIN_LOGO_URL, use_column_width=True)
114
  st.title("Restaurant Data Viewer")
115
+
116
+ # File upload logic
117
+ with st.sidebar:
118
+ st.image(SIDEBAR_LOGO_URL, use_column_width=True)
119
+ st.title("File Management")
120
+
121
+ # File uploader
122
+ uploaded_file = st.file_uploader("Choose an Excel file", type="xlsx")
123
+
124
+ if uploaded_file:
125
+ try:
126
+ # Save the uploaded file
127
+ file_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
128
+ with open(file_path, "wb") as f:
129
+ f.write(uploaded_file.getbuffer())
130
+
131
+ st.success(f"File '{uploaded_file.name}' uploaded successfully.")
132
+
133
+ df = load_data(file_path)
134
+ st.session_state.df = df
135
+
136
+ except Exception as e:
137
+ st.error(f"An error occurred: {e}")
138
+
139
+ # Manage existing files
140
+ st.subheader("Manage Existing Files")
141
+ existing_files = [f for f in os.listdir(UPLOAD_DIR) if f.endswith(".xlsx")]
142
+ if existing_files:
143
+ selected_file = st.selectbox("Select a file to view or delete", existing_files)
144
+ if st.button("Delete Selected File"):
145
+ os.remove(os.path.join(UPLOAD_DIR, selected_file))
146
+ st.success(f"File '{selected_file}' deleted successfully.")
147
+ trigger_rerun()
148
+
149
+ if st.button("Load Selected File") or st.session_state.df is None:
150
+ try:
151
+ file_path = os.path.join(UPLOAD_DIR, selected_file)
152
+ df = load_data(file_path)
153
+ st.session_state.df = df
154
+ except Exception as e:
155
+ st.error(f"An error occurred: {e}")
156
+ else:
157
+ st.info("No files available.")
158
+
159
+ # Display and filter the loaded dataframe if available
160
+ if st.session_state.df is not None:
161
+ df = st.session_state.df
162
+
163
+ # Define required columns
164
+ required_columns = ['Name', 'Cuisine', 'Location', 'Restaurant Type', 'Expiry Date', 'Website', 'Directions']
165
+
166
+ # Verify required columns
167
+ missing_columns = verify_columns(df, required_columns)
168
+ if missing_columns:
169
+ st.error(f"The following required columns are missing from the uploaded file: {', '.join(missing_columns)}")
170
+ else:
171
+ # Format the expiry date column to remove time
172
+ df = format_date_column(df, 'Expiry Date')
173
+
174
+ # Display the dataframe
175
+ st.subheader("Loaded Data")
176
+ st.dataframe(df)
177
+
178
+ # Filter functionality
179
+ st.subheader("Filters")
180
+ # Use columns for a more responsive layout
181
+ col1, col2, col3, col4, col5 = st.columns(5)
182
+
183
+ with col1:
184
+ # Filter by Name
185
+ name_filter = st.text_input("Name contains")
186
+ with col2:
187
+ # Filter by Cuisine
188
+ cuisine_filter = st.multiselect("Cuisine", sorted(df['Cuisine'].unique()))
189
+ with col3:
190
+ # Filter by Location
191
+ location_filter = st.multiselect("Location", df['Location'].unique())
192
+ with col4:
193
+ # Filter by Restaurant Type
194
+ restaurant_type_filter = st.multiselect("Restaurant Type", df['Restaurant Type'].unique())
195
+ with col5:
196
+ # Filter by Expiry Date
197
+ expiry_date_filter = st.date_input("Expiry Date", [])
198
+
199
+ # Apply filters
200
+ filtered_df = df.copy()
201
+
202
+ if name_filter:
203
+ filtered_df = filtered_df[filtered_df['Name'].str.contains(name_filter, case=False, na=False)]
204
+ if cuisine_filter:
205
+ filtered_df = filtered_df[filtered_df['Cuisine'].isin(cuisine_filter)]
206
+ if location_filter:
207
+ filtered_df = filtered_df[filtered_df['Location'].isin(location_filter)]
208
+ if restaurant_type_filter:
209
+ filtered_df = filtered_df[filtered_df['Restaurant Type'].isin(restaurant_type_filter)]
210
+ if expiry_date_filter:
211
+ if len(expiry_date_filter) == 1:
212
+ filtered_df = filtered_df[filtered_df['Expiry Date'] == expiry_date_filter[0]]
213
+ elif len(expiry_date_filter) == 2:
214
+ start_date, end_date = expiry_date_filter
215
+ filtered_df = filtered_df[(filtered_df['Expiry Date'] >= start_date) & (filtered_df['Expiry Date'] <= end_date)]
216
+
217
+ # Display the filtered dataframe in a tile format
218
+ st.subheader("Filtered Data")
219
+ if not filtered_df.empty:
220
+ num_cols = 3 # Number of columns for the tile layout
221
+ cols = st.columns(num_cols)
222
+ display_tiles(filtered_df, cols)
223
+
224
+ # Download button for filtered data
225
+ csv = convert_df_to_csv(filtered_df)
226
+ st.download_button(
227
+ label="Download filtered data as CSV",
228
+ data=csv,
229
+ file_name='filtered_data.csv',
230
+ mime='text/csv',
231
+ )
232
+ else:
233
+ st.info("No data matches the filter criteria.")