|
|
|
|
|
|
|
|
import os |
|
|
import sys |
|
|
import streamlit as st |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) |
|
|
if PROJECT_ROOT not in sys.path: |
|
|
sys.path.insert(0, PROJECT_ROOT) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
APP_TITLE = "Academia ARM — Cursos" |
|
|
APP_ICON = "🎓" |
|
|
APP_LAYOUT = "wide" |
|
|
|
|
|
try: |
|
|
|
|
|
from Config.settings import APP_TITLE as _TITLE, APP_ICON as _ICON, APP_LAYOUT as _LAYOUT |
|
|
APP_TITLE, APP_ICON, APP_LAYOUT = _TITLE, _ICON, _LAYOUT |
|
|
except ModuleNotFoundError: |
|
|
try: |
|
|
|
|
|
from config.settings import APP_TITLE as _TITLE, APP_ICON as _ICON, APP_LAYOUT as _LAYOUT |
|
|
APP_TITLE, APP_ICON, APP_LAYOUT = _TITLE, _ICON, _LAYOUT |
|
|
except ModuleNotFoundError: |
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.set_page_config(page_title=APP_TITLE, page_icon=APP_ICON, layout=APP_LAYOUT) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _try_import_utils(): |
|
|
try: |
|
|
from utils import ensure_dirs |
|
|
return ensure_dirs |
|
|
except Exception as e: |
|
|
st.warning( |
|
|
"⚠️ Não foi possível importar `utils.ensure_dirs`. " |
|
|
"Se você não precisa criar diretórios locais, pode ignorar. " |
|
|
f"Detalhes: {e}" |
|
|
) |
|
|
return lambda: None |
|
|
|
|
|
ensure_dirs = _try_import_utils() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _try_import_db(): |
|
|
try: |
|
|
from db import init_db |
|
|
return init_db |
|
|
except Exception as e: |
|
|
st.warning( |
|
|
"⚠️ Não foi possível importar `db.init_db`. " |
|
|
"Se você usa persistência via Hugging Face Dataset, " |
|
|
"verifique o módulo `db.py` ou `database/hfdb.py`. " |
|
|
f"Detalhes: {e}" |
|
|
) |
|
|
return lambda: None |
|
|
|
|
|
init_db = _try_import_db() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _try_import_hfdb(): |
|
|
try: |
|
|
from database.hfdb import ensure_db_file, load_data, save_data |
|
|
return ensure_db_file, load_data, save_data, None |
|
|
except Exception as e: |
|
|
return None, None, None, e |
|
|
|
|
|
ensure_db_file, load_data, save_data, hfdb_err = _try_import_hfdb() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _try_import_pages(): |
|
|
page_aluno = None |
|
|
page_admin = None |
|
|
aluno_err = admin_err = None |
|
|
|
|
|
try: |
|
|
import aluno as _aluno |
|
|
page_aluno = _aluno |
|
|
except Exception as e: |
|
|
aluno_err = e |
|
|
|
|
|
try: |
|
|
import admin as _admin |
|
|
page_admin = _admin |
|
|
except Exception as e: |
|
|
admin_err = e |
|
|
|
|
|
return page_aluno, aluno_err, page_admin, admin_err |
|
|
|
|
|
page_aluno, aluno_import_err, page_admin, admin_import_err = _try_import_pages() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
ensure_dirs() |
|
|
except Exception as e: |
|
|
st.warning(f"⚠️ Falha ao preparar diretórios locais: {e}") |
|
|
|
|
|
|
|
|
try: |
|
|
init_db() |
|
|
except Exception as e: |
|
|
st.error("❌ Falha ao inicializar o banco de dados/persistência (init_db).") |
|
|
st.exception(e) |
|
|
|
|
|
|
|
|
DB_STATE = None |
|
|
if ensure_db_file and load_data: |
|
|
try: |
|
|
ensure_db_file() |
|
|
DB_STATE = load_data() |
|
|
except Exception as e: |
|
|
st.error("❌ Não foi possível preparar/carregar o arquivo de banco no Dataset (HF).") |
|
|
st.exception(e) |
|
|
elif hfdb_err: |
|
|
st.info( |
|
|
"ℹ️ Persistência via Hugging Face Dataset não habilitada. " |
|
|
"Crie `database/hfdb.py` e configure os Secrets/Variables para persistência." |
|
|
) |
|
|
|
|
|
|
|
|
st.session_state.setdefault("is_admin", False) |
|
|
st.session_state.setdefault("admin_user", None) |
|
|
st.session_state.setdefault("aluno_logged_in", False) |
|
|
st.session_state.setdefault("aluno_nome", "") |
|
|
st.session_state.setdefault("aluno_email", "") |
|
|
st.session_state.setdefault("cal_ver", 0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _debug_env(): |
|
|
with st.expander("🔎 Debug rápido (ambiente)", expanded=False): |
|
|
st.write("HF_DATASET_PATH:", os.getenv("HF_DATASET_PATH")) |
|
|
st.write("HF_DB_FILE:", os.getenv("HF_DB_FILE")) |
|
|
st.write("HF_TOKEN presente?:", bool(os.getenv("HF_TOKEN"))) |
|
|
st.write("hfdb import error:", str(hfdb_err) if hfdb_err else None) |
|
|
st.write("DB_STATE inicial carregado?:", DB_STATE is not None) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main() -> None: |
|
|
st.title(APP_TITLE) |
|
|
_debug_env() |
|
|
|
|
|
|
|
|
if aluno_import_err: |
|
|
st.warning(f"⚠️ Módulo `aluno` não pôde ser carregado: {aluno_import_err}") |
|
|
if admin_import_err: |
|
|
st.warning(f"⚠️ Módulo `admin` não pôde ser carregado: {admin_import_err}") |
|
|
|
|
|
|
|
|
if DB_STATE is not None: |
|
|
with st.expander("🗄️ Snapshot do banco (somente leitura)", expanded=False): |
|
|
try: |
|
|
st.json(DB_STATE) |
|
|
except Exception: |
|
|
st.write(DB_STATE) |
|
|
|
|
|
tab_aluno, tab_admin = st.tabs(["👩🎓 Aluno", "🛠️ Admin"]) |
|
|
|
|
|
with tab_aluno: |
|
|
if page_aluno and hasattr(page_aluno, "render"): |
|
|
try: |
|
|
|
|
|
|
|
|
|
|
|
page_aluno.render() |
|
|
except Exception as e: |
|
|
st.error("Ocorreu um erro ao carregar a aba **Aluno**. Os demais menus continuam disponíveis.") |
|
|
st.exception(e) |
|
|
else: |
|
|
st.info("A aba **Aluno** ainda não está disponível. Verifique o arquivo `aluno.py` e a função `render()`.") |
|
|
|
|
|
with tab_admin: |
|
|
if page_admin and hasattr(page_admin, "render"): |
|
|
try: |
|
|
|
|
|
page_admin.render() |
|
|
except Exception as e: |
|
|
st.error("Ocorreu um erro ao carregar a aba **Admin**.") |
|
|
st.exception(e) |
|
|
else: |
|
|
st.info("A aba **Admin** ainda não está disponível. Verifique o arquivo `admin.py` e a função `render()`.") |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |