VisitNairobiNow / app.py
Iammcqwory's picture
Update app.py
a1c8e5f verified
import streamlit as st
import pandas as pd
from PIL import Image
import io
# Sample data storage (replace with a database in production)
if 'recommendations' not in st.session_state:
st.session_state.recommendations = pd.DataFrame(columns=["User", "Category", "Place", "Description", "Upvotes", "Photo"])
if 'users' not in st.session_state:
st.session_state.users = {}
if 'rewards' not in st.session_state:
st.session_state.rewards = {}
# Page configuration
st.set_page_config(page_title="VisitNairobiNow", page_icon="🌍", layout="wide")
# Sidebar for About Us and Help
st.sidebar.title("About VisitNairobiNow")
st.sidebar.write("""
**VisitNairobiNow** is a community-driven platform where users can share their favorite travel experiences in Nairobi and earn rewards for their contributions.
Explore hidden gems, connect with fellow travelers, and make the most of your Nairobi adventure!
""")
st.sidebar.write("---")
st.sidebar.header("Help")
st.sidebar.write("""
- **How to Submit a Recommendation**: Log in, fill out the form, and upload a photo.
- **Earn Rewards**: Get upvotes on your recommendations to earn points.
- **Redeem Rewards**: Use your points for discounts and exclusive deals.
""")
# User authentication in the sidebar
st.sidebar.header("Login / Sign Up")
if 'current_user' not in st.session_state:
# Login
username = st.sidebar.text_input("Username")
password = st.sidebar.text_input("Password", type="password")
if st.sidebar.button("Login"):
if username in st.session_state.users and st.session_state.users[username] == password:
st.session_state.current_user = username
st.sidebar.success(f"Logged in as {username}")
else:
st.sidebar.error("Invalid username or password")
# Sign Up
st.sidebar.write("Don't have an account? Sign up below!")
new_username = st.sidebar.text_input("Create Username", key="new_username")
new_password = st.sidebar.text_input("Create Password", type="password", key="new_password")
if st.sidebar.button("Sign Up"):
if new_username in st.session_state.users:
st.sidebar.error("Username already exists")
else:
# Add new user to the users dictionary
st.session_state.users[new_username] = new_password
st.session_state.rewards[new_username] = 0
# Automatically log in the new user
st.session_state.current_user = new_username
st.sidebar.success("Account created! You are now logged in.")
else:
st.sidebar.write(f"Logged in as: **{st.session_state.current_user}**")
if st.sidebar.button("Logout"):
del st.session_state.current_user
st.experimental_rerun()
# Main app functionality
if 'current_user' in st.session_state:
st.title("🌍 VisitNairobiNow")
st.write("Share your favorite places in Nairobi and earn rewards!")
# Tabs for navigation
tab1, tab2, tab3 = st.tabs(["Home", "Submit Recommendation", "Your Rewards"])
# Home Tab (Instagram-like UI)
with tab1:
st.header("Explore Recommendations")
if not st.session_state.recommendations.empty:
for index, row in st.session_state.recommendations.iterrows():
col1, col2 = st.columns([1, 3])
with col1:
if row["Photo"] is not None:
st.image(row["Photo"], caption=row["Place"], use_container_width=True) # Updated parameter
with col2:
st.subheader(row["Place"])
st.write(f"**Category:** {row['Category']}")
st.write(f"**Description:** {row['Description']}")
st.write(f"**Recommended by:** {row['User']}")
st.write(f"**Upvotes:** {row['Upvotes']}")
if st.button(f"Upvote πŸ‘ ({row['Upvotes']})", key=f"upvote_{index}"):
st.session_state.recommendations.at[index, "Upvotes"] += 1
st.session_state.rewards[row["User"]] += 1 # Reward the user
st.experimental_rerun()
st.write("---")
else:
st.write("No recommendations yet. Be the first to share!")
# Submit Recommendation Tab
with tab2:
st.header("Submit a Recommendation")
category = st.selectbox("Category", ["Destination", "Restaurant", "Activity", "Accommodation"])
place = st.text_input("Place Name")
description = st.text_area("Description")
photo = st.file_uploader("Upload a Photo", type=["jpg", "jpeg", "png"])
if st.button("Submit Recommendation"):
if place and description:
# Save the photo as bytes
photo_bytes = None
if photo is not None:
photo_bytes = io.BytesIO(photo.read())
new_recommendation = {
"User": st.session_state.current_user,
"Category": category,
"Place": place,
"Description": description,
"Upvotes": 0,
"Photo": photo_bytes
}
# Use pd.concat instead of append
st.session_state.recommendations = pd.concat(
[st.session_state.recommendations, pd.DataFrame([new_recommendation])],
ignore_index=True
)
st.success("Recommendation submitted!")
else:
st.error("Please fill in all fields.")
# Your Rewards Tab
with tab3:
st.header("Your Rewards")
st.write(f"**Total Rewards:** {st.session_state.rewards.get(st.session_state.current_user, 0)} points")
st.write("Earn more by submitting recommendations and getting upvotes!")
else:
st.warning("Please log in or sign up to access the platform.")