saas-template / app.py
eagle0504's picture
Update app.py
088c41c verified
from datetime import datetime
import streamlit as st
import streamlit_authenticator as stauth
import yaml
from yaml.loader import SafeLoader
from sections.software_engineer_basics import *
st.set_page_config(page_title="SAAS Template App", page_icon="🧊", layout="wide")
with open("config.yaml") as file:
config = yaml.load(file, Loader=SafeLoader)
authenticator = stauth.Authenticate(
config["credentials"],
config["cookie"]["name"],
config["cookie"]["key"],
config["cookie"]["expiry_days"],
config["preauthorized"],
)
# Credit: Time
def current_year():
now = datetime.now()
return now.year
with st.sidebar:
if not st.session_state["authentication_status"]:
st.markdown(
"""
# Welcome to **Name of App** 🎶🤖
Introduction / Instruction Manual ...🚀
### 🖱️Press "R" on your keyboard⌨️ to rerun the app for faster loading 🔄.
### 🔒Sample login info - User: "jsmith" | Password: "abc"
"""
)
# Default video
st.write(
"👋 Guide to Data Science, Machine Learning, and Artificial Intelligence!"
)
with st.expander("Login 🔒", expanded=True):
# authenticator.login("Login", "main")
authenticator.login()
if st.session_state["authentication_status"]:
# authenticator.logout("Logout", "main", key="unique_key_login")
authenticator.logout()
st.write(f'Welcome *{st.session_state["name"]}*')
st.title(
"You logged in! You can choose a session in the bottom of the sidebar and view content."
)
elif st.session_state["authentication_status"] is False:
st.error("Username/password is incorrect")
elif st.session_state["authentication_status"] is None:
st.warning("Please enter your username and password")
with st.expander("Reset password 😕"):
if st.session_state["authentication_status"]:
try:
if authenticator.reset_password(
st.session_state["username"], "Reset password"
):
st.success("Password modified successfully")
except Exception as e:
st.error(e)
with st.expander("Forgot password 😕"):
try:
(
username_of_forgotten_password,
email_of_forgotten_password,
new_random_password,
) = authenticator.forgot_password("Forgot password")
if username_of_forgotten_password:
st.success("New password to be sent securely")
# Random password should be transferred to user securely
else:
st.error("Username not found")
except Exception as e:
st.error(e)
with st.expander("Update user 😋"):
if st.session_state["authentication_status"]:
try:
if authenticator.update_user_details(
st.session_state["username"], "Update user details"
):
st.success("Entries updated successfully")
except Exception as e:
st.error(e)
with st.expander("Register user 😋"):
try:
# payment_key = st.text_input("Enter Payment Key:")
if authenticator.register_user("Register user", preauthorization=False):
# if register_user(payment_key):
st.success("User registered successfully")
except Exception as e:
st.error(e)
with open("config.yaml", "w") as file:
yaml.dump(config, file, default_flow_style=False)
with st.expander("Donation💲"):
stripe_payment_link = "https://buy.stripe.com/cN2bLGa0faMp5u8fYZ"
st.markdown(
f"""
Want to support me? 😄 Done here using this [link]({stripe_payment_link}).
"""
)
# Credit:
current_year = current_year() # This will print the current year
st.markdown(
f"""
<h6 style='text-align: left;'>Copyright © 2010-{current_year} Present Yiqiao Yin</h6>
""",
unsafe_allow_html=True,
)
# Main
if st.session_state["authentication_status"]:
st.markdown(
"""
# Homepage 🎶🤖
Content goes here.
"""
)
page_names_to_funcs = {
"Software Engineer Basics": software_engineer_basics
}
demo_name = st.sidebar.radio(
"Choose a topic below:", key="visibility", options=page_names_to_funcs.keys()
)
page_names_to_funcs[demo_name]()
with st.sidebar:
with st.expander("Login 🔒", expanded=True):
# authenticator.login("Login", "main")
authenticator.login()
st.warning(
"If you are running this app on HuggingFace, you'll need to just refresh your screen to log out."
)
if st.session_state["authentication_status"]:
try:
# authenticator.logout("Logout", "main", key="unique_key")
authenticator.logout()
except KeyError:
pass # ignore it
except Exception as err:
st.error(f"Unexpected exception {err}")
raise Exception(err) # but not this, let's crash the app
st.write(f'Welcome *{st.session_state["name"]}*')
st.title(
"You logged in! You can choose a session in the bottom of the sidebar and view content."
)
st.markdown(
"### 🖱️Press "
R" on your keyboard⌨️ to rerun the app for faster loading 🔄."
)
elif st.session_state["authentication_status"] is False:
st.error("Username/password is incorrect")
elif st.session_state["authentication_status"] is None:
st.warning("Please enter your username and password")