Spaces:
Running
Running
Commit
·
2a1a0bb
1
Parent(s):
3e8681f
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from sqlalchemy import create_engine
|
4 |
+
from filter import filter_dataframe
|
5 |
+
import toml
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Caminho para o arquivo secrets.toml na pasta .streamlit
|
9 |
+
secrets_path = os.path.join(".streamlit", "secrets.toml")
|
10 |
+
|
11 |
+
# Verificar se o arquivo secrets.toml existe
|
12 |
+
if not os.path.isfile(secrets_path):
|
13 |
+
st.error("O arquivo 'secrets.toml' não foi encontrado. Certifique-se de que ele está na pasta '.streamlit'.")
|
14 |
+
st.stop()
|
15 |
+
|
16 |
+
# Carregar as informações de secrets.toml
|
17 |
+
secrets = toml.load(secrets_path)
|
18 |
+
|
19 |
+
# Dados de login
|
20 |
+
USERNAME = secrets["login"]["username"]
|
21 |
+
PASSWORD = secrets["login"]["password"]
|
22 |
+
|
23 |
+
# Função para autenticação do usuário
|
24 |
+
def authenticate_user(username: str, password: str) -> bool:
|
25 |
+
return username == USERNAME and password == PASSWORD
|
26 |
+
|
27 |
+
# Tela de login
|
28 |
+
def login_screen():
|
29 |
+
st.title('Tela de Login')
|
30 |
+
|
31 |
+
with st.form(key='login_form'):
|
32 |
+
username = st.text_input("Nome de usuário")
|
33 |
+
password = st.text_input("Senha", type='password')
|
34 |
+
|
35 |
+
# Botão para submeter o formulário
|
36 |
+
submit_button = st.form_submit_button('Entrar')
|
37 |
+
|
38 |
+
if submit_button:
|
39 |
+
if authenticate_user(username, password):
|
40 |
+
st.session_state['logged_in'] = True
|
41 |
+
st.rerun()
|
42 |
+
else:
|
43 |
+
st.error('Nome de usuário ou senha inválidos')
|
44 |
+
|
45 |
+
# Função para ler dados da tabela SQL a partir de um arquivo
|
46 |
+
def load_data() -> pd.DataFrame:
|
47 |
+
db_config = secrets["database"]
|
48 |
+
dialect = db_config["dialect"]
|
49 |
+
driver = db_config["driver"]
|
50 |
+
username = db_config["username"]
|
51 |
+
password = db_config["password"]
|
52 |
+
host = db_config["host"]
|
53 |
+
port = db_config["port"]
|
54 |
+
database = db_config["database"]
|
55 |
+
|
56 |
+
connection_url = f"{dialect}+{driver}://{username}:{password}@{host}:{port}/{database}"
|
57 |
+
|
58 |
+
engine = create_engine(connection_url)
|
59 |
+
|
60 |
+
with open('query.sql', 'r', encoding='utf-8') as file:
|
61 |
+
query = file.read()
|
62 |
+
|
63 |
+
df = pd.read_sql(query, engine)
|
64 |
+
return df
|
65 |
+
|
66 |
+
# Função para download dos dados como CSV
|
67 |
+
def convert_df_to_csv(df):
|
68 |
+
return df.to_csv(index=False).encode('utf-8')
|
69 |
+
|
70 |
+
# Verificar o estado de login
|
71 |
+
if 'logged_in' not in st.session_state:
|
72 |
+
st.session_state['logged_in'] = False
|
73 |
+
|
74 |
+
# Se o usuário não está logado, exibe a tela de login
|
75 |
+
if not st.session_state['logged_in']:
|
76 |
+
login_screen()
|
77 |
+
else:
|
78 |
+
st.title('Tabela Future Mining')
|
79 |
+
|
80 |
+
df = load_data()
|
81 |
+
|
82 |
+
filtered_df = filter_dataframe(df)
|
83 |
+
|
84 |
+
all_columns = filtered_df.columns.tolist()
|
85 |
+
selected_columns = st.multiselect(
|
86 |
+
'Escolha as colunas para exibir:',
|
87 |
+
options=all_columns,
|
88 |
+
default=[]
|
89 |
+
)
|
90 |
+
|
91 |
+
if selected_columns:
|
92 |
+
displayed_df = filtered_df[selected_columns]
|
93 |
+
else:
|
94 |
+
displayed_df = filtered_df
|
95 |
+
|
96 |
+
st.write('Tabela com Colunas Selecionadas')
|
97 |
+
st.dataframe(displayed_df, column_order=selected_columns if selected_columns else None)
|
98 |
+
|
99 |
+
# Botão para download
|
100 |
+
csv = convert_df_to_csv(displayed_df)
|
101 |
+
st.download_button(
|
102 |
+
label="Baixar CSV",
|
103 |
+
data=csv,
|
104 |
+
file_name='dados_filtrados.csv',
|
105 |
+
mime='text/csv',
|
106 |
+
)
|