File size: 2,248 Bytes
d934e05
 
1e508c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1f9b8f
1e508c0
 
 
 
f8409b4
d934e05
 
 
 
 
 
 
 
1e508c0
 
 
2c5b1e7
1e508c0
d934e05
 
 
f8409b4
 
 
33de646
 
 
 
 
 
 
d934e05
 
 
 
 
 
1e508c0
d934e05
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
            print(f'{pu_user} logged in')
            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