PortalLVAM / app.py
bullm's picture
Update app.py
05cd3e5
from multiapp import ViewPortal, Auth
import pandas as pd
from sqlalchemy import create_engine
from Data import credentials
import streamlit as st
from streamlit_multipage import MultiPage
import extra_streamlit_components as stx
import datetime
def button_style():
style_button = """
<style>
.button {
display: inline-block;
background-color: #e8e8e8;
border-radius: 20px;
border: 4px #cccccc;
color: #4a4a4a;
text-align: center;
font-size: 12px;
padding: 2px;
width: 550px;
height:70px;
transition: all 0.5s;
cursor: pointer;
margin: 5px;
}
.button span {
cursor: pointer;
display: inline-block;
position: relative;
transition: 0.5s;
}
.button span:after {
content: '\00bb';
position: absolute;
opacity: 0;
top: 0;
right: -20px;
transition: 0.5s;
}
.button:hover {
background-color: #bb1114;
color:white;
}
button:hover span {
padding-right: 25px;
}
button:hover span:after {
opacity: 1;
right: 0;
}
.stMarkdown{
margin-bottom:0px;}
</style>
"""
st.markdown(style_button, unsafe_allow_html=True)
def style_title():
style = """
<style>
h1, h3{
color: "white";
font-family: Optima, sans-serif;
}
h1{
font-size:40px;
}
.st-ae {
font-family: Optima, sans-serif;
}
*
{
font-family: Optima, sans-serif;
}
.css-1y0tadsbody
{
padding:40px;
}
</style>
"""
st.markdown(style, unsafe_allow_html=True)
def get_user():
user = st.session_state.key
return user
def get_password():
password = st.session_state['pass']
return password
# @st.experimental_memo
def get_session_state():
return st.session_state
@st.experimental_memo
def read_cred():
url = credentials.credentials_postgresql["POSTGRESQL"]
engine = create_engine(url, echo=False)
credenciales = pd.read_sql_query("""select * from credenciales""",
con=engine)
credenciales.columns = ['Usuario', 'Password', 'Area', 'Cargo',
'Mail', 'Nombre']
engine.dispose()
return credenciales
def input_page(st, **state):
MultiPage.save({"user": "", "passw": ""})
user = st.sidebar.text_input('User', key="1")
passw = st.sidebar.text_input('Password', key="2")
if user and passw:
MultiPage.save({"user": user, "passw": passw})
def login_cookies(cookie_manager):
c1, c2, c3 = st.columns(3)
with c2:
user = st.text_input("User")
passw = st.text_input("Password")
if st.button("Login"):
cookie_manager.set("user", user, key="user", expires_at=datetime.datetime(year=2023, month=2, day=2))
cookie_manager.set("passw", passw, key="password", expires_at=datetime.datetime(year=2023, month=2, day=2))
return user, passw
def compute_page():
def get_manager():
return stx.CookieManager()
cookie_manager = get_manager()
cookies = cookie_manager.get_all()
st.write(cookies)
credenciales = read_cred()
try:
user = cookies["user"]
password = cookies["passw"]
mail = st.session_state['mail']
cargo = st.session_state['cargo']
password = st.session_state['pass']
area = st.session_state['area']
name = st.session_state["name"]
# st.write(get_session_state())
Portal = ViewPortal()
Portal.set_user(user, area, mail, cargo)
Portal.run_views()
except Exception as exc:
try:
auth = Auth()
user = cookies["user"]
password = cookies["passw"]
auth.log_in(user, password)
if auth.state:
user = auth.user
st.session_state.key = user
name = credenciales[credenciales["Usuario"]==user].iloc[0]["Nombre"]
st.session_state["name"] = name
area = auth.area
mail = auth.mail
cargo = auth.cargo
st.session_state['mail'] = mail
st.session_state['cargo'] = cargo
st.session_state['pass'] = password
st.session_state['area'] = area
Portal = ViewPortal()
Portal.set_user(user, area, mail, cargo)
Portal.run_views()
except:
auth = Auth()
if auth.state == "":
user, passw = login_cookies(cookie_manager)
auth.log_in(user, passw)
if auth.state:
user = auth.user
st.session_state.key = user
name = credenciales[credenciales["Usuario"] == user].iloc[0]["Nombre"]
st.session_state["name"] = name
area = auth.area
mail = auth.mail
cargo = auth.cargo
st.session_state['mail'] = mail
st.session_state['cargo'] = cargo
st.session_state['pass'] = passw
st.session_state['area'] = area
Portal = ViewPortal()
Portal.set_user(user, area, mail, cargo)
Portal.run_views()
else:
st.sidebar.error("Clave incorrecta")
def set_page_container_style(
max_width: int = 1100, max_width_100_percent: bool = False,
padding_top: int = 1, padding_right: int = 10, padding_left: int = 1, padding_bottom: int = 10,
color: str = 'black', background_color: str = 'white',
):
if max_width_100_percent:
max_width_str = f'max-width: 100%;'
else:
max_width_str = f'max-width: {max_width}px;'
st.markdown(
f'''
<style>
.css-189x3qo {{
padding-top: {padding_top}rem;
}}
.reportview-container .main .block-container {{
{max_width_str}
padding-top: {padding_top}rem;
padding-right: {padding_right}rem;
padding-left: {padding_left}rem;
padding-bottom: {padding_bottom}rem;
}}
.reportview-container .main {{
color: {color};
background-color: {background_color};
}}
</style>
''',
unsafe_allow_html=True,
)
if __name__ == '__main__':
import streamlit_authenticator as stauth
import yaml
st.set_page_config(layout="wide", page_title="Portal LVAM")
hashed_passwords = stauth.Hasher(["hola", "hola"]).generate()
with open('config.yaml') as file:
config = yaml.load(file, Loader=yaml.SafeLoader)
authenticator = stauth.Authenticate(
config['credentials']['names'],
config['credentials']['usernames'],
hashed_passwords,
config['cookie']['name'],
config['cookie']['key'],
config['cookie']['expiry_days']
)
name, authentication_status, username = authenticator.login('Login', 'main')
if st.session_state["authentication_status"]:
authenticator.logout('Logout', 'main')
st.write(f'Welcome *{st.session_state["name"]}*')
st.title('Some content')
elif st.session_state["authentication_status"] == False:
st.error('Username/password is incorrect')
elif st.session_state["authentication_status"] == None:
st.warning('Please enter your username and password')
# if __name__ == '__main__':
# st.set_page_config(layout="wide", page_title="Portal LVAM",
# page_icon="img/icono.png")
# place_0 = st.empty()
# col1, col2 = st.columns((2, 1))
# place_image = st.sidebar.empty()
# place_form = st.sidebar.empty()
# form1 = place_form.form("Ingresar")
# logged = False
# try:
# user = st.session_state.key
# password = st.session_state['pass']
# mail = st.session_state['mail']
# cargo = st.session_state['cargo']
# password = st.session_state['pass']
# area = st.session_state['area']
# name = st.session_state["name"]
# # st.write(get_session_state())
# Portal = ViewPortal()
# Portal.set_user(user, area, mail, cargo)
# Portal.run_views()
# except Exception as exc:
# st.write(exc)
# url = credentials.credentials_postgresql["POSTGRESQL"]
# engine = create_engine(url, echo=False)
# credenciales = pd.read_sql_query(
# """select * from credenciales""", con=engine)
# credenciales.columns = ['Usuario', 'Password',
# 'Area', 'Cargo', 'Mail', 'Nombre']
# try:
# usuario = get_user()
# except:
# usuario = ""
# user = form1.text_input("Enter a username", value=usuario)
# password = form1.text_input("Enter a password",
# type="password")
# submitted = form1.form_submit_button("Ingresar")
# auth = Auth()
# auth.log_in(user, password)
# if auth.state:
# user = auth.user
# st.session_state.key = user
# name = credenciales[credenciales["Usuario"]==user].iloc[0]["Nombre"]
# st.session_state["name"] = name
# area = auth.area
# mail = auth.mail
# cargo = auth.cargo
# st.session_state['mail'] = mail
# st.session_state['cargo'] = cargo
# st.session_state['pass'] = password
# st.session_state['area'] = area
# place_form.empty()
# place_image.empty()
# Portal = ViewPortal()
# Portal.set_user(user, area, mail, cargo)
# Portal.run_views()
# elif auth.state == "":
# st.write("")
# else:
# st.sidebar.error("Clave incorrecta")