Krish-Upgrix commited on
Commit
0de06d3
·
verified ·
1 Parent(s): 82fa6cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -108
app.py CHANGED
@@ -1,111 +1,3 @@
1
- import streamlit as st
2
- import firebase_admin
3
- from firebase_admin import credentials, db
4
- import base64
5
- from io import BytesIO
6
- from PIL import Image
7
- import requests
8
-
9
- # Initialize Firebase Realtime Database
10
- if not firebase_admin._apps:
11
- cred = credentials.Certificate("firebase_credentials.json")
12
- firebase_admin.initialize_app(cred, {
13
- 'databaseURL': 'https://binsight-beda0-default-rtdb.asia-southeast1.firebasedatabase.app/'
14
- })
15
-
16
- # Firebase Authentication REST API
17
- FIREBASE_AUTH_URL = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyCwYZcPI5pltxxHiMUxjmQUNIaObNXWydw"
18
-
19
- # Streamlit UI
20
- st.title("Binsight Admin Dashboard")
21
-
22
- # Session state for authentication
23
- if "authenticated" not in st.session_state:
24
- st.session_state.authenticated = False
25
- if "user" not in st.session_state:
26
- st.session_state.user = None
27
-
28
- # Authentication UI
29
- def login():
30
- st.subheader("Login")
31
- email = st.text_input("Email")
32
- password = st.text_input("Password", type="password")
33
- if st.button("Login"):
34
- payload = {"email": email, "password": password, "returnSecureToken": True}
35
- response = requests.post(FIREBASE_AUTH_URL, json=payload)
36
- if response.status_code == 200:
37
- st.session_state.authenticated = True
38
- st.session_state.user = email
39
- st.rerun()
40
- else:
41
- st.error("Invalid credentials or user does not exist.")
42
-
43
- # Logout functionality
44
- def logout():
45
- st.session_state.authenticated = False
46
- st.session_state.user = None
47
- st.rerun()
48
-
49
- # Main Dashboard
50
- if st.session_state.authenticated:
51
- st.sidebar.button("Logout", on_click=logout)
52
- st.sidebar.header("Filters")
53
- filter_status = st.sidebar.selectbox("Filter by Status", ["All", "Pending", "Allocated", "Completed"], index=0)
54
-
55
- dustbins_ref = db.reference("dustbins")
56
- dustbins = dustbins_ref.get() or {}
57
-
58
- filtered_dustbins = {
59
- key: value for key, value in dustbins.items()
60
- if filter_status == "All" or value["status"] == filter_status
61
- }
62
-
63
- st.subheader(f"Showing {filter_status.lower()} dustbins ({len(filtered_dustbins)})")
64
-
65
- for key, value in filtered_dustbins.items():
66
- with st.expander(f"📍 {value['address']}"):
67
- st.write(f"**Latitude**: {value['latitude']}")
68
- st.write(f"**Longitude**: {value['longitude']}")
69
- st.write(f"**Status**: {value['status']}")
70
- # st.write(f"**User**: {value['user_email']}")
71
-
72
- # Display Image
73
- if "image" in value:
74
- image_data = base64.b64decode(value["image"])
75
- st.image(Image.open(BytesIO(image_data)), caption="Dustbin Image", use_column_width=True)
76
-
77
- if value["status"] == "Pending":
78
- truck_email = st.text_input(f"Allocate Truck Email for {key}", key=f"truck_{key}")
79
- if st.button(f"Allocate Truck", key=f"allocate_{key}"):
80
- if truck_email:
81
- dustbins_ref.child(key).update({
82
- "allocated_truck": truck_email,
83
- "status": "Allocated"
84
- })
85
- st.success("Truck allocated successfully!")
86
- else:
87
- st.error("Please enter a valid truck email.")
88
-
89
- # Back button to redirect to dashboard
90
- st.markdown("<br>", unsafe_allow_html=True)
91
- st.markdown("<a href='https://binsight.onrender.com/dashboard.html' target='_self' style='text-decoration:none;'><button style='padding: 10px 20px; font-size: 16px;'>⬅ Back to Dashboard</button></a>", unsafe_allow_html=True)
92
- else:
93
- login()
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
- # Best version without back button
108
-
109
  # import streamlit as st
110
  # import firebase_admin
111
  # from firebase_admin import credentials, db
@@ -193,6 +85,10 @@ else:
193
  # st.success("Truck allocated successfully!")
194
  # else:
195
  # st.error("Please enter a valid truck email.")
 
 
 
 
196
  # else:
197
  # login()
198
 
@@ -208,6 +104,110 @@ else:
208
 
209
 
210
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
 
212
 
213
  ## Below code is working but it is not having login of admin. that is accessible to anyone..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # import streamlit as st
