Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from home import dashboard
|
| 3 |
+
from streamlit_option_menu import option_menu
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
import uuid
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# st.set_page_config(page_title="Authentication", page_icon=":guardsman:", layout="wide")
|
| 10 |
+
|
| 11 |
+
# st.title("Authentication")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def load_json():
|
| 15 |
+
with open("database/data.json") as file:
|
| 16 |
+
data = json.load(file)
|
| 17 |
+
return data
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def save_json():
|
| 24 |
+
with open("database/data.json", "w") as file:
|
| 25 |
+
json.dump(data, file, indent=4)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def login():
|
| 30 |
+
st.title("Login")
|
| 31 |
+
data = json.load(open("database/data.json"))
|
| 32 |
+
usrname = st.text_input("Username")
|
| 33 |
+
password = st.text_input("Password", type="password")
|
| 34 |
+
if st.button("Login", key="loginkey"):
|
| 35 |
+
for user in data["users"]:
|
| 36 |
+
if usrname == user["username"] and password == user["password"]:
|
| 37 |
+
st.success("Logged in as {}".format(usrname))
|
| 38 |
+
st.session_state["user"] = "logged"
|
| 39 |
+
flag = True
|
| 40 |
+
st.experimental_rerun()
|
| 41 |
+
else:
|
| 42 |
+
flag = False
|
| 43 |
+
if flag == False:
|
| 44 |
+
st.error("Invalid username or password")
|
| 45 |
+
st.stop()
|
| 46 |
+
st.balloons()
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def signup():
|
| 50 |
+
|
| 51 |
+
st.title("Signup")
|
| 52 |
+
username = st.text_input("Username")
|
| 53 |
+
password = st.text_input("Password", type="password")
|
| 54 |
+
confirm_password = st.text_input("Confirm Password", type="password")
|
| 55 |
+
if st.button("Signup", key="signupkey"):
|
| 56 |
+
if password == confirm_password:
|
| 57 |
+
data = json.load(open("database/data.json"))
|
| 58 |
+
newuser = {
|
| 59 |
+
"username": username,
|
| 60 |
+
"password": password,
|
| 61 |
+
"id": str(uuid.uuid4())
|
| 62 |
+
}
|
| 63 |
+
data["users"].append(newuser)
|
| 64 |
+
json.dump(data, open("database/data.json", "w"), indent=4)
|
| 65 |
+
st.success("Account created! You can now login.")
|
| 66 |
+
st.snow()
|
| 67 |
+
st.cache_data.clear()
|
| 68 |
+
else:
|
| 69 |
+
st.error("Passwords do not match")
|
| 70 |
+
|
| 71 |
+
def main():
|
| 72 |
+
# st.title("Authentication")
|
| 73 |
+
if "user" not in st.session_state:
|
| 74 |
+
st.session_state["user"] = "visitor"
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
if st.session_state["user"] == "logged":
|
| 79 |
+
dashboard()
|
| 80 |
+
|
| 81 |
+
elif st.session_state["user"] == "visitor":
|
| 82 |
+
|
| 83 |
+
option = option_menu(
|
| 84 |
+
menu_title="Authentication",
|
| 85 |
+
options=["Login", "Signup"],
|
| 86 |
+
icons=["house", "activity"],
|
| 87 |
+
menu_icon="cast",
|
| 88 |
+
default_index=0,
|
| 89 |
+
orientation="horizontal",
|
| 90 |
+
|
| 91 |
+
)
|
| 92 |
+
if option == "Login":
|
| 93 |
+
login()
|
| 94 |
+
elif option == "Signup":
|
| 95 |
+
signup()
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
main()
|