-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHIVE.py
144 lines (110 loc) · 3.86 KB
/
HIVE.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import streamlit as st
import bcrypt
from pathlib import Path
import json
from pymongo import MongoClient
from streamlit_extras.switch_page_button import switch_page
from streamlit.source_util import _on_pages_changed, get_pages
from dotenv import load_dotenv
import os
load_dotenv()
DEFAULT_PAGE = "HIVE.py"
SECOND_PAGE_NAME = "Dashboard"
# all pages request
def get_all_pages():
default_pages = get_pages(DEFAULT_PAGE)
pages_path = Path("pages.json")
if pages_path.exists():
saved_default_pages = json.loads(pages_path.read_text())
else:
saved_default_pages = default_pages.copy()
pages_path.write_text(json.dumps(default_pages, indent=4))
return saved_default_pages
# clear all page but not login page
def clear_all_but_first_page():
current_pages = get_pages(DEFAULT_PAGE)
if len(current_pages.keys()) == 1:
return
get_all_pages()
# Remove all but the first page
key, val = list(current_pages.items())[0]
current_pages.clear()
current_pages[key] = val
_on_pages_changed.send()
# show all pages
def show_all_pages():
current_pages = get_pages(DEFAULT_PAGE)
saved_pages = get_all_pages()
# Replace all the missing pages
for key in saved_pages:
if key not in current_pages:
current_pages[key] = saved_pages[key]
_on_pages_changed.send()
# Hide default page
def hide_page(name: str):
current_pages = get_pages(DEFAULT_PAGE)
for key, val in current_pages.items():
if val["page_name"] == name:
del current_pages[key]
_on_pages_changed.send()
break
# calling only default(login) page
clear_all_but_first_page()
st.image('HIVE.png', use_column_width=True)
st.title("Welcome to Hive")
# st.sidebar.success("Select a page")
uri = os.getenv("API_KEY")
# Connect to MongoDB
client = MongoClient(uri)
db = client["Donate_app"]
users_collection = db["users"]
# Login form
def login():
st.header("Login")
username = st.text_input("Username")
password = st.text_input("Password", type="password")
if st.button("Login"):
# Retrieve user details from the database
user = users_collection.find_one({"username": username})
if user:
# Verify the password
if bcrypt.checkpw(password.encode('utf-8'), user['password']):
st.success("Logged In Sucessfully {}".format(username))
# Redirect to the desired page
show_all_pages() # call all page
hide_page(DEFAULT_PAGE.replace(".py", "")) # hide first page
switch_page(SECOND_PAGE_NAME) # switch to second page
# switch_page("Dashboard")
else:
st.error("Invalid username or password")
clear_all_but_first_page() # clear all page but show first page
else:
st.error("Invalid username or password")
clear_all_but_first_page() # clear all page but show first page
# Sign-up form
def signup():
st.header("Sign Up")
username = st.text_input("Username")
email = st.text_input("Email")
password = st.text_input("Password", type="password")
if st.button("Sign Up"):
# Hash the password
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
# Insert user details into the database
user_data = {
"username": username,
"email": email,
"password": hashed_password
}
users_collection.insert_one(user_data)
st.success("Successfully registered!")
# Run the Streamlit app
def main():
# Display the login or sign-up form based on user selection
form_choice = st.selectbox("Select an option:", ("Login", "Sign Up"))
if form_choice == "Login":
login()
elif form_choice == "Sign Up":
signup()
if __name__ == '__main__':
main()