Spaces:
Runtime error
Runtime error
import streamlit as st | |
from streamlit_option_menu import option_menu | |
from pymongo import MongoClient | |
import os | |
from home import dashboard | |
if 'user' not in st.session_state: | |
st.session_state['user'] = "Guest" | |
# Connecting to MongoD | |
uri = os.environ["MONGO_CONNECTION_STRING"] | |
client = MongoClient(uri, tlsCertificateKeyFile="database/cert.pem") | |
db = client["myapp"] | |
col = db["users"] | |
# Checking connectiong to database | |
try: | |
client.admin.command('ping') | |
print("Connection Established Successfully!") | |
except Exception as e: | |
print(f"Not Connected: {e}") | |
def Signup(): | |
username = st.text_input("Username") | |
password = st.text_input("Password", type="password") | |
confpass = st.text_input("Confirm Password", type="password") | |
newuser = { | |
"username": username,"password": password | |
} | |
if st.button("Signup"): | |
if password == confpass: | |
col.insert_one({"username": username, "password": password}) | |
st.write("You are Registered Sucessfully") | |
else: | |
"Password do not match" | |
def Login(): | |
username = st.text_input("username") | |
password = st.text_input("Password") | |
if st.button("Login"): | |
allusers = list(col.find()) | |
for anyuser in allusers: | |
if username == anyuser["username"] and password == anyuser["password"]: | |
st.success("You are logged in") | |
st.session_state["user"] = "isuser" | |
st.experimental_rerun() | |
else: | |
st.error("Your username or password do not match") | |
def main(): | |
if st.session_state["user"] == "Guest": | |
with st.sidebar: | |
selected = option_menu("Menu", ["Login", "Signup"], icons = ["house", "person"]) | |
if selected == "Login": | |
Login() | |
elif selected == "Signup": | |
Signup() | |
elif st.session_state["user"] == "isuser": | |
dashboard() | |
main() | |