Spaces:
Sleeping
Sleeping
new file
Browse files
App.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from streamlit_option_menu import option_menu
|
3 |
+
from home import dashboard
|
4 |
+
import json
|
5 |
+
import uuid
|
6 |
+
|
7 |
+
def load_json():
|
8 |
+
with open("database/data.json") as file:
|
9 |
+
data = json.load(file)
|
10 |
+
return data
|
11 |
+
|
12 |
+
def login():
|
13 |
+
st.title("Login")
|
14 |
+
data = json.load(open("database/data.json"))
|
15 |
+
usrname = st.text_input("Username")
|
16 |
+
password = st.text_input("Password", type="password")
|
17 |
+
if st.button("Login", key="loginkey"):
|
18 |
+
for user in data["users"]:
|
19 |
+
if usrname == user["username"] and password == user["password"]:
|
20 |
+
st.success("Logged in as {}".format(usrname))
|
21 |
+
st.session_state["user"] = "logged"
|
22 |
+
flag = True
|
23 |
+
st.experimental_rerun()
|
24 |
+
else:
|
25 |
+
flag = False
|
26 |
+
if flag == False:
|
27 |
+
st.error("Invalid username or password")
|
28 |
+
st.stop()
|
29 |
+
|
30 |
+
|
31 |
+
def signup():
|
32 |
+
st.title("Signup")
|
33 |
+
username = st.text_input("Username")
|
34 |
+
password = st.text_input("Password", type="password")
|
35 |
+
confirm_password = st.text_input("Confirm Password", type="password")
|
36 |
+
if st.button("Signup", key="signupkey"):
|
37 |
+
if password == confirm_password:
|
38 |
+
data = json.load(open("database/data.json"))
|
39 |
+
newuser = {
|
40 |
+
"username": username,
|
41 |
+
"password": password,
|
42 |
+
"id": str(uuid.uuid4())
|
43 |
+
}
|
44 |
+
data["users"].append(newuser)
|
45 |
+
with open("database/data.json", "w") as file:
|
46 |
+
json.dump(data, file)
|
47 |
+
st.success("Account created")
|
48 |
+
else:
|
49 |
+
st.error("Passwords do not match")
|
50 |
+
|
51 |
+
|
52 |
+
def main():
|
53 |
+
if "user" not in st.session_state:
|
54 |
+
st.session_state["user"] = "visitor"
|
55 |
+
if st.session_state["user"] == "logged":
|
56 |
+
dashboard()
|
57 |
+
elif st.session_state["user"] == "visitor":
|
58 |
+
option = option_menu(
|
59 |
+
menu_title="Authentication",
|
60 |
+
options=["Login", "Signup"],
|
61 |
+
)
|
62 |
+
|
63 |
+
if option == "Login":
|
64 |
+
login()
|
65 |
+
elif option == "Signup":
|
66 |
+
signup()
|
67 |
+
|
68 |
+
|
69 |
+
main()
|