User-Profile-Management / backup.app4.py
awacke1's picture
Create backup.app4.py
152d24f verified
import streamlit as st
import os
import json
def save_user_data(email, data):
"""Save user data to a file named after the user's email."""
with open(f"{email}.txt", "w") as file:
file.write(json.dumps(data))
def load_user_data(email):
"""Load user data from a file named after the user's email, or create a new file if it doesn't exist."""
if not os.path.isfile(f"{email}.txt"):
return {'email': '', 'phone': '', 'password': '', 'social': {}}
with open(f"{email}.txt", "r") as file:
return json.loads(file.read())
def list_saved_users():
"""List all files (users) with saved states containing '@' in filename."""
return [f[:-4] for f in os.listdir() if f.endswith('.txt') and '@' in f]
def display_social_links(social_data):
"""Display social media links as Markdown."""
st.markdown("## Social Media Links")
for platform, url in social_data.items():
if url:
emoji = {"instagram": "πŸ“·", "twitter": "🐦", "facebook": "πŸ“˜", "huggingface": "πŸ€—", "github": "πŸ’»", "linkedin": "πŸ”—"}.get(platform, "")
st.markdown(f"{emoji} [{platform.capitalize()}]({url})")
def main():
st.title('User Data Management')
# Sidebar for selecting a user file
st.sidebar.title("Saved Users")
saved_users = list_saved_users()
selected_user = st.sidebar.selectbox("Select a user", options=['New User'] + saved_users)
# Load or initialize data
if selected_user and selected_user != 'New User':
cached_data = load_user_data(selected_user)
else:
cached_data = {'email': '', 'phone': '', 'password': '', 'social': {}}
# Input fields with emojis
new_email = st.text_input("πŸ“§ Email Address", value=cached_data['email'])
new_phone = st.text_input("πŸ“± Mobile Phone", value=cached_data['phone'])
new_password = st.text_input("πŸ”‘ Password", value=cached_data['password'], type='password')
# Social media fields
st.markdown("### Social Media Profiles")
social_fields = ["instagram", "twitter", "facebook", "huggingface", "github", "linkedin"]
for field in social_fields:
emoji = {"instagram": "πŸ“·", "twitter": "🐦", "facebook": "πŸ“˜", "huggingface": "πŸ€—", "github": "πŸ’»", "linkedin": "πŸ”—"}.get(field, "")
cached_data['social'][field] = st.text_input(f"{emoji} {field.capitalize()}", value=cached_data['social'].get(field, ''))
# Save data when changes are made
if st.button("Save Data"):
save_user_data(new_email, cached_data)
st.sidebar.success("Data updated and saved!")
# Show email link with GET parameter
if new_email:
st.write(f"Link for this user: `https://huggingface.co./spaces/awacke1/Streamlit-Memory-Cache-Resource-and-Data/?email={new_email}`")
st.session_state['last_email'] = new_email
# Display current data without password
display_data = cached_data.copy()
display_data['password'] = '*****' # Hide the password
st.write("Current Data:")
st.json(display_data)
# Display social media links
display_social_links(cached_data['social'])
# Password Reset Simulation
if st.sidebar.button("Reset Password"):
reset_email = st.sidebar.text_input("Enter email for password reset")
if reset_email and reset_email in saved_users:
reset_data = load_user_data(reset_email)
reset_data['password'] = 'new_password' # Reset password
save_user_data(reset_email, reset_data)
st.sidebar.success(f"Password reset for {reset_email}")
# Run the app
if __name__ == "__main__":
main()