Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import firebase_admin | |
| from firebase_admin import credentials, db | |
| from datetime import datetime | |
| import base64 | |
| from io import BytesIO | |
| from PIL import Image | |
| import requests | |
| # Initialize Firebase Realtime Database | |
| if not firebase_admin._apps: | |
| cred = credentials.Certificate("firebase_credentials.json") | |
| firebase_admin.initialize_app(cred, { | |
| 'databaseURL': 'https://binsight-beda0-default-rtdb.asia-southeast1.firebasedatabase.app/' | |
| }) | |
| # Firebase Authentication REST API | |
| FIREBASE_AUTH_URL = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyAj-0voQamIhPvnWZPd_SwkhlOR1pXynvg" | |
| st.title("Binsight Driver Dashboard") | |
| # Session state for authentication | |
| if "authenticated" not in st.session_state: | |
| st.session_state.authenticated = False | |
| if "driver_email" not in st.session_state: | |
| st.session_state.driver_email = None | |
| # Authentication UI | |
| st.sidebar.header("Driver Login") | |
| def login(): | |
| email = st.sidebar.text_input("Enter your email") | |
| password = st.sidebar.text_input("Enter your password", type="password") | |
| if st.sidebar.button("Login"): | |
| payload = {"email": email, "password": password, "returnSecureToken": True} | |
| response = requests.post(FIREBASE_AUTH_URL, json=payload) | |
| if response.status_code == 200: | |
| st.session_state.authenticated = True | |
| st.session_state.driver_email = email | |
| st.sidebar.success(f"Logged in as {email}") | |
| st.rerun() | |
| else: | |
| st.sidebar.error("Invalid credentials or user does not exist.") | |
| # Logout functionality | |
| def logout(): | |
| st.session_state.authenticated = False | |
| st.session_state.driver_email = None | |
| st.rerun() | |
| if st.session_state.authenticated: | |
| st.sidebar.button("Logout", on_click=logout) | |
| else: | |
| login() | |
| st.stop() | |
| # Fetch allocated tasks | |
| dustbins_ref = db.reference("dustbins") | |
| dustbins = dustbins_ref.get() or {} | |
| allocated_tasks = { | |
| key: value for key, value in dustbins.items() | |
| if value.get("allocated_truck") == st.session_state["driver_email"] and value.get("status") == "Allocated" | |
| } | |
| st.subheader(f"Allocated Tasks ({len(allocated_tasks)})") | |
| for key, value in allocated_tasks.items(): | |
| with st.expander(f"π Task at {value['address']}"): | |
| st.write(f"**Latitude**: {value['latitude']}") | |
| st.write(f"**Longitude**: {value['longitude']}") | |
| if "image" in value: | |
| image_data = base64.b64decode(value["image"]) | |
| st.image(Image.open(BytesIO(image_data)), caption="Dustbin Image", use_column_width=True) | |
| if st.button(f"Mark as Done", key=f"done_{key}"): | |
| dustbins_ref.child(key).update({ | |
| "status": "Completed", | |
| "completed_at": str(datetime.now()), | |
| "completed_by": st.session_state["driver_email"] | |
| }) | |
| st.success(f"Task at {value['address']} marked as completed!") | |
| # Back button to redirect to dashboard | |
| st.markdown("<br>", unsafe_allow_html=True) | |
| 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) | |
| # Best version without back button | |
| # import streamlit as st | |
| # import firebase_admin | |
| # from firebase_admin import credentials, db | |
| # from datetime import datetime | |
| # import base64 | |
| # from io import BytesIO | |
| # from PIL import Image | |
| # import requests | |
| # # Initialize Firebase Realtime Database | |
| # if not firebase_admin._apps: | |
| # cred = credentials.Certificate("firebase_credentials.json") | |
| # firebase_admin.initialize_app(cred, { | |
| # 'databaseURL': 'https://binsight-beda0-default-rtdb.asia-southeast1.firebasedatabase.app/' | |
| # }) | |
| # # Firebase Authentication REST API | |
| # FIREBASE_AUTH_URL = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyAj-0voQamIhPvnWZPd_SwkhlOR1pXynvg" | |
| # st.title("Binsight Driver Dashboard") | |
| # # Session state for authentication | |
| # if "authenticated" not in st.session_state: | |
| # st.session_state.authenticated = False | |
| # if "driver_email" not in st.session_state: | |
| # st.session_state.driver_email = None | |
| # # Authentication UI | |
| # st.sidebar.header("Driver Login") | |
| # def login(): | |
| # email = st.sidebar.text_input("Enter your email") | |
| # password = st.sidebar.text_input("Enter your password", type="password") | |
| # if st.sidebar.button("Login"): | |
| # payload = {"email": email, "password": password, "returnSecureToken": True} | |
| # response = requests.post(FIREBASE_AUTH_URL, json=payload) | |
| # if response.status_code == 200: | |
| # st.session_state.authenticated = True | |
| # st.session_state.driver_email = email | |
| # st.sidebar.success(f"Logged in as {email}") | |
| # st.rerun() | |
| # else: | |
| # st.sidebar.error("Invalid credentials or user does not exist.") | |
| # # Logout functionality | |
| # def logout(): | |
| # st.session_state.authenticated = False | |
| # st.session_state.driver_email = None | |
| # st.rerun() | |
| # if st.session_state.authenticated: | |
| # st.sidebar.button("Logout", on_click=logout) | |
| # else: | |
| # login() | |
| # st.stop() | |
| # # Fetch allocated tasks | |
| # dustbins_ref = db.reference("dustbins") | |
| # dustbins = dustbins_ref.get() or {} | |
| # allocated_tasks = { | |
| # key: value for key, value in dustbins.items() | |
| # if value.get("allocated_truck") == st.session_state["driver_email"] and value.get("status") == "Allocated" | |
| # } | |
| # st.subheader(f"Allocated Tasks ({len(allocated_tasks)})") | |
| # for key, value in allocated_tasks.items(): | |
| # with st.expander(f"π Task at {value['address']}"): | |
| # st.write(f"**Latitude**: {value['latitude']}") | |
| # st.write(f"**Longitude**: {value['longitude']}") | |
| # if "image" in value: | |
| # image_data = base64.b64decode(value["image"]) | |
| # st.image(Image.open(BytesIO(image_data)), caption="Dustbin Image", use_column_width=True) | |
| # if st.button(f"Mark as Done", key=f"done_{key}"): | |
| # dustbins_ref.child(key).update({ | |
| # "status": "Completed", | |
| # "completed_at": str(datetime.now()), | |
| # "completed_by": st.session_state["driver_email"] | |
| # }) | |
| # st.success(f"Task at {value['address']} marked as completed!") | |
| ## Below code is working but it is doesn't having login proper for user. anyone can login | |
| # import streamlit as st | |
| # import firebase_admin | |
| # from firebase_admin import credentials, db | |
| # from datetime import datetime | |
| # import base64 | |
| # from io import BytesIO | |
| # from PIL import Image | |
| # # Initialize Firebase (Check if already initialized) | |
| # if not firebase_admin._apps: | |
| # cred = credentials.Certificate("firebase_credentials.json") | |
| # firebase_admin.initialize_app(cred, { | |
| # 'databaseURL': 'https://binsight-beda0-default-rtdb.asia-southeast1.firebasedatabase.app/' | |
| # }) | |
| # st.title("Binsight Driver Dashboard") | |
| # st.sidebar.header("Driver Login") | |
| # driver_email = st.sidebar.text_input("Enter your email") | |
| # login_button = st.sidebar.button("Login") | |
| # if login_button and driver_email: | |
| # st.session_state["driver_email"] = driver_email | |
| # st.sidebar.success(f"Logged in as {driver_email}") | |
| # if "driver_email" not in st.session_state: | |
| # st.warning("Please log in first.") | |
| # st.stop() | |
| # dustbins_ref = db.reference("dustbins") | |
| # dustbins = dustbins_ref.get() or {} | |
| # allocated_tasks = { | |
| # key: value for key, value in dustbins.items() | |
| # if value.get("allocated_truck") == st.session_state["driver_email"] and value.get("status") == "Allocated" | |
| # } | |
| # st.subheader(f"Allocated Tasks ({len(allocated_tasks)})") | |
| # for key, value in allocated_tasks.items(): | |
| # with st.expander(f"π Task at {value['address']}"): | |
| # st.write(f"**Latitude**: {value['latitude']}") | |
| # st.write(f"**Longitude**: {value['longitude']}") | |
| # if "image" in value: | |
| # image_data = base64.b64decode(value["image"]) | |
| # st.image(Image.open(BytesIO(image_data)), caption="Dustbin Image", use_column_width=True) | |
| # if st.button(f"Mark as Done", key=f"done_{key}"): | |
| # dustbins_ref.child(key).update({ | |
| # "status": "Completed", | |
| # "completed_at": str(datetime.now()), | |
| # "completed_by": st.session_state["driver_email"] | |
| # }) | |
| # st.success(f"Task at {value['address']} marked as completed!") | |
| # # Best without image | |
| # # import streamlit as st | |
| # # import firebase_admin | |
| # # from firebase_admin import credentials, db | |
| # # from datetime import datetime | |
| # # # Initialize Firebase | |
| # # if not firebase_admin._apps: | |
| # # cred = credentials.Certificate("firebase_credentials.json") | |
| # # firebase_admin.initialize_app(cred, { | |
| # # 'databaseURL': 'https://binsight-beda0-default-rtdb.asia-southeast1.firebasedatabase.app/' | |
| # # }) | |
| # # st.title("Binsight Driver Dashboard") | |
| # # st.sidebar.header("Driver Login") | |
| # # driver_email = st.sidebar.text_input("Enter your email") | |
| # # login_button = st.sidebar.button("Login") | |
| # # if login_button and driver_email: | |
| # # st.session_state["driver_email"] = driver_email | |
| # # st.sidebar.success(f"Logged in as {driver_email}") | |
| # # if "driver_email" not in st.session_state: | |
| # # st.warning("Please log in first.") | |
| # # st.stop() | |
| # # dustbins_ref = db.reference("dustbins") | |
| # # dustbins = dustbins_ref.get() or {} | |
| # # allocated_tasks = { | |
| # # key: value for key, value in dustbins.items() | |
| # # if value.get("allocated_truck") == st.session_state["driver_email"] and value.get("status") == "Allocated" | |
| # # } | |
| # # st.subheader(f"Allocated Tasks ({len(allocated_tasks)})") | |
| # # for key, value in allocated_tasks.items(): | |
| # # with st.expander(f"π Task at {value['address']}"): | |
| # # st.write(f"**Latitude**: {value['latitude']}") | |
| # # st.write(f"**Longitude**: {value['longitude']}") | |
| # # st.write(f"**Classification**: {', '.join([f'{k} ({v:.2f})' for k, v in value['classification'].items()])}") | |
| # # if st.button(f"Mark as Done", key=f"done_{key}"): | |
| # # dustbins_ref.child(key).update({ | |
| # # "status": "Completed", | |
| # # "completed_at": str(datetime.now()), | |
| # # "completed_by": st.session_state["driver_email"] | |
| # # }) | |
| # # st.success(f"Task at {value['address']} marked as completed!") | |
| # # import streamlit as st | |
| # # import firebase_admin | |
| # # from firebase_admin import credentials, db | |
| # # from datetime import datetime | |
| # # # Initialize Firebase | |
| # # if not firebase_admin._apps: | |
| # # cred = credentials.Certificate("firebase_credentials.json") # Path to your Firebase JSON | |
| # # firebase_admin.initialize_app(cred, { | |
| # # 'databaseURL': 'https://binsight-beda0-default-rtdb.asia-southeast1.firebasedatabase.app/' # Replace with your Firebase Realtime Database URL | |
| # # }) | |
| # # # Streamlit App | |
| # # st.title("Binsight Driver Dashboard") | |
| # # st.sidebar.header("Driver Login") | |
| # # # Driver Login | |
| # # driver_email = st.sidebar.text_input("Enter your email") | |
| # # login_button = st.sidebar.button("Login") | |
| # # if login_button: | |
| # # if driver_email: | |
| # # # Fetch allocated tasks from Firebase | |
| # # dustbins_ref = db.reference("dustbins") | |
| # # dustbins = dustbins_ref.get() | |
| # # # Filter tasks for the logged-in driver | |
| # # allocated_tasks = { | |
| # # key: value | |
| # # for key, value in dustbins.items() | |
| # # if value.get("allocated_truck") == driver_email and value.get("status") == "Allocated" | |
| # # } | |
| # # if allocated_tasks: | |
| # # st.subheader(f"Allocated Tasks ({len(allocated_tasks)})") | |
| # # for key, value in allocated_tasks.items(): | |
| # # with st.expander(f"π Task at {value['address']}"): | |
| # # st.write(f"**Latitude**: {value['latitude']}") | |
| # # st.write(f"**Longitude**: {value['longitude']}") | |
| # # st.write(f"**Classification**: {', '.join([f'{k} ({v:.2f})' for k, v in value['classification'].items()])}") | |
| # # st.write(f"**Allocated At**: {value.get('allocated_at', 'N/A')}") | |
| # # # Submit Work Done | |
| # # if st.button(f"Mark as Done", key=f"done_{key}"): | |
| # # dustbins_ref.child(key).update({ | |
| # # "status": "Completed", | |
| # # "completed_at": str(datetime.now()), | |
| # # "completed_by": driver_email | |
| # # }) | |
| # # st.success(f"Task at {value['address']} marked as completed!") | |
| # # else: | |
| # # st.info("No allocated tasks found.") | |
| # # else: | |
| # # st.error("Please enter your email to login.") | |
| # # import streamlit as st | |
| # # import firebase_admin | |
| # # from firebase_admin import credentials, db | |
| # # from datetime import datetime | |
| # # # Initialize Firebase | |
| # # if not firebase_admin._apps: | |
| # # cred = credentials.Certificate("firebase_credentials.json") # Path to your Firebase JSON | |
| # # firebase_admin.initialize_app(cred, { | |
| # # 'databaseURL': 'https://binsight-beda0-default-rtdb.asia-southeast1.firebasedatabase.app/' # Replace with your Firebase Realtime Database URL | |
| # # }) | |
| # # # Streamlit App | |
| # # st.title("Binsight Driver Dashboard") | |
| # # st.sidebar.header("Driver Login") | |
| # # # Driver Login | |
| # # driver_email = st.sidebar.text_input("Enter your email", help="Provide your registered email.") | |
| # # login_button = st.sidebar.button("Login") | |
| # # if login_button: | |
| # # if driver_email: | |
| # # # Fetch allocated tasks from Firebase | |
| # # dustbins_ref = db.reference("dustbins") | |
| # # dustbins = dustbins_ref.get() | |
| # # if not dustbins: | |
| # # st.info("No tasks available.") | |
| # # else: | |
| # # # Filter tasks for the logged-in driver | |
| # # allocated_tasks = { | |
| # # key: value | |
| # # for key, value in dustbins.items() | |
| # # if value.get("allocated_truck") == driver_email and value.get("status") == "Allocated" | |
| # # } | |
| # # if allocated_tasks: | |
| # # st.subheader(f"Allocated Tasks ({len(allocated_tasks)})") | |
| # # for key, value in allocated_tasks.items(): | |
| # # with st.expander(f"π Task at {value['address']}"): | |
| # # st.write(f"**Latitude**: {value['latitude']}") | |
| # # st.write(f"**Longitude**: {value['longitude']}") | |
| # # st.write(f"**Classification**: {', '.join([f'{k} ({v:.2f})' for k, v in value['classification'].items()])}") | |
| # # st.write(f"**Allocated At**: {value.get('allocated_at', 'N/A')}") | |
| # # # Submit Work Done | |
| # # if st.button(f"Mark as Done", key=f"done_{key}"): | |
| # # dustbins_ref.child(key).update({ | |
| # # "status": "Completed", | |
| # # "completed_at": str(datetime.now()), | |
| # # "completed_by": driver_email, | |
| # # }) | |
| # # st.success(f"Task at {value['address']} marked as completed!") | |
| # # else: | |
| # # st.info("No allocated tasks found for your account.") | |
| # # else: | |
| # # st.error("Please enter your email to login.") | |