Files
annescribe/webapp_main.py
T
2024-12-29 21:27:11 +01:00

66 lines
1.9 KiB
Python

import os
import tempfile
import json
import yaml
import subprocess
import streamlit as st
import streamlit_authenticator as st_auth
from streamlit_autorefresh import st_autorefresh
from webapp.home import render_home
CONFIG_FILE = os.environ.get('CONFIG_FILE', "config.yml")
if CONFIG_FILE is None:
raise ValueError('CONFIG_FILE environment variable is not set')
elif not os.path.exists(CONFIG_FILE):
raise ValueError(f'CONFIG_FILE {CONFIG_FILE} does not exist')
with open(CONFIG_FILE, 'r') as config_file:
config = yaml.safe_load(config_file)
AUTH_FILE = os.environ.get('AUTH_FILE', "auth.yml")
if AUTH_FILE is None:
raise ValueError('AUTH_YML_FILE environment variable is not set')
elif not os.path.exists(AUTH_FILE):
raise ValueError(f'AUTH_YML_FILE {AUTH_FILE} does not exist')
with open(AUTH_FILE, 'r') as auth_file:
auth_config = yaml.safe_load(auth_file)
authenticator = st_auth.Authenticate(
auth_config["credentials"],
auth_config["cookie"]["name"],
auth_config["cookie"]["key"],
auth_config["cookie"]["expiry_days"],
)
content_wrapper = st.empty()
def render_login():
with content_wrapper.container():
st.title('Annescribe Login')
st.write("Please login with your credentials.")
authenticator.login(callback=st.session_state.clear())
if "authentication_status" not in st.session_state.keys():
authentication_status = None
st.session_state["authentication_status"] = authentication_status
else:
authentication_status = st.session_state.get("authentication_status")
username = st.session_state.get("username")
print(authentication_status)
if authentication_status is True:
render_home(content_wrapper, config, authenticator)
elif authentication_status is None:
st.warning('Please enter your username and password')
render_login()
else:
# make sure to remove cookie
render_login()
# st_autorefresh(interval=2000, )