-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
114 lines (97 loc) · 3.91 KB
/
Copy pathmain.py
File metadata and controls
114 lines (97 loc) · 3.91 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
# lost_and_found_app_v2.py
import streamlit as st
import pandas as pd
from datetime import datetime, timedelta
import os
from PIL import Image
# --- Setup ---
st.set_page_config(page_title="Lost & Found | College Campus", page_icon="🎒", layout="wide")
st.markdown("<h1 style='text-align: center; color: #4CAF50;'>🎒 Lost and Found Portal</h1>", unsafe_allow_html=True)
UPLOAD_DIR = "uploads"
if not os.path.exists(UPLOAD_DIR):
os.makedirs(UPLOAD_DIR)
DB_FILE = "lost_found_db.csv"
if os.path.exists(DB_FILE):
db = pd.read_csv(DB_FILE)
else:
db = pd.DataFrame(columns=["Type", "Item Name", "Description", "Location", "Date", "Contact", "Image Path"])
# --- Sidebar Menu ---
st.sidebar.header("📋 Navigation")
menu = st.sidebar.radio("Choose an option", ["🏷 Report Item", "🔍 View Items", "🔎 Search"])
# --- Functions ---
def save_to_database(entry):
global db
db = pd.concat([db, pd.DataFrame([entry])], ignore_index=True)
db.to_csv(DB_FILE, index=False)
def upload_image(file):
if file:
filepath = os.path.join(UPLOAD_DIR, f"{datetime.now().strftime('%Y%m%d%H%M%S')}_{file.name}")
with open(filepath, "wb") as f:
f.write(file.getbuffer())
return filepath
return None
def display_item_card(row):
col1, col2 = st.columns([1, 2])
with col1:
if pd.notna(row['Image Path']):
st.image(row['Image Path'], width=150)
with col2:
st.markdown(f"### {row['Item Name']} {'🛑' if row['Type']=='Lost' else '✅'}")
st.markdown(f"**Type:** {row['Type']}")
st.markdown(f"**Location:** {row['Location']}")
st.markdown(f"**Date:** {row['Date']}")
st.markdown(f"**Description:** {row['Description']}")
st.markdown(f"**Contact:** {row['Contact']}")
st.markdown("---")
# --- Pages ---
if menu == "🏷 Report Item":
st.subheader("📝 Report a Lost or Found Item")
item_type = st.selectbox("Type", ["Lost", "Found"])
item_name = st.text_input("Item Name")
description = st.text_area("Description")
location = st.text_input("Location where item was lost/found")
date = st.date_input("Date")
contact = st.text_input("Your Contact Information (Phone / Email)")
image = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
if st.button("Submit"):
if item_name and description and location and contact:
image_path = upload_image(image)
entry = {
"Type": item_type,
"Item Name": item_name,
"Description": description,
"Location": location,
"Date": date.strftime("%Y-%m-%d"),
"Contact": contact,
"Image Path": image_path
}
save_to_database(entry)
st.success(f"{item_type} item reported successfully!")
else:
st.error("Please fill all the required fields!")
elif menu == "🔍 View Items":
st.subheader("📋 List of Items")
item_type_filter = st.selectbox("Filter by Type", ["All", "Lost", "Found"])
if item_type_filter == "All":
filtered_db = db
else:
filtered_db = db[db['Type'] == item_type_filter]
if filtered_db.empty:
st.warning("No items reported yet.")
else:
for idx, row in filtered_db.iterrows():
display_item_card(row)
elif menu == "🔎 Search":
st.subheader("🔍 Search Items")
keyword = st.text_input("Enter item name, description, or location to search")
if keyword:
results = db[
db["Item Name"].str.contains(keyword, case=False, na=False) |
db["Description"].str.contains(keyword, case=False, na=False) |
db["Location"].str.contains(keyword, case=False, na=False)
]
if results.empty:
st.warning("No matching items found.")
else:
for idx, row in results.iterrows():
display_item_card(row)