File size: 3,677 Bytes
f51b905
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import json
import os

if 'username' not in st.session_state:
    st.session_state.username = ''
if 'scratch_pad' not in st.session_state:
    st.session_state.scratch_pad = ''

history_file = 'user_history.json'
if not os.path.exists(history_file):
    with open(history_file, 'w') as file:
        json.dump({}, file)

with open(history_file, 'r') as file:
    user_history = json.load(file)

def save_session(username, password):
    st.session_state.username = username
    st.session_state.scratch_pad = st.session_state.scratch_pad
    user_history[username] = {
        'password': password,
        'scratch_pad': st.session_state.scratch_pad
    }
    with open(history_file, 'w') as file:
        json.dump(user_history, file)

def register_user(username, password):
    if username not in user_history:
        save_session(username, password)
        st.success("User registered successfully!")
    else:
        st.warning("Username already exists. Please choose a different username.")

def login_user(username, password):
    if username in user_history:
        stored_password = user_history[username]['password']
        if password == stored_password:
            st.session_state.username = username
            st.session_state.scratch_pad = user_history[username]['scratch_pad']
            st.success("Logged in successfully!")
            return True
        else:
            st.error("Incorrect password. Please try again.")
    else:
        st.warning("Username not found. Please register first.")
    return False

def reset_password(username):
    if username in user_history:
        new_password = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
        user_history[username]['password'] = new_password
        with open(history_file, 'w') as file:
            json.dump(user_history, file)
        st.success(f"Password reset successful. Your new password is: {new_password}")
    else:
        st.warning("Username not found.")

def save_scratch_pad(username, content):
    filename = f"{username}_scratch_pad.txt"
    with open(filename, 'w') as file:
        file.write(content)
    st.success(f"Scratch pad saved successfully. Filename: {filename}")

def load_scratch_pad(username):
    filename = f"{username}_scratch_pad.txt"
    if os.path.exists(filename):
        with open(filename, 'r') as file:
            content = file.read()
        return content
    return ''

def get_user_files(username):
    files = [f for f in os.listdir('.') if f.startswith(f"{username}_")]
    return files

st.title("Welcome to My App")

if st.session_state.username:
    st.subheader(f"Welcome back, {st.session_state.username}!")
    scratch_pad_content = load_scratch_pad(st.session_state.username)
    scratch_pad = st.text_area("Scratch Pad", value=scratch_pad_content, height=200)
    if st.button("πŸ’Ύ Save"):
        save_scratch_pad(st.session_state.username, scratch_pad)
    if st.button("πŸ”‘ Reset Password"):
        reset_password(st.session_state.username)

    st.subheader("Your Files")
    user_files = get_user_files(st.session_state.username)
    for file in user_files:
        st.write(file)
        with open(file, 'rb') as f:
            st.download_button(label=f"Download {file}", data=f, file_name=file)
else:
    username = st.text_input("Username:")
    password = st.text_input("Password:", type="password")
    col1, col2 = st.columns(2)
    with col1:
        if st.button("πŸ”‘ Login"):
            if login_user(username, password):
                st.experimental_rerun()
    with col2:
        if st.button("πŸ“ Register"):
            register_user(username, password)