Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,90 +1,80 @@
|
|
1 |
import streamlit as st
|
2 |
import json
|
3 |
import os
|
4 |
-
|
|
|
5 |
|
6 |
-
# Initialize session state
|
7 |
if 'username' not in st.session_state:
|
8 |
-
|
9 |
if 'scratch_pad' not in st.session_state:
|
10 |
-
|
11 |
|
12 |
-
# Load or create user history file
|
13 |
history_file = 'user_history.json'
|
14 |
if not os.path.exists(history_file):
|
15 |
-
|
16 |
-
|
17 |
|
18 |
-
# Load user history
|
19 |
with open(history_file, 'r') as file:
|
20 |
-
|
21 |
-
|
22 |
-
# Generate encryption key
|
23 |
-
key = Fernet.generate_key()
|
24 |
-
fernet = Fernet(key)
|
25 |
|
26 |
def save_session(username, password):
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
json.dump(user_history, file)
|
36 |
|
37 |
def register_user(username, password):
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
|
45 |
def login_user(username, password):
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
else:
|
60 |
-
st.warning("Username not found. Please register first.")
|
61 |
-
return False
|
62 |
-
|
63 |
def reset_password(username):
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
70 |
|
71 |
-
st.title("
|
72 |
|
73 |
if st.session_state.username:
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
else:
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
1 |
import streamlit as st
|
2 |
import json
|
3 |
import os
|
4 |
+
import random
|
5 |
+
import string
|
6 |
|
|
|
7 |
if 'username' not in st.session_state:
|
8 |
+
st.session_state.username = ''
|
9 |
if 'scratch_pad' not in st.session_state:
|
10 |
+
st.session_state.scratch_pad = ''
|
11 |
|
|
|
12 |
history_file = 'user_history.json'
|
13 |
if not os.path.exists(history_file):
|
14 |
+
with open(history_file, 'w') as file:
|
15 |
+
json.dump({}, file)
|
16 |
|
|
|
17 |
with open(history_file, 'r') as file:
|
18 |
+
user_history = json.load(file)
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def save_session(username, password):
|
21 |
+
st.session_state.username = username
|
22 |
+
st.session_state.scratch_pad = st.session_state.scratch_pad
|
23 |
+
user_history[username] = {
|
24 |
+
'password': password,
|
25 |
+
'scratch_pad': st.session_state.scratch_pad
|
26 |
+
}
|
27 |
+
with open(history_file, 'w') as file:
|
28 |
+
json.dump(user_history, file)
|
|
|
29 |
|
30 |
def register_user(username, password):
|
31 |
+
if username not in user_history:
|
32 |
+
save_session(username, password)
|
33 |
+
st.success("User registered successfully!")
|
34 |
+
else:
|
35 |
+
st.warning("Username already exists. Please choose a different username.")
|
|
|
36 |
|
37 |
def login_user(username, password):
|
38 |
+
if username in user_history:
|
39 |
+
stored_password = user_history[username]['password']
|
40 |
+
if password == stored_password:
|
41 |
+
st.session_state.username = username
|
42 |
+
st.session_state.scratch_pad = user_history[username]['scratch_pad']
|
43 |
+
st.success("Logged in successfully!")
|
44 |
+
return True
|
45 |
+
else:
|
46 |
+
st.error("Incorrect password. Please try again.")
|
47 |
+
else:
|
48 |
+
st.warning("Username not found. Please register first.")
|
49 |
+
return False
|
50 |
+
|
|
|
|
|
|
|
|
|
51 |
def reset_password(username):
|
52 |
+
if username in user_history:
|
53 |
+
new_password = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
|
54 |
+
user_history[username]['password'] = new_password
|
55 |
+
with open(history_file, 'w') as file:
|
56 |
+
json.dump(user_history, file)
|
57 |
+
st.success(f"Password reset successful. Your new password is: {new_password}")
|
58 |
+
else:
|
59 |
+
st.warning("Username not found.")
|
60 |
|
61 |
+
st.title("Welcome to My App")
|
62 |
|
63 |
if st.session_state.username:
|
64 |
+
st.subheader(f"Welcome back, {st.session_state.username}!")
|
65 |
+
scratch_pad = st.text_area("Scratch Pad", value=st.session_state.scratch_pad, height=200)
|
66 |
+
if st.button("πΎ Save"):
|
67 |
+
save_session(st.session_state.username, user_history[st.session_state.username]['password'])
|
68 |
+
if st.button("π Reset Password"):
|
69 |
+
reset_password(st.session_state.username)
|
70 |
else:
|
71 |
+
username = st.text_input("Username:")
|
72 |
+
password = st.text_input("Password:", type="password")
|
73 |
+
col1, col2 = st.columns(2)
|
74 |
+
with col1:
|
75 |
+
if st.button("π Login"):
|
76 |
+
if login_user(username, password):
|
77 |
+
st.experimental_rerun()
|
78 |
+
with col2:
|
79 |
+
if st.button("π Register"):
|
80 |
+
register_user(username, password)
|