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.
107 lines
4.0 KiB
107 lines
4.0 KiB
import streamlit as st
|
|
import pandas as pd
|
|
import requests
|
|
from streamlit_cookies_manager import CookieManager
|
|
from sqlalchemy import create_engine, text
|
|
db = st.secrets.db_info
|
|
auth = st.secrets.auth
|
|
|
|
cookies = CookieManager()
|
|
if not cookies.ready():
|
|
# Wait for the component to load and send us current cookies.
|
|
st.stop()
|
|
|
|
|
|
if token:=cookies.get("token"):
|
|
st.session_state["token"] = token
|
|
|
|
|
|
def check_token():
|
|
if 'token' in st.session_state:
|
|
r = requests.get(auth.check_url+st.session_state["token"])
|
|
if r.status_code==200:
|
|
st.session_state["auth"]=r.json()
|
|
return True
|
|
return False
|
|
|
|
|
|
def logout():
|
|
del st.session_state["token"]
|
|
del cookies["token"]
|
|
|
|
def get_token(username:str,password:str ):
|
|
r = requests.post(auth.auth_url,data={'username':username, 'password':password})
|
|
if r.status_code==200:
|
|
data = r.json()
|
|
st.session_state["token"] = data["access_token"]
|
|
cookies["token"] = data["access_token"]
|
|
return True
|
|
return False
|
|
|
|
def login_form():
|
|
with st.form("Авторизация"):
|
|
username = st.text_input(label="Имя пользователя")
|
|
password = st.text_input(label="Пароль", type="password")
|
|
submitted = st.form_submit_button("Войти")
|
|
if all((submitted , username, password)):
|
|
if get_token(username,password):
|
|
st.rerun()
|
|
else:
|
|
st.error("Имя или пароль указаны неправильно!")
|
|
else:
|
|
st.warning("Нужно заполнить имя и пароль!")
|
|
def side_bar():
|
|
with st.sidebar:
|
|
st.markdown(f"""Вы вошли как <span style='color: red;'>{st.session_state.auth.get('login')}</span>""", unsafe_allow_html=True)
|
|
logoutted = st.button("Выход")
|
|
if logoutted:
|
|
logout()
|
|
st.rerun()
|
|
with st.popover("Open popover"):
|
|
st.markdown("Hello World 👋")
|
|
name = st.text_input("What's your name?")
|
|
st.write(name)
|
|
|
|
|
|
|
|
def home_page():
|
|
option = st.selectbox(
|
|
"Выберете далее",
|
|
("SQL", "EXCEL"),
|
|
)
|
|
if option =="SQL":
|
|
engine= create_engine(f"mssql+pyodbc://{db.username}:{db.password}@{db.host}/{db.db_name}?driver=ODBC+Driver+17+for+SQL+Server")
|
|
|
|
df = pd.read_sql_query(sql =text("SELECT * FROM tParameters"), con=engine, index_col="id")
|
|
search_str = st.text_input("Строка поиска")
|
|
df_top5 = df[df["name"].str.lower().str.contains(search_str.lower())].groupby().aggregate("value":"avg")
|
|
table = st.dataframe(df_top5, selection_mode='single-row', on_select='rerun')
|
|
if row_id :=table.get("selection").get("rows"):
|
|
vote(row_id[0],df.iloc[row_id[0]].to_dict())
|
|
elif option == 'EXCEL':
|
|
uploaded_file = st.file_uploader("Загрузить файл EXCEL")
|
|
if uploaded_file is not None:
|
|
dataframe = pd.read_excel(uploaded_file)
|
|
st.write(dataframe)
|
|
st.link_button("Переход на страницу Reports","/Reports")
|
|
|
|
@st.dialog("Модальное окно")
|
|
def vote(id: int,item_dict:dict):
|
|
id = st.text_input(label="ID", value=str(id), disabled=True)
|
|
id_group = st.text_input(label="id_group", value=str(item_dict.get('id_group')),)
|
|
name = st.text_input(label="name", value=str(item_dict.get('name')),)
|
|
format = st.text_input(label="format", value=str(item_dict.get('format')),)
|
|
vtable = st.text_input(label="vtable", value=str(item_dict.get('vtable')),)
|
|
unit = st.text_input(label="unit", value=str(item_dict.get('unit')),)
|
|
exact_format = st.text_input(label="exact_format", value=str(item_dict.get('exact_format')),)
|
|
inHistory = st.text_input(label="inHistory", value=str(item_dict.get('inHistory')),)
|
|
if st.button("Submit"):
|
|
#какая то функция обновления данных
|
|
st.rerun()
|
|
|
|
if __name__ == "__main__":
|
|
if check_token():
|
|
side_bar()
|
|
home_page()
|
|
else:
|
|
login_form()
|