Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
|
5 |
+
if 'username' not in st.session_state:
|
6 |
+
st.session_state.username = ''
|
7 |
+
if 'scratch_pad' not in st.session_state:
|
8 |
+
st.session_state.scratch_pad = ''
|
9 |
+
|
10 |
+
history_file = 'user_history.json'
|
11 |
+
if not os.path.exists(history_file):
|
12 |
+
with open(history_file, 'w') as file:
|
13 |
+
json.dump({}, file)
|
14 |
+
|
15 |
+
with open(history_file, 'r') as file:
|
16 |
+
user_history = json.load(file)
|
17 |
+
|
18 |
+
def save_session(username, password):
|
19 |
+
st.session_state.username = username
|
20 |
+
st.session_state.scratch_pad = st.session_state.scratch_pad
|
21 |
+
user_history[username] = {
|
22 |
+
'password': password,
|
23 |
+
'scratch_pad': st.session_state.scratch_pad
|
24 |
+
}
|
25 |
+
with open(history_file, 'w') as file:
|
26 |
+
json.dump(user_history, file)
|
27 |
+
|
28 |
+
def register_user(username, password):
|
29 |
+
if username not in user_history:
|
30 |
+
save_session(username, password)
|
31 |
+
st.success("User registered successfully!")
|
32 |
+
else:
|
33 |
+
st.warning("Username already exists. Please choose a different username.")
|
34 |
+
|
35 |
+
def login_user(username, password):
|
36 |
+
if username in user_history:
|
37 |
+
stored_password = user_history[username]['password']
|
38 |
+
if password == stored_password:
|
39 |
+
st.session_state.username = username
|
40 |
+
st.session_state.scratch_pad = user_history[username]['scratch_pad']
|
41 |
+
st.success("Logged in successfully!")
|
42 |
+
return True
|
43 |
+
else:
|
44 |
+
st.error("Incorrect password. Please try again.")
|
45 |
+
else:
|
46 |
+
st.warning("Username not found. Please register first.")
|
47 |
+
return False
|
48 |
+
|
49 |
+
def reset_password(username):
|
50 |
+
if username in user_history:
|
51 |
+
new_password = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
|
52 |
+
user_history[username]['password'] = new_password
|
53 |
+
with open(history_file, 'w') as file:
|
54 |
+
json.dump(user_history, file)
|
55 |
+
st.success(f"Password reset successful. Your new password is: {new_password}")
|
56 |
+
else:
|
57 |
+
st.warning("Username not found.")
|
58 |
+
|
59 |
+
def save_scratch_pad(username, content):
|
60 |
+
filename = f"{username}_scratch_pad.txt"
|
61 |
+
with open(filename, 'w') as file:
|
62 |
+
file.write(content)
|
63 |
+
st.success(f"Scratch pad saved successfully. Filename: {filename}")
|
64 |
+
|
65 |
+
def load_scratch_pad(username):
|
66 |
+
filename = f"{username}_scratch_pad.txt"
|
67 |
+
if os.path.exists(filename):
|
68 |
+
with open(filename, 'r') as file:
|
69 |
+
content = file.read()
|
70 |
+
return content
|
71 |
+
return ''
|
72 |
+
|
73 |
+
def get_user_files(username):
|
74 |
+
files = [f for f in os.listdir('.') if f.startswith(f"{username}_")]
|
75 |
+
return files
|
76 |
+
|
77 |
+
st.title("Welcome to My App")
|
78 |
+
|
79 |
+
if st.session_state.username:
|
80 |
+
st.subheader(f"Welcome back, {st.session_state.username}!")
|
81 |
+
scratch_pad_content = load_scratch_pad(st.session_state.username)
|
82 |
+
scratch_pad = st.text_area("Scratch Pad", value=scratch_pad_content, height=200)
|
83 |
+
if st.button("πΎ Save"):
|
84 |
+
save_scratch_pad(st.session_state.username, scratch_pad)
|
85 |
+
if st.button("π Reset Password"):
|
86 |
+
reset_password(st.session_state.username)
|
87 |
+
|
88 |
+
st.subheader("Your Files")
|
89 |
+
user_files = get_user_files(st.session_state.username)
|
90 |
+
for file in user_files:
|
91 |
+
st.write(file)
|
92 |
+
with open(file, 'rb') as f:
|
93 |
+
st.download_button(label=f"Download {file}", data=f, file_name=file)
|
94 |
+
else:
|
95 |
+
username = st.text_input("Username:")
|
96 |
+
password = st.text_input("Password:", type="password")
|
97 |
+
col1, col2 = st.columns(2)
|
98 |
+
with col1:
|
99 |
+
if st.button("π Login"):
|
100 |
+
if login_user(username, password):
|
101 |
+
st.experimental_rerun()
|
102 |
+
with col2:
|
103 |
+
if st.button("π Register"):
|
104 |
+
register_user(username, password)
|