You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

130 lines
4.0 KiB

import streamlit as st
import streamlit_authenticator as stauth
import yaml
from yaml.loader import SafeLoader
from streamlit_authenticator import LoginError
from time import sleep
from colorprinter.print_color import *
from _arango import ArangoDB
def get_settings():
"""
Function to get the settings from the ArangoDB.
"""
arango = ArangoDB(db_name=st.session_state["username"])
settings = arango.db.collection("settings").get("settings")
if settings:
st.session_state["settings"] = settings
else:
st.session_state["settings"] = {'current_collection': None, 'current_page': None}
return st.session_state["settings"]
st.set_page_config(page_title="Science Assistant Fish", page_icon="🐟")
with open("streamlit_users.yaml") as file:
config = yaml.load(file, Loader=SafeLoader)
authenticator = stauth.Authenticate(
config["credentials"],
config["cookie"]["name"],
config["cookie"]["key"],
config["cookie"]["expiry_days"],
)
try:
authenticator.login()
except LoginError as e:
st.error(e)
if st.session_state["authentication_status"]:
sleep(0.1)
# Retry mechanism for importing get_settings
for _ in range(3):
try:
get_settings()
except ImportError as e:
sleep(0.3)
print_red(e)
print("Retrying to import get_settings...")
# Retry mechanism for importing pages
for _ in range(3):
try:
from streamlit_pages import (
Article_Collections,
Bot_Chat,
Projects,
Settings,
RSS_Feeds
)
break
except ImportError as e:
# Write the full error traceback
sleep(0.3)
print_red(e)
print("Retrying to import pages...")
st.session_state["settings"] = get_settings()
if isinstance(st.session_state["settings"], dict) and "current_page" in st.session_state["settings"]:
st.session_state["current_page"] = st.session_state["settings"]["current_page"]
else:
if "current_page" not in st.session_state:
st.session_state["current_page"] = None
if "not_downloaded" not in st.session_state:
st.session_state["not_downloaded"] = {}
# Pages
bot_chat = st.Page(Bot_Chat)
projects = st.Page(Projects)
article_collections = st.Page(Article_Collections)
settings = st.Page(Settings)
rss_feeds = st.Page(RSS_Feeds)
sleep(0.1)
pg = st.navigation([bot_chat, projects, article_collections, rss_feeds, settings])
sleep(0.1)
pg.run()
# try: #TODO Use this when in production
# pg.run()
# except Exception as e:
# print_red(e)
# st.error("An error occurred. The site will be reloaded.")
# import traceback
# from datetime import datetime
# from time import sleep
# traceback_string = traceback.format_exc()
# traceback.print_exc()
# arango = ArangoDB(db_name="base")
# timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
# print_rainbow(st.session_state.to_dict())
# session_state = st.session_state.to_dict()
# if 'bot' in session_state:
# del session_state['bot']
# arango.db.collection("error_logs").insert(
# {
# "error": traceback_string,
# "_key": timestamp,
# "session_state": session_state,
# },
# overwrite=True,
# )
# with st.status(":red[An error occurred. The site will be reloaded.]"):
# for i in range(5):
# sleep(1)
# st.write(f"Reloading in {5-i} seconds...")
# st.rerun()
with st.sidebar:
st.write("---")
authenticator.logout()
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")