llm-arch / src /st_helpers.py
alfraser's picture
Removed debug print statements which cause extra chaff in the logs
1c63a8a
raw
history blame
2.21 kB
import streamlit as st
def login(pw: str) -> bool:
"""
Convenience wrapper for login functionality. Has side effect of storing the logged in
user name in the session state
:param pw: Password to attempt login
:return: True/False if logged in
"""
pw_users = st.secrets['app_password'].split(';')
for pu in pw_users:
split_point=pu.find('(')
pu_password = pu[0:split_point]
if pu_password == pw:
pu_user = pu[split_point + 1: -1]
st.session_state['username'] = pu_user
return True
return False
def st_setup(page_title: str, layout: str = 'wide', skip_login: bool = False):
"""
Sets up standard outline (wide layout), checks for logged in status and then
displays the login box if not logged in. Should be used as a conditional to display
the other content on the page.
:param page_title: The streamlit page title for this page
:return: bool - logged in status
"""
st.set_page_config(page_title=page_title, layout=layout)
username='username'
if username in st.session_state:
st.markdown(f'<div style="float: right; padding-top: 10px;">Logged in as <b>{st.session_state[username]}</b></span>', unsafe_allow_html=True)
with st.sidebar:
st.image('img/uob-logo.png', width=200)
if skip_login:
return True
# Option to add a running_local key to secrets.toml for local login to speed up development
# This relies on the protection of streamlit secrets in production, which password login already relies on
running_local = 'running_local'
if running_local in st.secrets:
return True
logged_in = 'logged_in'
if logged_in not in st.session_state or st.session_state[logged_in]==False:
_, c2, _ = st.columns(3)
with c2:
st.write('### Log in')
pw = st.text_input(type='password', label='Password')
if st.button('Log in'):
if login(pw):
st.session_state[logged_in]=True
st.rerun()
else:
st.info("Invalid password")
return False
else:
return True