2
  # import firebase_admin
3
  # from firebase_admin import credentials, db
 
85
  # st.success("Truck allocated successfully!")
86
  # else:
87
  # st.error("Please enter a valid truck email.")
88
+
89
+ # # Back button to redirect to dashboard
90
+ # st.markdown("<br>", unsafe_allow_html=True)
91
+ # st.markdown("<a href='https://binsight.onrender.com/dashboard.html' target='_self' style='text-decoration:none;'><button style='padding: 10px 20px; font-size: 16px;'>⬅ Back to Dashboard</button></a>", unsafe_allow_html=True)
92
  # else:
93
  # login()
94
 
 
104
 
105
 
106
 
107
+ # Best version without back button
108
+
109
+ import streamlit as st
110
+ import firebase_admin
111
+ from firebase_admin import credentials, db
112
+ import base64
113
+ from io import BytesIO
114
+ from PIL import Image
115
+ import requests
116
+
117
+ # Initialize Firebase Realtime Database
118
+ if not firebase_admin._apps:
119
+ cred = credentials.Certificate("firebase_credentials.json")
120
+ firebase_admin.initialize_app(cred, {
121
+ 'databaseURL': 'https://binsight-beda0-default-rtdb.asia-southeast1.firebasedatabase.app/'
122
+ })
123
+
124
+ # Firebase Authentication REST API
125
+ FIREBASE_AUTH_URL = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyCwYZcPI5pltxxHiMUxjmQUNIaObNXWydw"
126
+
127
+ # Streamlit UI
128
+ st.title("Binsight Admin Dashboard")
129
+
130
+ # Session state for authentication
131
+ if "authenticated" not in st.session_state:
132
+ st.session_state.authenticated = False
133
+ if "user" not in st.session_state:
134
+ st.session_state.user = None
135
+
136
+ # Authentication UI
137
+ def login():
138
+ st.subheader("Login")
139
+ email = st.text_input("Email")
140
+ password = st.text_input("Password", type="password")
141
+ if st.button("Login"):
142
+ payload = {"email": email, "password": password, "returnSecureToken": True}
143
+ response = requests.post(FIREBASE_AUTH_URL, json=payload)
144
+ if response.status_code == 200:
145
+ st.session_state.authenticated = True
146
+ st.session_state.user = email
147
+ st.rerun()
148
+ else:
149
+ st.error("Invalid credentials or user does not exist.")
150
+
151
+ # Logout functionality
152
+ def logout():
153
+ st.session_state.authenticated = False
154
+ st.session_state.user = None
155
+ st.rerun()
156
+
157
+ # Main Dashboard
158
+ if st.session_state.authenticated:
159
+ st.sidebar.button("Logout", on_click=logout)
160
+ st.sidebar.header("Filters")
161
+ filter_status = st.sidebar.selectbox("Filter by Status", ["All", "Pending", "Allocated", "Completed"], index=0)
162
+
163
+ dustbins_ref = db.reference("dustbins")
164
+ dustbins = dustbins_ref.get() or {}
165
+
166
+ filtered_dustbins = {
167
+ key: value for key, value in dustbins.items()
168
+ if filter_status == "All" or value["status"] == filter_status
169
+ }
170
+
171
+ st.subheader(f"Showing {filter_status.lower()} dustbins ({len(filtered_dustbins)})")
172
+
173
+ for key, value in filtered_dustbins.items():
174
+ with st.expander(f"📍 {value['address']}"):
175
+ st.write(f"**Latitude**: {value['latitude']}")
176
+ st.write(f"**Longitude**: {value['longitude']}")
177
+ st.write(f"**Status**: {value['status']}")
178
+ # st.write(f"**User**: {value['user_email']}")
179
+
180
+ # Display Image
181
+ if "image" in value:
182
+ image_data = base64.b64decode(value["image"])
183
+ st.image(Image.open(BytesIO(image_data)), caption="Dustbin Image", use_column_width=True)
184
+
185
+ if value["status"] == "Pending":
186
+ truck_email = st.text_input(f"Allocate Truck Email for {key}", key=f"truck_{key}")
187
+ if st.button(f"Allocate Truck", key=f"allocate_{key}"):
188
+ if truck_email:
189
+ dustbins_ref.child(key).update({
190
+ "allocated_truck": truck_email,
191
+ "status": "Allocated"
192
+ })
193
+ st.success("Truck allocated successfully!")
194
+ else:
195
+ st.error("Please enter a valid truck email.")
196
+ else:
197
+ login()
198
+
199
+
200
+
201
+
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+
210
+
211
 
212
 
213
  ## Below code is working but it is not having login of admin. that is accessible to anyone..